Revision 6c736ed7 snf-astakos-app/astakos/im/models.py

b/snf-astakos-app/astakos/im/models.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
43 43
from django.contrib.auth.models import User, UserManager
44 44

  
45 45
from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION, BILLING_FIELDS, QUEUE_CONNECTION
46
from astakos.im.queue.userevent import UserEvent
47
from synnefo.lib.queue import exchange_connect, exchange_send, exchange_close, Receipt
48 46

  
49 47
QUEUE_CLIENT_ID = 3 # Astakos.
50 48

  
......
54 52
    """
55 53
    # Use UserManager to get the create_user method, etc.
56 54
    objects = UserManager()
57
    
55

  
58 56
    affiliation = models.CharField('Affiliation', max_length=255, blank=True)
59 57
    provider = models.CharField('Provider', max_length=255, blank=True)
60
    
58

  
61 59
    #for invitations
62 60
    user_level = DEFAULT_USER_LEVEL
63 61
    level = models.IntegerField('Inviter level', default=user_level)
64 62
    invitations = models.IntegerField('Invitations left', default=INVITATIONS_PER_LEVEL.get(user_level, 0))
65
    
63

  
66 64
    auth_token = models.CharField('Authentication Token', max_length=32,
67 65
                                  null=True, blank=True)
68 66
    auth_token_created = models.DateTimeField('Token creation date', null=True)
69 67
    auth_token_expires = models.DateTimeField('Token expiration date', null=True)
70
    
68

  
71 69
    updated = models.DateTimeField('Update date')
72 70
    is_verified = models.BooleanField('Is verified?', default=False)
73
    
71

  
74 72
    # ex. screen_name for twitter, eppn for shibboleth
75 73
    third_party_identifier = models.CharField('Third-party identifier', max_length=255, null=True, blank=True)
76
    
74

  
77 75
    email_verified = models.BooleanField('Email verified?', default=False)
78
    
76

  
79 77
    has_credits = models.BooleanField('Has credits?', default=False)
80 78
    has_signed_terms = models.BooleanField('Agree with the terms?', default=False)
81 79
    date_signed_terms = models.DateTimeField('Signed terms date', null=True)
82
    
80

  
83 81
    @property
84 82
    def realname(self):
85 83
        return '%s %s' %(self.first_name, self.last_name)
86
    
84

  
87 85
    @realname.setter
88 86
    def realname(self, value):
89 87
        parts = value.split(' ')
......
92 90
            self.last_name = parts[1]
93 91
        else:
94 92
            self.last_name = parts[0]
95
    
93

  
96 94
    @property
97 95
    def invitation(self):
98 96
        try:
99 97
            return Invitation.objects.get(username=self.email)
100 98
        except Invitation.DoesNotExist:
101 99
            return None
102
    
100

  
103 101
    def save(self, update_timestamps=True, **kwargs):
104 102
        if update_timestamps:
105 103
            if not self.id:
......
118 116
                self.provider = 'local'
119 117
        report_user_event(self)
120 118
        super(AstakosUser, self).save(**kwargs)
121
    
119

  
122 120
    def renew_token(self):
123 121
        md5 = hashlib.md5()
124 122
        md5.update(self.username)
125 123
        md5.update(self.realname.encode('ascii', 'ignore'))
126 124
        md5.update(asctime())
127
        
125

  
128 126
        self.auth_token = b64encode(md5.digest())
129 127
        self.auth_token_created = datetime.now()
130 128
        self.auth_token_expires = self.auth_token_created + \
131 129
                                  timedelta(hours=AUTH_TOKEN_DURATION)
132
    
130

  
133 131
    def __unicode__(self):
134 132
        return self.username
135 133

  
......
137 135
    """
138 136
    Model for approval terms
139 137
    """
140
    
138

  
141 139
    date = models.DateTimeField('Issue date', db_index=True, default=datetime.now())
142 140
    location = models.CharField('Terms location', max_length=255)
143 141

  
......
157 155
    #obsolete: we keep it just for transfering the data
158 156
    accepted = models.DateTimeField('Acceptance date', null=True, blank=True)
159 157
    consumed = models.DateTimeField('Consumption date', null=True, blank=True)
160
    
158

  
161 159
    def consume(self):
162 160
        self.is_consumed = True
163 161
        self.consumed = datetime.now()
164 162
        self.save()
165
        
163

  
166 164
    def __unicode__(self):
167 165
        return '%s -> %s [%d]' % (self.inviter, self.username, self.code)
168 166

  
......
177 175
            if (db_instance.__getattribute__(f) != user.__getattribute__(f)):
178 176
                return True
179 177
        return False
180
    
178

  
181 179
    if QUEUE_CONNECTION and should_send(user):
180

  
181
        from astakos.im.queue.userevent import UserEvent
182
        from synnefo.lib.queue import exchange_connect, exchange_send, \
183
                exchange_close
184

  
182 185
        eventType = 'create' if not user.id else 'modify'
183 186
        body = UserEvent(QUEUE_CLIENT_ID, user, eventType, {}).format()
184 187
        conn = exchange_connect(QUEUE_CONNECTION)
......
187 190
        routing_key = '%s.user' % exchange
188 191
        exchange_send(conn, routing_key, body)
189 192
        exchange_close(conn)
193

  

Also available in: Unified diff