reg/events.py
author nishanth
Fri, 23 Apr 2010 16:35:42 +0530
changeset 98 1af134a1e53d
parent 84 bb6a1bd4b3f8
permissions -rwxr-xr-x
now an email will be sent to user after he resets his password.
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
46
ff5f34e42aec registration for workshop is now integrated with man registartion.
nishanth
parents: 45
diff changeset
     4
from django.core.mail import send_mail
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     5
from django.db import IntegrityError
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
     6
62
b7e47cc39342 renamed the project to ws_app and modified imports accordingly .
nishanth
parents: 59
diff changeset
     7
from ws_app.reg.models import Profile, Event
b7e47cc39342 renamed the project to ws_app and modified imports accordingly .
nishanth
parents: 59
diff changeset
     8
from ws_app.reg.utils import gen_key
2
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
81
a76987f54dac changed url in reg events.
nishanth
parents: 62
diff changeset
    44
    activation_link = "http://fossee.in/workshop/registration/activate/%s"%user.get_profile().activation_key
58
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    45
    subject = "Activate your account"
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    46
    message = """
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    47
    Dear %s,
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    48
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    49
    Thank you for registering at fossee.in.
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    50
    Your are just a step away from completeing your registration.
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    51
    Please click on the link below or open the url in your browser to activate your account.
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    52
    %s
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    53
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    54
    Please mail your queries and complaints to admin@fossee.in.
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    55
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    56
    If you have not registered at fossee, please ignore this mail.
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    57
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    58
    Regards,
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    59
    FOSSEE Team
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    60
    """%(user.get_full_name(), activation_link)
84
bb6a1bd4b3f8 removed print statement since mod wsgi does not allow printing
nishanth
parents: 81
diff changeset
    61
58
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    62
    send_mail(subject, message, "admin@fossee.in", [user.email])
a26c82f593f0 implemented send activation. have to test it and do the same for change password.
nishanth
parents: 46
diff changeset
    63
43
757d1da69255 added venue field in event model and corresponding forms and views.
nishanth
parents: 23
diff changeset
    64
def create_event(title, description, start_date, stop_date, venue, created_by=None):
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    65
    """ make an event and save it.
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    66
    """
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    67
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    68
    new_event = Event()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    69
    new_event.title = title
8
e2699e042129 now a user can create an event if he is a staff
nishanth
parents: 7
diff changeset
    70
    new_event.description = description
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    71
    new_event.start_date = start_date
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    72
    new_event.stop_date = stop_date
43
757d1da69255 added venue field in event model and corresponding forms and views.
nishanth
parents: 23
diff changeset
    73
    new_event.venue = venue
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    74
    new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    75
    if created_by:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    76
        new_event.organizers.add(created_by)
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    77
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    78
    while True:
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    79
        try:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    80
            new_event.key = gen_key(10)
2
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    81
            new_event.save()
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    82
            return new_event
c11afa8623f7 incorporated gen_key .
nishanth
parents:
diff changeset
    83
        except IntegrityError:
6
057498d12450 users can now register but still there is no concept of activation e-mail .
nishanth
parents: 2
diff changeset
    84
            pass
7
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    85
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    86
def activate_user(user):
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    87
    """ mark the is_active flag as true.
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    88
    """
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    89
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    90
    user.is_active = True
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    91
    user.save()
af9ab5ad2786 fixed a bug in registration .
nishanth
parents: 6
diff changeset
    92
    return user
9
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    93
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    94
def reset_password(user):
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    95
    """ get a key and set it as password.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    96
    for now, print the key.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    97
    later on add the send mail function.
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    98
    """
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
    99
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   100
    new_password = gen_key(10)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   101
    user.set_password(new_password)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   102
    user.save()
98
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   103
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   104
    subject = "Password on FOSSEE registration"
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   105
    message = """
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   106
    Dear %s,
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   107
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   108
    Your new password is "%s".
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   109
    This password can be changed using the "change password" link available in "My Profile" page after you login.
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   110
 
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   111
    Please mail your queries and complaints to admin@fossee.in.
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   112
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   113
    If you have not registered at fossee, please ignore this mail.
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   114
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   115
    Regards,
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   116
    FOSSEE Team
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   117
    """%(user.get_full_name(), new_password)
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   118
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   119
    send_mail(subject, message, "admin@fossee.in", [user.email])
1af134a1e53d now an email will be sent to user after he resets his password.
nishanth
parents: 84
diff changeset
   120
9
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   121
    return new_password
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   122
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   123
def change_password(user, new_password):
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   124
    """ 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
   125
    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
   126
    """
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   127
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   128
    user.set_password(new_password)
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   129
    user.save()
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   130
17
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   131
def update_profile(user, properties):
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   132
    """ properties must be a dictionary that contains all the attributes that
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   133
    we take while a new user registers.
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   134
    """
9
e29ecb7819e7 password change and password reset have been done.
nishanth
parents: 8
diff changeset
   135
17
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   136
    profile = user.get_profile()
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   137
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   138
    user.first_name = properties['first_name']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   139
    user.last_name = properties['last_name']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   140
    user.save()
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   141
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   142
    profile.gender = properties['gender']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   143
    profile.profession = properties['profession']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   144
    profile.affiliated_to = properties['affiliated_to']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   145
    profile.interests = properties['interests']
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   146
    profile.save()
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   147
125b6fc8f20b users can view and edit profile .
nishanth
parents: 9
diff changeset
   148