1 # Copyright 2011 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.
36 from time import asctime
37 from datetime import datetime, timedelta
38 from base64 import b64encode
40 from django.conf import settings
41 from django.db import models
44 class User(models.Model):
48 ('DELETED', 'Deleted'),
49 ('SUSPENDED', 'Suspended')
52 uniq = models.CharField('Unique ID', max_length=255, null=True)
54 realname = models.CharField('Real Name', max_length=255, default='')
55 email = models.CharField('Email', max_length=255, default='')
56 affiliation = models.CharField('Affiliation', max_length=255, default='')
57 state = models.CharField('Account state', choices=ACCOUNT_STATE, max_length=16, default='ACTIVE')
60 quota = models.BigIntegerField('Storage Limit', default=settings.DEFAULT_QUOTA)
61 max_invitations = models.IntegerField('Max number of invitations', null=True)
63 is_admin = models.BooleanField('Admin', default=False)
65 auth_token = models.CharField('Authentication Token', max_length=32, null=True)
66 auth_token_created = models.DateTimeField('Time of auth token creation')
67 auth_token_expires = models.DateTimeField('Time of auth token expiration')
69 created = models.DateTimeField('Time of creation')
70 updated = models.DateTimeField('Time of last update')
72 def save(self, update_timestamps=True):
75 self.created = datetime.now()
76 #self.auth_token_created = datetime.now()
77 #self.auth_token_expires = datetime.now()
78 self.updated = datetime.now()
79 super(User, self).save()
81 def renew_token(self):
84 md5.update(self.realname.encode('ascii', 'ignore'))
87 self.auth_token = b64encode(md5.digest())
88 self.auth_token_created = datetime.now()
89 self.auth_token_expires = self.auth_token_created + \
90 timedelta(hours=settings.AUTH_TOKEN_DURATION)
93 verbose_name = u'User'
95 def __unicode__(self):
98 class Invitation(models.Model):
99 source = models.ForeignKey(User, related_name="source")
100 target = models.ForeignKey(User, related_name="target")
101 accepted = models.BooleanField('Is the invitation accepted?', default=False)
102 level = models.IntegerField('Invitation depth level', null=True)
104 created = models.DateTimeField(auto_now_add=True)
105 updated = models.DateTimeField(auto_now=True)
108 verbose_name = u'Invitation'
110 def __unicode__(self):
111 return "From: %s, To: %s" % (self.source, self.target)