Statistics
| Branch: | Tag: | Revision:

root / pithos / im / models.py @ bee5ffa6

History | View | Annotate | Download (4.6 kB)

1 aa4fac11 Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 aa4fac11 Giorgos Verigakis
# 
3 aa4fac11 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 aa4fac11 Giorgos Verigakis
# without modification, are permitted provided that the following
5 aa4fac11 Giorgos Verigakis
# conditions are met:
6 aa4fac11 Giorgos Verigakis
# 
7 aa4fac11 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 aa4fac11 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 aa4fac11 Giorgos Verigakis
#      disclaimer.
10 aa4fac11 Giorgos Verigakis
# 
11 aa4fac11 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 aa4fac11 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 aa4fac11 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 aa4fac11 Giorgos Verigakis
#      provided with the distribution.
15 aa4fac11 Giorgos Verigakis
# 
16 aa4fac11 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 aa4fac11 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 aa4fac11 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 aa4fac11 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 aa4fac11 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 aa4fac11 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 aa4fac11 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 aa4fac11 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 aa4fac11 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 aa4fac11 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 aa4fac11 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 aa4fac11 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 aa4fac11 Giorgos Verigakis
# 
29 aa4fac11 Giorgos Verigakis
# The views and conclusions contained in the software and
30 aa4fac11 Giorgos Verigakis
# documentation are those of the authors and should not be
31 aa4fac11 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 aa4fac11 Giorgos Verigakis
# or implied, of GRNET S.A.
33 aa4fac11 Giorgos Verigakis
34 2d0fff7a Antony Chazapis
import logging
35 2d8da097 Antony Chazapis
import hashlib
36 2d8da097 Antony Chazapis
37 2d8da097 Antony Chazapis
from time import asctime
38 2d8da097 Antony Chazapis
from datetime import datetime, timedelta
39 2d8da097 Antony Chazapis
from base64 import b64encode
40 d28ad65e Antony Chazapis
41 7e392d16 Antony Chazapis
from django.conf import settings
42 d7d60147 Antony Chazapis
from django.db import models
43 aa4fac11 Giorgos Verigakis
44 aa4fac11 Giorgos Verigakis
45 7e392d16 Antony Chazapis
class User(models.Model):
46 7e392d16 Antony Chazapis
    ACCOUNT_STATE = (
47 7e392d16 Antony Chazapis
        ('ACTIVE', 'Active'),
48 7e392d16 Antony Chazapis
        ('DELETED', 'Deleted'),
49 bee5ffa6 Sofia Papagiannaki
        ('SUSPENDED', 'Suspended'),
50 bee5ffa6 Sofia Papagiannaki
        ('UNVERIFIED', 'Unverified')
51 7e392d16 Antony Chazapis
    )
52 7e392d16 Antony Chazapis
    
53 d7d60147 Antony Chazapis
    uniq = models.CharField('Unique ID', max_length=255, null=True)
54 7e392d16 Antony Chazapis
    
55 d7d60147 Antony Chazapis
    realname = models.CharField('Real Name', max_length=255, default='')
56 7e392d16 Antony Chazapis
    email = models.CharField('Email', max_length=255, default='')
57 d7d60147 Antony Chazapis
    affiliation = models.CharField('Affiliation', max_length=255, default='')
58 93f046ab Giorgos Verigakis
    state = models.CharField('Account state', choices=ACCOUNT_STATE,
59 93f046ab Giorgos Verigakis
                                max_length=16, default='ACTIVE')
60 93f046ab Giorgos Verigakis
    
61 bee5ffa6 Sofia Papagiannaki
    #for invitations
62 93f046ab Giorgos Verigakis
    level = models.IntegerField('Inviter level', default=4)
63 93f046ab Giorgos Verigakis
    invitations = models.IntegerField('Invitations left', default=0)
64 7e392d16 Antony Chazapis
    
65 bee5ffa6 Sofia Papagiannaki
    #for local
66 bee5ffa6 Sofia Papagiannaki
    password = models.CharField('Password', max_length=255, default='')
67 bee5ffa6 Sofia Papagiannaki
    
68 93f046ab Giorgos Verigakis
    is_admin = models.BooleanField('Admin?', default=False)
69 7e392d16 Antony Chazapis
    
70 93f046ab Giorgos Verigakis
    auth_token = models.CharField('Authentication Token', max_length=32,
71 93f046ab Giorgos Verigakis
                                    null=True, blank=True)
72 9a43d4b9 Giorgos Verigakis
    auth_token_created = models.DateTimeField('Token creation date',
73 9a43d4b9 Giorgos Verigakis
                                                null=True)
74 9a43d4b9 Giorgos Verigakis
    auth_token_expires = models.DateTimeField('Token expiration date',
75 9a43d4b9 Giorgos Verigakis
                                                null=True)
76 7e392d16 Antony Chazapis
    
77 93f046ab Giorgos Verigakis
    created = models.DateTimeField('Creation date')
78 93f046ab Giorgos Verigakis
    updated = models.DateTimeField('Update date')
79 4e02cf2a Sofia Papagiannaki
    
80 10854362 Giorgos Verigakis
    @property
81 10854362 Giorgos Verigakis
    def quota(self):
82 2d0fff7a Antony Chazapis
        return settings.DEFAULT_QUOTA
83 10854362 Giorgos Verigakis
84 10854362 Giorgos Verigakis
    @quota.setter
85 10854362 Giorgos Verigakis
    def quota(self, value):
86 2d0fff7a Antony Chazapis
        logging.debug('Set quota to: %s', value)
87 2d0fff7a Antony Chazapis
    
88 9a43d4b9 Giorgos Verigakis
    def save(self, update_timestamps=True, **kwargs):
89 4e02cf2a Sofia Papagiannaki
        if update_timestamps:
90 4e02cf2a Sofia Papagiannaki
            if not self.id:
91 2d8da097 Antony Chazapis
                self.created = datetime.now()
92 2d8da097 Antony Chazapis
            self.updated = datetime.now()
93 9a43d4b9 Giorgos Verigakis
        super(User, self).save(**kwargs)
94 51c0cb6e Antony Chazapis
    
95 2d8da097 Antony Chazapis
    def renew_token(self):
96 2d8da097 Antony Chazapis
        md5 = hashlib.md5()
97 2d8da097 Antony Chazapis
        md5.update(self.uniq)
98 2d8da097 Antony Chazapis
        md5.update(self.realname.encode('ascii', 'ignore'))
99 2d8da097 Antony Chazapis
        md5.update(asctime())
100 2d8da097 Antony Chazapis
        
101 2d8da097 Antony Chazapis
        self.auth_token = b64encode(md5.digest())
102 2d8da097 Antony Chazapis
        self.auth_token_created = datetime.now()
103 2d8da097 Antony Chazapis
        self.auth_token_expires = self.auth_token_created + \
104 2d8da097 Antony Chazapis
                                  timedelta(hours=settings.AUTH_TOKEN_DURATION)
105 2d8da097 Antony Chazapis
    
106 d7d60147 Antony Chazapis
    def __unicode__(self):
107 d7d60147 Antony Chazapis
        return self.uniq
108 7e392d16 Antony Chazapis
109 93f046ab Giorgos Verigakis
110 7e392d16 Antony Chazapis
class Invitation(models.Model):
111 93f046ab Giorgos Verigakis
    inviter = models.ForeignKey(User, related_name='invitations_sent',
112 93f046ab Giorgos Verigakis
                                null=True)
113 93f046ab Giorgos Verigakis
    realname = models.CharField('Real name', max_length=255)
114 93f046ab Giorgos Verigakis
    uniq = models.CharField('Real name', max_length=255)
115 93f046ab Giorgos Verigakis
    code = models.BigIntegerField('Invitation code', db_index=True)
116 93f046ab Giorgos Verigakis
    is_accepted = models.BooleanField('Accepted?', default=False)
117 93f046ab Giorgos Verigakis
    created = models.DateTimeField('Creation date', auto_now_add=True)
118 93f046ab Giorgos Verigakis
    accepted = models.DateTimeField('Acceptance date', null=True, blank=True)
119 7e392d16 Antony Chazapis
    
120 7e392d16 Antony Chazapis
    def __unicode__(self):
121 93f046ab Giorgos Verigakis
        return '%s -> %s [%d]' % (self.inviter, self.uniq, self.code)