taskapp/events/user.py
author anoop
Mon, 08 Mar 2010 13:37:26 +0530
changeset 207 2de52334fe6c
parent 153 925af1b4ee65
permissions -rw-r--r--
changed the MEDIA_URL in settings.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     1
from django.contrib.auth.models import User
153
925af1b4ee65 ditchaxed credit model.
nishanth
parents: 107
diff changeset
     2
from pytask.taskapp.models import Profile, Task, Comment
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     3
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     4
""" A collection of helper methods. note that there is no validation done here.
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     5
we take care of validation and others checks in methods that invoke these methods.
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     6
"""
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     7
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     8
def updateProfile(user_profile, properties):
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     9
    """ updates the given properties in the profile for a user. 
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    10
    args:
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    11
        user_profile : a profile object
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    12
        properties : a dictionary with attributes to set as keys and corresponding values
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    13
    """
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    14
    
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    15
    for attr,value in properties.items():
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    16
        user_profile.__setattr__(attr,value)
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    17
    user_profile.save()
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    18
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    19
def createUser(username,email,password,dob,gender):
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    20
    """ create a user and create a profile and update its properties 
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    21
    args:
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    22
        username : a username that does not exist
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    23
        email : a valid email
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    24
        password : a password
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    25
        dob : a date object
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    26
        gender : u'M'/u'F' 
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    27
    """
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    28
31
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    29
    try:
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    30
        user = User.objects.get(username=username)
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    31
        return user
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    32
    except:        
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    33
        user = User(username=username, email=email)
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    34
        user.set_password(password)
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    35
        user.save()
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    36
        properties = {'dob':dob, 'gender':gender}
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    37
        user_profile = Profile(user=user)
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    38
        updateProfile(user_profile, properties)
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    39
        return user
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    40
    
30
055dc4c1db57 fixed a bug in createSuUser method in events/user.py .
nishanth
parents: 12
diff changeset
    41
def createSuUser(username,email,password,dob,gender):
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    42
    """ create user using createUser method and set the is_superuser flag """
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    43
    
30
055dc4c1db57 fixed a bug in createSuUser method in events/user.py .
nishanth
parents: 12
diff changeset
    44
    su_user = createUser(username,email,password,dob,gender)
12
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    45
    su_user.is_staff = True
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    46
    su_user.is_superuser = True
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
    47
    su_user.save()
31
988d4994578f modified createUser method in events/user.py .
nishanth
parents: 30
diff changeset
    48
    return su_user
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    49
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    50
def changeRole(role, user):
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    51
    """ change the status of user to role.
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    52
    """
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    53
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    54
    user_profile = user.get_profile()
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    55
    user_profile.rights = role
4903b4973fc8 completed the process_request part.
nishanth
parents: 31
diff changeset
    56
    user_profile.save()