1 # Copyright 2011-2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
37 from time import asctime
38 from datetime import datetime, timedelta
39 from base64 import b64encode
41 from django.db import models
42 from django.contrib.auth.models import User, UserManager
44 from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION
46 class AstakosUser(User):
48 Extends ``django.contrib.auth.models.User`` by defining additional fields.
50 # Use UserManager to get the create_user method, etc.
51 objects = UserManager()
53 affiliation = models.CharField('Affiliation', max_length=255, blank=True)
54 provider = models.CharField('Provider', max_length=255, blank=True)
57 user_level = DEFAULT_USER_LEVEL
58 level = models.IntegerField('Inviter level', default=user_level)
59 invitations = models.IntegerField('Invitations left', default=INVITATIONS_PER_LEVEL.get(user_level, 0))
61 auth_token = models.CharField('Authentication Token', max_length=32,
62 null=True, blank=True)
63 auth_token_created = models.DateTimeField('Token creation date', null=True)
64 auth_token_expires = models.DateTimeField('Token expiration date', null=True)
66 updated = models.DateTimeField('Update date')
67 is_verified = models.BooleanField('Is verified?', default=False)
69 # ex. screen_name for twitter, eppn for shibboleth
70 third_party_identifier = models.CharField('Third-party identifier', max_length=255, null=True, blank=True)
72 email_verified = models.BooleanField('Email verified?', default=False)
76 return '%s %s' %(self.first_name, self.last_name)
79 def realname(self, value):
80 parts = value.split(' ')
82 self.first_name = parts[0]
83 self.last_name = parts[1]
85 self.last_name = parts[0]
90 return Invitation.objects.get(username=self.email)
91 except Invitation.DoesNotExist:
94 def save(self, update_timestamps=True, **kwargs):
98 while not self.username:
99 username = uuid.uuid4().hex[:30]
101 AstakosUser.objects.get(username = username)
102 except AstakosUser.DoesNotExist, e:
103 self.username = username
104 self.is_active = False
105 if not self.provider:
106 self.provider = 'local'
107 self.date_joined = datetime.now()
108 self.updated = datetime.now()
109 super(AstakosUser, self).save(**kwargs)
111 def renew_token(self):
113 md5.update(self.username)
114 md5.update(self.realname.encode('ascii', 'ignore'))
115 md5.update(asctime())
117 self.auth_token = b64encode(md5.digest())
118 self.auth_token_created = datetime.now()
119 self.auth_token_expires = self.auth_token_created + \
120 timedelta(hours=AUTH_TOKEN_DURATION)
122 def __unicode__(self):
125 class Invitation(models.Model):
127 Model for registring invitations
129 inviter = models.ForeignKey(AstakosUser, related_name='invitations_sent',
131 realname = models.CharField('Real name', max_length=255)
132 username = models.CharField('Unique ID', max_length=255, unique=True)
133 code = models.BigIntegerField('Invitation code', db_index=True)
134 #obsolete: we keep it just for transfering the data
135 is_accepted = models.BooleanField('Accepted?', default=False)
136 is_consumed = models.BooleanField('Consumed?', default=False)
137 created = models.DateTimeField('Creation date', auto_now_add=True)
138 #obsolete: we keep it just for transfering the data
139 accepted = models.DateTimeField('Acceptance date', null=True, blank=True)
140 consumed = models.DateTimeField('Consumption date', null=True, blank=True)
143 self.is_consumed = True
144 self.consumed = datetime.now()
147 def __unicode__(self):
148 return '%s -> %s [%d]' % (self.inviter, self.username, self.code)