taskapp/events/user.py
branchbuildout
changeset 227 3c8f3b0e5b00
parent 214 679c7e237052
child 228 81994e525e69
equal deleted inserted replaced
214:679c7e237052 227:3c8f3b0e5b00
     1 from django.contrib.auth.models import User
       
     2 from pytask.taskapp.models import Profile, Task, Comment
       
     3 
       
     4 """ A collection of helper methods. note that there is no validation done here.
       
     5 we take care of validation and others checks in methods that invoke these methods.
       
     6 """
       
     7 
       
     8 def updateProfile(user_profile, properties):
       
     9     """ updates the given properties in the profile for a user. 
       
    10     args:
       
    11         user_profile : a profile object
       
    12         properties : a dictionary with attributes to set as keys and corresponding values
       
    13     """
       
    14     
       
    15     for attr,value in properties.items():
       
    16         user_profile.__setattr__(attr,value)
       
    17     user_profile.save()
       
    18 
       
    19 def createUser(username,email,password,dob,gender):
       
    20     """ create a user and create a profile and update its properties 
       
    21     args:
       
    22         username : a username that does not exist
       
    23         email : a valid email
       
    24         password : a password
       
    25         dob : a date object
       
    26         gender : u'M'/u'F' 
       
    27     """
       
    28 
       
    29     try:
       
    30         user = User.objects.get(username=username)
       
    31         return user
       
    32     except:        
       
    33         user = User(username=username, email=email)
       
    34         user.set_password(password)
       
    35         user.save()
       
    36         properties = {'dob':dob, 'gender':gender}
       
    37         user_profile = Profile(user=user)
       
    38         updateProfile(user_profile, properties)
       
    39         return user
       
    40     
       
    41 def createSuUser(username,email,password,dob,gender):
       
    42     """ create user using createUser method and set the is_superuser flag """
       
    43     
       
    44     su_user = createUser(username,email,password,dob,gender)
       
    45     su_user.is_staff = True
       
    46     su_user.is_superuser = True
       
    47     su_user.save()
       
    48     return su_user
       
    49 
       
    50 def changeRole(role, user):
       
    51     """ change the status of user to role.
       
    52     """
       
    53 
       
    54     user_profile = user.get_profile()
       
    55     user_profile.rights = role
       
    56     user_profile.save()