Statistics
| Branch: | Tag: | Revision:

root / pithos / im / models.py @ 9afb87c8

History | View | Annotate | Download (4.5 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

    
44

    
45
class User(models.Model):
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,
58
                                max_length=16, default='ACTIVE')
59
    
60
    level = models.IntegerField('Inviter level', default=4)
61
    invitations = models.IntegerField('Invitations left', default=0)
62
    
63
    is_admin = models.BooleanField('Admin?', default=False)
64
    
65
    auth_token = models.CharField('Authentication Token', max_length=32,
66
                                    null=True, blank=True)
67
    auth_token_created = models.DateTimeField('Token creation date',
68
                                                null=True)
69
    auth_token_expires = models.DateTimeField('Token expiration date',
70
                                                null=True)
71
    
72
    created = models.DateTimeField('Creation date')
73
    updated = models.DateTimeField('Update date')
74
    
75
    @property
76
    def quota(self):
77
        return settings.DEFAULT_QUOTA
78

    
79
    @quota.setter
80
    def quota(self, value):
81
        logging.debug('Set quota to: %s', value)
82
    
83
    def save(self, update_timestamps=True, **kwargs):
84
        if update_timestamps:
85
            if not self.id:
86
                self.created = datetime.now()
87
            self.updated = datetime.now()
88
        super(User, self).save(**kwargs)
89
    
90
    def renew_token(self):
91
        md5 = hashlib.md5()
92
        md5.update(self.uniq)
93
        md5.update(self.realname.encode('ascii', 'ignore'))
94
        md5.update(asctime())
95
        
96
        self.auth_token = b64encode(md5.digest())
97
        self.auth_token_created = datetime.now()
98
        self.auth_token_expires = self.auth_token_created + \
99
                                  timedelta(hours=settings.AUTH_TOKEN_DURATION)
100
    
101
    def __unicode__(self):
102
        return self.uniq
103

    
104

    
105
class Invitation(models.Model):
106
    inviter = models.ForeignKey(User, related_name='invitations_sent',
107
                                null=True)
108
    realname = models.CharField('Real name', max_length=255)
109
    uniq = models.CharField('Real name', max_length=255)
110
    code = models.BigIntegerField('Invitation code', db_index=True)
111
    is_accepted = models.BooleanField('Accepted?', default=False)
112
    created = models.DateTimeField('Creation date', auto_now_add=True)
113
    accepted = models.DateTimeField('Acceptance date', null=True, blank=True)
114
    
115
    def __unicode__(self):
116
        return '%s -> %s [%d]' % (self.inviter, self.uniq, self.code)