|
1 from django.contrib.auth.models import User |
|
2 from pytask.taskapp.models import Profile, Task, Comment, Credit |
|
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 user = User(username=username, email=email) |
|
30 user.set_password(password) |
|
31 user.save() |
|
32 properties = {'dob':dob, 'gender':gender} |
|
33 user_profile = Profile(user=user) |
|
34 updateProfile(user_profile, properties) |
|
35 return user |
|
36 |
|
37 def createSuUser(username,email,password,**properties): |
|
38 """ create user using createUser method and set the is_superuser flag """ |
|
39 |
|
40 su_user = createUser(username,email,password,**properties) |
|
41 su_user.is_staff = True |
|
42 su_user.is_superuser = True |
|
43 su_user.save() |