Merge changes from master branch. Fix quota updates. Clean up util. Create token...
[pithos] / pithos / im / models.py
1 # Copyright 2011 GRNET S.A. All rights reserved.
2
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10
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.
15
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.
28
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.
33
34 import hashlib
35
36 from time import asctime
37 from datetime import datetime, timedelta
38 from base64 import b64encode
39
40 from django.conf import settings
41 from django.db import models
42
43
44 class User(models.Model):
45     
46     ACCOUNT_STATE = (
47         ('ACTIVE', 'Active'),
48         ('DELETED', 'Deleted'),
49         ('SUSPENDED', 'Suspended')
50     )
51     
52     uniq = models.CharField('Unique ID', max_length=255, null=True)
53     
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')
58     
59     # Lose these...
60     quota = models.BigIntegerField('Storage Limit', default=settings.DEFAULT_QUOTA)
61     max_invitations = models.IntegerField('Max number of invitations', null=True)
62     
63     is_admin = models.BooleanField('Admin', default=False)
64     
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')
68     
69     created = models.DateTimeField('Time of creation')
70     updated = models.DateTimeField('Time of last update')
71     
72     def save(self, update_timestamps=True):
73         if update_timestamps:
74             if not self.id:
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()
80     
81     def renew_token(self):
82         md5 = hashlib.md5()
83         md5.update(self.uniq)
84         md5.update(self.realname.encode('ascii', 'ignore'))
85         md5.update(asctime())
86         
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)
91     
92     class Meta:
93         verbose_name = u'User'
94     
95     def __unicode__(self):
96         return self.uniq
97
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)
103     
104     created = models.DateTimeField(auto_now_add=True)
105     updated = models.DateTimeField(auto_now=True)
106
107     class Meta:
108         verbose_name = u'Invitation'
109
110     def __unicode__(self):
111         return "From: %s, To: %s" % (self.source, self.target)