root / snf-astakos-app / astakos / im / models.py @ 660c7a4f
History | View | Annotate | Download (6.8 kB)
1 | aba1e498 | Antony Chazapis | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 64cd4730 | Antony Chazapis | #
|
3 | 64cd4730 | Antony Chazapis | # Redistribution and use in source and binary forms, with or
|
4 | 64cd4730 | Antony Chazapis | # without modification, are permitted provided that the following
|
5 | 64cd4730 | Antony Chazapis | # conditions are met:
|
6 | 64cd4730 | Antony Chazapis | #
|
7 | 64cd4730 | Antony Chazapis | # 1. Redistributions of source code must retain the above
|
8 | 64cd4730 | Antony Chazapis | # copyright notice, this list of conditions and the following
|
9 | 64cd4730 | Antony Chazapis | # disclaimer.
|
10 | 64cd4730 | Antony Chazapis | #
|
11 | 64cd4730 | Antony Chazapis | # 2. Redistributions in binary form must reproduce the above
|
12 | 64cd4730 | Antony Chazapis | # copyright notice, this list of conditions and the following
|
13 | 64cd4730 | Antony Chazapis | # disclaimer in the documentation and/or other materials
|
14 | 64cd4730 | Antony Chazapis | # provided with the distribution.
|
15 | 64cd4730 | Antony Chazapis | #
|
16 | 64cd4730 | Antony Chazapis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 64cd4730 | Antony Chazapis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 64cd4730 | Antony Chazapis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 64cd4730 | Antony Chazapis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 64cd4730 | Antony Chazapis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 64cd4730 | Antony Chazapis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 64cd4730 | Antony Chazapis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 64cd4730 | Antony Chazapis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 64cd4730 | Antony Chazapis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 64cd4730 | Antony Chazapis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 64cd4730 | Antony Chazapis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 64cd4730 | Antony Chazapis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 64cd4730 | Antony Chazapis | #
|
29 | 64cd4730 | Antony Chazapis | # The views and conclusions contained in the software and
|
30 | 64cd4730 | Antony Chazapis | # documentation are those of the authors and should not be
|
31 | 64cd4730 | Antony Chazapis | # interpreted as representing official policies, either expressed
|
32 | 64cd4730 | Antony Chazapis | # or implied, of GRNET S.A.
|
33 | 64cd4730 | Antony Chazapis | |
34 | 64cd4730 | Antony Chazapis | import hashlib |
35 | 15efc749 | Sofia Papagiannaki | import uuid |
36 | 64cd4730 | Antony Chazapis | |
37 | 64cd4730 | Antony Chazapis | from time import asctime |
38 | 64cd4730 | Antony Chazapis | from datetime import datetime, timedelta |
39 | 64cd4730 | Antony Chazapis | from base64 import b64encode |
40 | 64cd4730 | Antony Chazapis | |
41 | 64cd4730 | Antony Chazapis | from django.db import models |
42 | 0905ccd2 | Sofia Papagiannaki | from django.contrib.auth.models import User, UserManager |
43 | 64cd4730 | Antony Chazapis | |
44 | 660c7a4f | Sofia Papagiannaki | from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION, BILLING_FIELDS, QUEUE_EXCHANGE |
45 | 9c01d9d1 | Sofia Papagiannaki | from synnefo.lib.queue import exchange_connect, exchange_send, exchange_close, Receipt |
46 | 9c01d9d1 | Sofia Papagiannaki | |
47 | 9c01d9d1 | Sofia Papagiannaki | QUEUE_CLIENT_ID = 3 # Astakos. |
48 | 64cd4730 | Antony Chazapis | |
49 | 0905ccd2 | Sofia Papagiannaki | class AstakosUser(User): |
50 | 890b0eaf | Sofia Papagiannaki | """
|
51 | 890b0eaf | Sofia Papagiannaki | Extends ``django.contrib.auth.models.User`` by defining additional fields.
|
52 | 890b0eaf | Sofia Papagiannaki | """
|
53 | 0905ccd2 | Sofia Papagiannaki | # Use UserManager to get the create_user method, etc.
|
54 | 0905ccd2 | Sofia Papagiannaki | objects = UserManager() |
55 | 64cd4730 | Antony Chazapis | |
56 | 5ed6816e | Sofia Papagiannaki | affiliation = models.CharField('Affiliation', max_length=255, blank=True) |
57 | 5ed6816e | Sofia Papagiannaki | provider = models.CharField('Provider', max_length=255, blank=True) |
58 | 64cd4730 | Antony Chazapis | |
59 | 64cd4730 | Antony Chazapis | #for invitations
|
60 | 92defad4 | Sofia Papagiannaki | user_level = DEFAULT_USER_LEVEL |
61 | a196eb7e | Sofia Papagiannaki | level = models.IntegerField('Inviter level', default=user_level)
|
62 | 92defad4 | Sofia Papagiannaki | invitations = models.IntegerField('Invitations left', default=INVITATIONS_PER_LEVEL[user_level])
|
63 | 64cd4730 | Antony Chazapis | |
64 | 64cd4730 | Antony Chazapis | auth_token = models.CharField('Authentication Token', max_length=32, |
65 | 0905ccd2 | Sofia Papagiannaki | null=True, blank=True) |
66 | 0905ccd2 | Sofia Papagiannaki | auth_token_created = models.DateTimeField('Token creation date', null=True) |
67 | 0905ccd2 | Sofia Papagiannaki | auth_token_expires = models.DateTimeField('Token expiration date', null=True) |
68 | 64cd4730 | Antony Chazapis | |
69 | 64cd4730 | Antony Chazapis | updated = models.DateTimeField('Update date')
|
70 | 890b0eaf | Sofia Papagiannaki | is_verified = models.BooleanField('Is verified?', default=False) |
71 | 64cd4730 | Antony Chazapis | |
72 | 15efc749 | Sofia Papagiannaki | # ex. screen_name for twitter, eppn for shibboleth
|
73 | 15efc749 | Sofia Papagiannaki | third_party_identifier = models.CharField('Third-party identifier', max_length=255, null=True, blank=True) |
74 | 15efc749 | Sofia Papagiannaki | |
75 | 0905ccd2 | Sofia Papagiannaki | @property
|
76 | 0905ccd2 | Sofia Papagiannaki | def realname(self): |
77 | 0905ccd2 | Sofia Papagiannaki | return '%s %s' %(self.first_name, self.last_name) |
78 | 64cd4730 | Antony Chazapis | |
79 | 0905ccd2 | Sofia Papagiannaki | @realname.setter
|
80 | 0905ccd2 | Sofia Papagiannaki | def realname(self, value): |
81 | 0905ccd2 | Sofia Papagiannaki | parts = value.split(' ')
|
82 | 0905ccd2 | Sofia Papagiannaki | if len(parts) == 2: |
83 | 0905ccd2 | Sofia Papagiannaki | self.first_name = parts[0] |
84 | 0905ccd2 | Sofia Papagiannaki | self.last_name = parts[1] |
85 | 0905ccd2 | Sofia Papagiannaki | else:
|
86 | 0905ccd2 | Sofia Papagiannaki | self.last_name = parts[0] |
87 | 64cd4730 | Antony Chazapis | |
88 | 64cd4730 | Antony Chazapis | @property
|
89 | 64cd4730 | Antony Chazapis | def invitation(self): |
90 | 64cd4730 | Antony Chazapis | try:
|
91 | 9fb8e808 | Sofia Papagiannaki | return Invitation.objects.get(username=self.email) |
92 | 64cd4730 | Antony Chazapis | except Invitation.DoesNotExist:
|
93 | 64cd4730 | Antony Chazapis | return None |
94 | 64cd4730 | Antony Chazapis | |
95 | 64cd4730 | Antony Chazapis | def save(self, update_timestamps=True, **kwargs): |
96 | 64cd4730 | Antony Chazapis | if update_timestamps:
|
97 | 64cd4730 | Antony Chazapis | if not self.id: |
98 | 0905ccd2 | Sofia Papagiannaki | self.date_joined = datetime.now()
|
99 | 64cd4730 | Antony Chazapis | self.updated = datetime.now()
|
100 | 9c01d9d1 | Sofia Papagiannaki | if not self.id: |
101 | 9c01d9d1 | Sofia Papagiannaki | # set username
|
102 | 9c01d9d1 | Sofia Papagiannaki | while not self.username: |
103 | 9c01d9d1 | Sofia Papagiannaki | username = uuid.uuid4().hex[:30]
|
104 | 9c01d9d1 | Sofia Papagiannaki | try:
|
105 | 9c01d9d1 | Sofia Papagiannaki | AstakosUser.objects.get(username = username) |
106 | 9c01d9d1 | Sofia Papagiannaki | except AstakosUser.DoesNotExist, e:
|
107 | 9c01d9d1 | Sofia Papagiannaki | self.username = username
|
108 | 9c01d9d1 | Sofia Papagiannaki | self.is_active = False |
109 | 9c01d9d1 | Sofia Papagiannaki | if not self.provider: |
110 | 9c01d9d1 | Sofia Papagiannaki | self.provider = 'local' |
111 | 9c01d9d1 | Sofia Papagiannaki | report_user_event(self)
|
112 | 0905ccd2 | Sofia Papagiannaki | super(AstakosUser, self).save(**kwargs) |
113 | 64cd4730 | Antony Chazapis | |
114 | 64cd4730 | Antony Chazapis | def renew_token(self): |
115 | 64cd4730 | Antony Chazapis | md5 = hashlib.md5() |
116 | 0905ccd2 | Sofia Papagiannaki | md5.update(self.username)
|
117 | 64cd4730 | Antony Chazapis | md5.update(self.realname.encode('ascii', 'ignore')) |
118 | 64cd4730 | Antony Chazapis | md5.update(asctime()) |
119 | 64cd4730 | Antony Chazapis | |
120 | 64cd4730 | Antony Chazapis | self.auth_token = b64encode(md5.digest())
|
121 | 64cd4730 | Antony Chazapis | self.auth_token_created = datetime.now()
|
122 | 64cd4730 | Antony Chazapis | self.auth_token_expires = self.auth_token_created + \ |
123 | 92defad4 | Sofia Papagiannaki | timedelta(hours=AUTH_TOKEN_DURATION) |
124 | 64cd4730 | Antony Chazapis | |
125 | 64cd4730 | Antony Chazapis | def __unicode__(self): |
126 | 0905ccd2 | Sofia Papagiannaki | return self.username |
127 | 64cd4730 | Antony Chazapis | |
128 | 64cd4730 | Antony Chazapis | class Invitation(models.Model): |
129 | 890b0eaf | Sofia Papagiannaki | """
|
130 | 890b0eaf | Sofia Papagiannaki | Model for registring invitations
|
131 | 890b0eaf | Sofia Papagiannaki | """
|
132 | 0905ccd2 | Sofia Papagiannaki | inviter = models.ForeignKey(AstakosUser, related_name='invitations_sent',
|
133 | 64cd4730 | Antony Chazapis | null=True)
|
134 | 64cd4730 | Antony Chazapis | realname = models.CharField('Real name', max_length=255) |
135 | 0905ccd2 | Sofia Papagiannaki | username = models.CharField('Unique ID', max_length=255) |
136 | 64cd4730 | Antony Chazapis | code = models.BigIntegerField('Invitation code', db_index=True) |
137 | 64cd4730 | Antony Chazapis | #obsolete: we keep it just for transfering the data
|
138 | 64cd4730 | Antony Chazapis | is_accepted = models.BooleanField('Accepted?', default=False) |
139 | 64cd4730 | Antony Chazapis | is_consumed = models.BooleanField('Consumed?', default=False) |
140 | 64cd4730 | Antony Chazapis | created = models.DateTimeField('Creation date', auto_now_add=True) |
141 | 64cd4730 | Antony Chazapis | #obsolete: we keep it just for transfering the data
|
142 | 64cd4730 | Antony Chazapis | accepted = models.DateTimeField('Acceptance date', null=True, blank=True) |
143 | 64cd4730 | Antony Chazapis | consumed = models.DateTimeField('Consumption date', null=True, blank=True) |
144 | 64cd4730 | Antony Chazapis | |
145 | 64cd4730 | Antony Chazapis | def consume(self): |
146 | 64cd4730 | Antony Chazapis | self.is_consumed = True |
147 | 64cd4730 | Antony Chazapis | self.consumed = datetime.now()
|
148 | 64cd4730 | Antony Chazapis | self.save()
|
149 | 64cd4730 | Antony Chazapis | |
150 | 64cd4730 | Antony Chazapis | def __unicode__(self): |
151 | 0905ccd2 | Sofia Papagiannaki | return '%s -> %s [%d]' % (self.inviter, self.username, self.code) |
152 | 9c01d9d1 | Sofia Papagiannaki | |
153 | 9c01d9d1 | Sofia Papagiannaki | def report_user_event(user): |
154 | 9c01d9d1 | Sofia Papagiannaki | def should_send(user): |
155 | 9c01d9d1 | Sofia Papagiannaki | # report event incase of new user instance
|
156 | 9c01d9d1 | Sofia Papagiannaki | # or if specific fields are modified
|
157 | 9c01d9d1 | Sofia Papagiannaki | if not user.id: |
158 | 9c01d9d1 | Sofia Papagiannaki | return True |
159 | 9c01d9d1 | Sofia Papagiannaki | db_instance = AstakosUser.objects.get(id = user.id) |
160 | 9c01d9d1 | Sofia Papagiannaki | for f in BILLING_FIELDS: |
161 | 9c01d9d1 | Sofia Papagiannaki | if (db_instance.__getattribute__(f) != user.__getattribute__(f)):
|
162 | 9c01d9d1 | Sofia Papagiannaki | return True |
163 | 9c01d9d1 | Sofia Papagiannaki | return False |
164 | 9c01d9d1 | Sofia Papagiannaki | |
165 | 660c7a4f | Sofia Papagiannaki | if QUEUE_EXCHANGE and should_send(user): |
166 | 9c01d9d1 | Sofia Papagiannaki | l = [[elem, str(user.__getattribute__(elem))] for elem in BILLING_FIELDS] |
167 | 9c01d9d1 | Sofia Papagiannaki | details = dict(l)
|
168 | 660c7a4f | Sofia Papagiannaki | details['eventType'] = 'create' if not user.id else 'modify' |
169 | 9c01d9d1 | Sofia Papagiannaki | body = Receipt(QUEUE_CLIENT_ID, user.email, '', 0, details).format() |
170 | 660c7a4f | Sofia Papagiannaki | conn = exchange_connect(QUEUE_EXCHANGE) |
171 | 660c7a4f | Sofia Papagiannaki | routing_key = QUEUE_EXCHANGE.replace('#', body['id']) |
172 | 9c01d9d1 | Sofia Papagiannaki | exchange_send(conn, routing_key, body) |
173 | 9c01d9d1 | Sofia Papagiannaki | exchange_close(conn) |