Lottery machine

 


range - from 1 to 1000
amount of numbers 1-30

Altough my lottery row generator is mighty impressive codewise, the purpose of this project is not to demonstrate my superior programmer skills :) This page is powered by Django, and this whole project was a learning experience for me, mostly on how to set up a django site on shared hosting. The page is also using JQuery, for getting new numbers without reloading the page. JQuery is again something I've been waiting to try out with Django and this little project suited well for that purpose too (altough I only use it for the most basic AJAX and some animations).

This project uses FCGI / Apache method, and it seems to work quite nicely. The configuration is straigth from Django docs. Below you can find the code for generating random lottery numbers along with the rest of the code from the views.py (django served data), in case you're interested :) "getnumber" -view is the function called by the JQuery ajax handler, it takes the paremetrs "to" and "amount" as GET data. The decorator @never_cache is used because otherwise some web browsers (IE!) would cache the data, and only numbers that are fetched the first time would be presented. The decorator makes sure that the GET request is not cached. I guess this is handled by setting correct headers to http response.

The JQuery stuff you can see by looking at the source of this page.

views.py

# -*- coding: iso-8859-1 -*-

import random
import os

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
from myproject.settings import LOTTOPATH

def get_random_values(to, amount):
    numbers = []
    random_values = []
    
    for i in range(1,to+1):
        numbers.append(i)

    for i in range(1, amount+1):
        random_number = random.randint(0, len(numbers)-1)
        random_values.append(numbers.pop(random_number))

    random_values.sort()
    return random_values


def validates(to, amount):
    try:
        int(to)
        int(amount)
    except:
        return False
    if int(to) < int(amount):
        return False
    if int(to) < 1 or int(to) > 1000:
        return False
    if int(amount) < 1 or int(amount) > 30:
        return False
    return True 

def index(request):
    f = open(os.path.join(LOTTOPATH, "views.py"), "r")
    lines = f.readlines()
    f.close()
    return render_to_response('lotto/index.html', {'lines' : lines})

@never_cache
def getnumber(request):
    if 'to' in request.GET and 'amount' in request.GET:
        if validates(request.GET['to'], request.GET['amount']):
            numbers = get_random_values(int(request.GET['to']), int(request.GET['amount']))
            string = ""
            id = "id_"
            for number in numbers:
                id += str(number)
                string += '<div id="'+id+'"class="animation">' + str(number)+ '</div>\n'
            string += '<div style="height: 43px">&nbsp</div>'
            return HttpResponse(string)
 
    return HttpResponse("[INVALID USER INPUT]")


...back to projects...