pytask/taskapp/events/user.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 06 Jan 2011 19:07:04 +0530
branchbuildout
changeset 227 3c8f3b0e5b00
permissions -rw-r--r--
Add support for buildout and move the files to the directory to support buildout structure.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
227
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
from django.contrib.auth.models import User
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
from pytask.taskapp.models import Profile, Task, Comment
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
""" A collection of helper methods. note that there is no validation done here.
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
we take care of validation and others checks in methods that invoke these methods.
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
"""
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
def updateProfile(user_profile, properties):
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
    """ updates the given properties in the profile for a user. 
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
    args:
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
        user_profile : a profile object
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
        properties : a dictionary with attributes to set as keys and corresponding values
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
    """
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
    
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
    for attr,value in properties.items():
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
        user_profile.__setattr__(attr,value)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
    user_profile.save()
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
def createUser(username,email,password,dob,gender):
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
    """ create a user and create a profile and update its properties 
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
    args:
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
        username : a username that does not exist
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
        email : a valid email
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
        password : a password
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
        dob : a date object
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
        gender : u'M'/u'F' 
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
    """
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
    try:
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
        user = User.objects.get(username=username)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
        return user
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
    except:        
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
        user = User(username=username, email=email)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
        user.set_password(password)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
        user.save()
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
        properties = {'dob':dob, 'gender':gender}
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
        user_profile = Profile(user=user)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
        updateProfile(user_profile, properties)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
        return user
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
    
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
def createSuUser(username,email,password,dob,gender):
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
    """ create user using createUser method and set the is_superuser flag """
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
    
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
    su_user = createUser(username,email,password,dob,gender)
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
    su_user.is_staff = True
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
    su_user.is_superuser = True
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
    su_user.save()
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
    return su_user
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
def changeRole(role, user):
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
    """ change the status of user to role.
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
    """
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
    user_profile = user.get_profile()
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
    user_profile.rights = role
3c8f3b0e5b00 Add support for buildout and move the files to the directory to support buildout structure.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
    user_profile.save()