Statistics
| Branch: | Tag: | Revision:

root / logic / users.py @ 60d6a13b

History | View | Annotate | Download (1.8 kB)

1 420f2c20 Georgios Gousios
#
2 420f2c20 Georgios Gousios
# Business Logic for working with users
3 420f2c20 Georgios Gousios
#
4 420f2c20 Georgios Gousios
# Copyright 2010 Greek Research and Technology Network
5 420f2c20 Georgios Gousios
#
6 c9fdfa27 Georgios Gousios
from django.conf import settings
7 420f2c20 Georgios Gousios
8 420f2c20 Georgios Gousios
from synnefo.db.models import SynnefoUser
9 420f2c20 Georgios Gousios
from django.db import transaction
10 420f2c20 Georgios Gousios
import hashlib
11 938e89ab Georgios Gousios
import time
12 b8033991 Georgios Gousios
import string
13 c9fdfa27 Georgios Gousios
from datetime import datetime, timedelta
14 420f2c20 Georgios Gousios
15 420f2c20 Georgios Gousios
@transaction.commit_on_success
16 76cc889c Georgios Gousios
def _register_user(f, u, unq, t):
17 76cc889c Georgios Gousios
    user = SynnefoUser()
18 76cc889c Georgios Gousios
    user.realname = f
19 76cc889c Georgios Gousios
    user.name = u
20 76cc889c Georgios Gousios
    user.uniq = unq
21 76cc889c Georgios Gousios
    user.type = t
22 76cc889c Georgios Gousios
    user.credit = 10 #TODO: Fix this when we have a per group policy
23 420f2c20 Georgios Gousios
    user.save()
24 938e89ab Georgios Gousios
    create_auth_token(user)
25 420f2c20 Georgios Gousios
26 84ae2c4c Georgios Gousios
def create_uname(fullname):
27 84ae2c4c Georgios Gousios
    fullname = fullname.strip()
28 84ae2c4c Georgios Gousios
    uname = None
29 84ae2c4c Georgios Gousios
30 84ae2c4c Georgios Gousios
    if fullname.find(' ') is not -1:
31 b8033991 Georgios Gousios
        (name, surname) = (fullname.split(' ')[0], string.join(fullname.split(' ')[-1:], ''))
32 b8033991 Georgios Gousios
        uname = "%s%s" % (string.join(surname[0:7],''), name[0])
33 b8033991 Georgios Gousios
        uname = uname.lower()
34 84ae2c4c Georgios Gousios
    else:
35 84ae2c4c Georgios Gousios
        uname = fullname[0:7].lower()
36 84ae2c4c Georgios Gousios
37 84ae2c4c Georgios Gousios
    return uname
38 84ae2c4c Georgios Gousios
39 76cc889c Georgios Gousios
@transaction.commit_on_success
40 76cc889c Georgios Gousios
def delete_user(user):
41 76cc889c Georgios Gousios
    if user is not None:
42 76cc889c Georgios Gousios
        user.delete()
43 76cc889c Georgios Gousios
44 420f2c20 Georgios Gousios
def register_student(fullname, username, uniqid):
45 420f2c20 Georgios Gousios
    _register_user(fullname, username, uniqid, 'STUDENT')
46 420f2c20 Georgios Gousios
47 420f2c20 Georgios Gousios
def register_professor(fullname, username, uniqid):
48 420f2c20 Georgios Gousios
    _register_user(fullname, username, uniqid, 'PROFESSOR')
49 420f2c20 Georgios Gousios
50 84ae2c4c Georgios Gousios
def register_user(fullname, email):
51 84ae2c4c Georgios Gousios
    uname = create_uname (fullname)
52 84ae2c4c Georgios Gousios
    _register_user(fullname, uname, email, 'USER')
53 84ae2c4c Georgios Gousios
54 938e89ab Georgios Gousios
@transaction.commit_on_success
55 420f2c20 Georgios Gousios
def create_auth_token(user):
56 76cc889c Georgios Gousios
    md5 = hashlib.md5()
57 76cc889c Georgios Gousios
    md5.update(user.uniq)
58 b8033991 Georgios Gousios
    md5.update(user.name.encode('ascii', 'ignore'))
59 1896d262 Georgios Gousios
    md5.update(time.asctime())
60 420f2c20 Georgios Gousios
61 938e89ab Georgios Gousios
    user.auth_token = md5.hexdigest()
62 1896d262 Georgios Gousios
    user.auth_token_created = datetime.now()
63 c9fdfa27 Georgios Gousios
    user.auth_token_expires = user.auth_token_created + \
64 c9fdfa27 Georgios Gousios
                              timedelta(hours=settings.AUTH_TOKEN_DURATION)
65 938e89ab Georgios Gousios
    user.save()
66 462c7e47 Georgios Gousios
67 420f2c20 Georgios Gousios
#def login(username, password):