root / astakos / im / models.py @ 890b0eaf
History | View | Annotate | Download (5.4 kB)
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 logging |
35 |
import hashlib |
36 |
|
37 |
from time import asctime |
38 |
from datetime import datetime, timedelta |
39 |
from base64 import b64encode |
40 |
|
41 |
from django.conf import settings |
42 |
from django.db import models |
43 |
from django.contrib.auth.models import User, UserManager |
44 |
|
45 |
from astakos.im.interface import get_quota, set_quota |
46 |
|
47 |
class AstakosUser(User): |
48 |
"""
|
49 |
Extends ``django.contrib.auth.models.User`` by defining additional fields.
|
50 |
"""
|
51 |
# Use UserManager to get the create_user method, etc.
|
52 |
objects = UserManager() |
53 |
|
54 |
affiliation = models.CharField('Affiliation', max_length=255, default='') |
55 |
provider = models.CharField('Provider', max_length=255, default='') |
56 |
|
57 |
#for invitations
|
58 |
level = models.IntegerField('Inviter level', default=4) |
59 |
invitations = models.IntegerField('Invitations left', default=0) |
60 |
|
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) |
65 |
|
66 |
updated = models.DateTimeField('Update date')
|
67 |
is_verified = models.BooleanField('Is verified?', default=False) |
68 |
|
69 |
@property
|
70 |
def realname(self): |
71 |
return '%s %s' %(self.first_name, self.last_name) |
72 |
|
73 |
@realname.setter
|
74 |
def realname(self, value): |
75 |
parts = value.split(' ')
|
76 |
if len(parts) == 2: |
77 |
self.first_name = parts[0] |
78 |
self.last_name = parts[1] |
79 |
else:
|
80 |
self.last_name = parts[0] |
81 |
|
82 |
@property
|
83 |
def quota(self): |
84 |
return get_quota(self.username) |
85 |
|
86 |
@quota.setter
|
87 |
def quota(self, value): |
88 |
set_quota(self.username, value)
|
89 |
|
90 |
@property
|
91 |
def invitation(self): |
92 |
try:
|
93 |
return Invitation.objects.get(username=self.username) |
94 |
except Invitation.DoesNotExist:
|
95 |
return None |
96 |
|
97 |
def save(self, update_timestamps=True, **kwargs): |
98 |
if update_timestamps:
|
99 |
if not self.id: |
100 |
self.date_joined = datetime.now()
|
101 |
self.updated = datetime.now()
|
102 |
|
103 |
super(AstakosUser, self).save(**kwargs) |
104 |
|
105 |
#invitation consume
|
106 |
if self.invitation and not self.invitation.is_consumed: |
107 |
self.invitation.consume()
|
108 |
|
109 |
def renew_token(self): |
110 |
md5 = hashlib.md5() |
111 |
md5.update(self.username)
|
112 |
md5.update(self.realname.encode('ascii', 'ignore')) |
113 |
md5.update(asctime()) |
114 |
|
115 |
self.auth_token = b64encode(md5.digest())
|
116 |
self.auth_token_created = datetime.now()
|
117 |
self.auth_token_expires = self.auth_token_created + \ |
118 |
timedelta(hours=settings.AUTH_TOKEN_DURATION) |
119 |
|
120 |
def __unicode__(self): |
121 |
return self.username |
122 |
|
123 |
class Invitation(models.Model): |
124 |
"""
|
125 |
Model for registring invitations
|
126 |
"""
|
127 |
inviter = models.ForeignKey(AstakosUser, related_name='invitations_sent',
|
128 |
null=True)
|
129 |
realname = models.CharField('Real name', max_length=255) |
130 |
username = models.CharField('Unique ID', max_length=255) |
131 |
code = models.BigIntegerField('Invitation code', db_index=True) |
132 |
#obsolete: we keep it just for transfering the data
|
133 |
is_accepted = models.BooleanField('Accepted?', default=False) |
134 |
is_consumed = models.BooleanField('Consumed?', default=False) |
135 |
created = models.DateTimeField('Creation date', auto_now_add=True) |
136 |
#obsolete: we keep it just for transfering the data
|
137 |
accepted = models.DateTimeField('Acceptance date', null=True, blank=True) |
138 |
consumed = models.DateTimeField('Consumption date', null=True, blank=True) |
139 |
|
140 |
def consume(self): |
141 |
self.is_consumed = True |
142 |
self.consumed = datetime.now()
|
143 |
self.save()
|
144 |
|
145 |
def __unicode__(self): |
146 |
return '%s -> %s [%d]' % (self.inviter, self.username, self.code) |