reg/events.py
author nishanth
Fri, 09 Apr 2010 15:48:47 +0530
changeset 7 af9ab5ad2786
parent 6 057498d12450
child 8 e2699e042129
permissions -rw-r--r--
fixed a bug in registration .
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
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
     7
from workshop.reg.models import Profile, Event
2
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
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    10
def create_user(email, password, firstname="", lastname="", gender="M", profession="S", affiliated_to="", interests="" ):
2
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
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    14
    while True:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    15
        try:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    16
            username = gen_key(8)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    17
            user = User.objects.get(username=username)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    18
        except User.DoesNotExist:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    19
            break
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    20
    new_user = User.objects.create_user(username, email, password)
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    21
    new_user.firstname = firstname
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    22
    new_user.lastname = lastname
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    23
    new_user.is_active = False
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    24
    new_user.save()
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    25
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    26
    new_profile = Profile(user=new_user)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    27
    new_profile.gender = gender
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    28
    new_profile.profession = profession
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    29
    new_profile.affiliated_to = affiliated_to
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    30
    new_profile.interests = interests
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    31
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    32
    while True:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    33
        try:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    34
            new_profile.activation_key = gen_key(30)
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    35
            new_profile.save()
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    36
            return new_user
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    37
        except IntegrityError:
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    38
            pass
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    39
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    40
def create_event(title, desc, start_date, stop_date, created_by=None):
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    41
    """ make an event and save it.
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    42
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    43
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    44
    new_event = Event()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    45
    new_event.title = title
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    46
    new_event.description = desc
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    47
    new_event.start_date = start_date
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    48
    new_event.stop_date = stop_date
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    49
    new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    50
    if created_by:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    51
        new_event.organizers.add(created_by)
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    52
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    53
    while True:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    54
        try:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    55
            new_event.key = gen_key(10)
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    56
            new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    57
            return new_event
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    58
        except IntegrityError:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    59
            pass
7
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    60
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    61
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    62
def activate_user(user):
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    63
    """ mark the is_active flag as true.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    64
    """
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    65
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    66
    user.is_active = True
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    67
    user.save()
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    68
    return user