reg/events.py
author nishanth
Thu, 15 Apr 2010 14:38:48 +0530
changeset 43 757d1da69255
parent 23 42e2a810e1c8
child 45 b66d405eb8c7
permissions -rw-r--r--
added venue field in event model and corresponding forms and views.
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
17
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    10
def create_user(email, password, first_name="", last_name="", 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)
17
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    21
    new_user.first_name = first_name
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    22
    new_user.last_name = last_name
2
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
20
9354ef8119c6 added account_inactive and resend_activationkey functionalities
nishanth
parents: 18
diff changeset
    40
def send_activation(user):
9354ef8119c6 added account_inactive and resend_activationkey functionalities
nishanth
parents: 18
diff changeset
    41
    """ get key from profile and send an email.
9354ef8119c6 added account_inactive and resend_activationkey functionalities
nishanth
parents: 18
diff changeset
    42
    """
9354ef8119c6 added account_inactive and resend_activationkey functionalities
nishanth
parents: 18
diff changeset
    43
23
42e2a810e1c8 modified seed_db and view_event template .
nishanth
parents: 20
diff changeset
    44
    print "http://localhost:8000/reg/activate/%s"%user.get_profile().activation_key
20
9354ef8119c6 added account_inactive and resend_activationkey functionalities
nishanth
parents: 18
diff changeset
    45
43
757d1da69255 added venue field in event model and corresponding forms and views.
nishanth
parents: 23
diff changeset
    46
def create_event(title, description, start_date, stop_date, venue, created_by=None):
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    47
    """ make an event and save it.
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    48
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    49
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    50
    new_event = Event()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    51
    new_event.title = title
8
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 7
diff changeset
    52
    new_event.description = description
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    53
    new_event.start_date = start_date
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    54
    new_event.stop_date = stop_date
43
757d1da69255 added venue field in event model and corresponding forms and views.
nishanth
parents: 23
diff changeset
    55
    new_event.venue = venue
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    56
    new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    57
    if created_by:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    58
        new_event.organizers.add(created_by)
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    59
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    60
    while True:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    61
        try:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    62
            new_event.key = gen_key(10)
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    63
            new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    64
            return new_event
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    65
        except IntegrityError:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    66
            pass
7
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    67
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    68
def activate_user(user):
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    69
    """ mark the is_active flag as true.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    70
    """
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    71
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    72
    user.is_active = True
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    73
    user.save()
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    74
    return user
9
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    75
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    76
def reset_password(user):
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    77
    """ get a key and set it as password.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    78
    for now, print the key.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    79
    later on add the send mail function.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    80
    """
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    81
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    82
    new_password = gen_key(10)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    83
    user.set_password(new_password)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    84
    user.save()
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    85
    return new_password
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    86
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    87
def change_password(user, new_password):
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    88
    """ for now just set the password and be done with it.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    89
    later on, if we want to do something else also then we can add them here.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    90
    """
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    91
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    92
    user.set_password(new_password)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    93
    user.save()
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    94
17
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    95
def update_profile(user, properties):
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    96
    """ properties must be a dictionary that contains all the attributes that
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    97
    we take while a new user registers.
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
    98
    """
9
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    99
17
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   100
    profile = user.get_profile()
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   101
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   102
    user.first_name = properties['first_name']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   103
    user.last_name = properties['last_name']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   104
    user.save()
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   105
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   106
    profile.gender = properties['gender']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   107
    profile.profession = properties['profession']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   108
    profile.affiliated_to = properties['affiliated_to']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   109
    profile.interests = properties['interests']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   110
    profile.save()
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   111
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   112
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   113