summaryrefslogtreecommitdiff
path: root/allotter/management/commands/loadusers.py
blob: 99fd075c735f6d4a3df00295085fa90a5cff9877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from optparse import make_option
from datetime import datetime
from csv import DictReader
from django.core.management.base import BaseCommand
from allotter.models import Exam, Application, User, Profile

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--usdf',metavar='User details file name', type=str),
    )
    help = "Give the filename of the csv files that has all the details of the users"
    
    def handle(self, *args, **options):
        
        clean_users()
        
        load_users(options)
        
        self.stdout.write('Done\n')
        

def clean_users():
    """Removes all the objects from the database, required as if not done there might be a case of multiple entries"""
    User.objects.filter(is_superuser=False).delete()

def load_users(options):
    """Load option code and option name from the given csv file. The file should 
    declare a list of "exam_code,option_code,option,code".
    """
    userDetailsFileName=options.get('usdf')
    try:
        userDetailsFile = open(userDetailsFileName, 'rb')
    except IOError as (errno,strerror):
        print "I/O error({0}): {1}".format(errno, strerror)
    
        
    userReader = DictReader(userDetailsFile, delimiter=":")

    
    for data in userReader:
        appno = data['AppNo.']
        regno = data['Reg.No.']
        new_user = User.objects.create_user(regno, password=appno, email="")
        application = Application(user=new_user)
        application.np = int(data['NP'])
        if data['P1'].strip():
            application.first_paper = Exam.objects.get(exam_code=data['P1'])
            try:
                application.second_paper = Exam.objects.get(exam_code=data['P2'])
            except:
                pass
        else:
            application.first_paper = Exam.objects.get(exam_code=data['P2'])
        
            
        application.nat = data['Nat']
        application.gender = data['Gdr']
        application.cent = data['Cent']
        application.cgy = data['Cgy']
        application.save()
        dob = datetime.strptime(data['DOB'], "%d/%m/%y")
        new_profile = Profile(user=new_user, application=application)
        new_profile.dob = dob
        new_profile.save()
        print "Added user with {0} and {1} with dob as {2}".format(appno,regno,dob)