Revision 27e26a41 snf-astakos-app/astakos/im/util.py

b/snf-astakos-app/astakos/im/util.py
40 40
from functools import wraps
41 41

  
42 42
from datetime import tzinfo, timedelta
43
from django.http import HttpResponse, urlencode
43
from django.http import HttpResponse, HttpResponseBadRequest, urlencode
44 44
from django.template import RequestContext
45 45
from django.contrib.sites.models import Site
46 46
from django.utils.translation import ugettext as _
47 47
from django.contrib.auth import login, authenticate
48 48
from django.core.urlresolvers import reverse
49
from django.core.exceptions import ValidationError
49 50

  
50 51
from astakos.im.models import AstakosUser, Invitation, ApprovalTerms
51 52
from astakos.im.settings import INVITATIONS_PER_LEVEL, COOKIE_NAME, COOKIE_DOMAIN, COOKIE_SECURE, FORCE_PROFILE_UPDATE
......
70 71
def epoch(datetime):
71 72
    return int(time.mktime(datetime.timetuple())*1000)
72 73

  
73
def get_or_create_user(email, realname='', first_name='', last_name='', affiliation='', level=0, provider='local', password=''):
74
    """Find or register a user into the internal database
75
       and issue a token for subsequent requests.
76
    """
77
    user, created = AstakosUser.objects.get_or_create(email=email,
78
        defaults={
79
            'password':password,
80
            'affiliation':affiliation,
81
            'level':level,
82
            'invitations':INVITATIONS_PER_LEVEL.get(level, 0),
83
            'provider':provider,
84
            'realname':realname,
85
            'first_name':first_name,
86
            'last_name':last_name
87
        })
88
    if created:
89
        user.renew_token()
90
        user.save()
91
        logger.info('Created user %s', user)
92
    
93
    return user
94

  
95 74
def get_context(request, extra_context={}, **kwargs):
96 75
    if not extra_context:
97 76
        extra_context = {}
......
131 110
    renew = renew or (user.auth_token_expires and user.auth_token_expires < datetime.datetime.now())
132 111
    if renew:
133 112
        user.renew_token()
134
        user.save()
113
        try:
114
            user.save()
115
        except ValidationError, e:
116
            return HttpResponseBadRequest(e) 
135 117
    
136 118
    if FORCE_PROFILE_UPDATE and not user.is_verified and not user.is_superuser:
137 119
        params = ''

Also available in: Unified diff