reg/events.py
author nishanth
Thu, 08 Apr 2010 22:28:15 +0530
changeset 2 c11afa8623f7
child 6 057498d12450
permissions -rw-r--r--
incorporated gen_key .
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     1
#!/usr/bin/python
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     3
from django.contrib.auth.models import User
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     4
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     5
from django.db import IntegrityError
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     6
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     7
from workshop.reg.models import Event
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     8
from workshop.reg.utils import gen_key
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     9
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    10
def create_user(email, password, firstname="", lastname=""):
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    11
    """ create a user with random username and set the password.
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    12
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    13
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    14
    username = ('%s %s'%(firstname, lastname)).title().strip()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    15
    new_user = User.objects.create_user(username, email, password)
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    16
    new_user.firstname = firstname
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    17
    new_user.lastname = lastname
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    18
    new_user.is_active = False
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    19
    new_user.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    20
    return new_user
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    21
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    22
def create_event(title, desc, start_date, stop_date, created_by=None):
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    23
    """ make an event and save it.
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    24
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    25
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    26
    new_event = Event()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    27
    new_event.title = title
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    28
    new_event.description = desc
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    29
    new_event.start_date = start_date
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    30
    new_event.stop_date = stop_date
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    31
    new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    32
    if created_by:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    33
        new_event.organizers.add(created_by)
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    34
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    35
    new_event.key = gen_key()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    36
    while True:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    37
        try:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    38
            new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    39
            return new_event
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    40
        except IntegrityError:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    41
            new_event.key = gen_key()