taskapp/events/user.py
author nishanth
Fri, 05 Feb 2010 15:40:47 +0530
changeset 31 988d4994578f
parent 30 055dc4c1db57
child 107 4903b4973fc8
permissions -rw-r--r--
modified createUser method in events/user.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
a93eebabfeb1 created forms, views, templates, actions for home_page, browse_task.
nishanth
parents:
diff changeset
     2
from pytask.taskapp.models import Profile, Task, Comment, Credit
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