This repository was archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathmodels.py
More file actions
68 lines (54 loc) · 2.19 KB
/
models.py
File metadata and controls
68 lines (54 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import random
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.sites.models import Site
from django.utils.hashcompat import sha_constructor
INVITATION_ALLOTMENT = getattr(settings, 'INVITATION_ALLOTMENT', 5)
INVITATION_STATUS_SENT = 0
INVITATION_STATUS_ACCEPTED = 1
INVITATION_STATUS_DECLINED = 2
INVITATION_STATUS_CHOICES = (
(INVITATION_STATUS_SENT, 'Sent'),
(INVITATION_STATUS_ACCEPTED, 'Accepted'),
(INVITATION_STATUS_DECLINED, 'Declined'),
)
class InvitationManager(models.Manager):
def get_invitation(self, token):
try:
return self.get(token=token, status=INVITATION_STATUS_SENT)
except self.model.DoesNotExist:
return False
def create_token(self, email):
salt = sha_constructor(str(random.random())).hexdigest()[:5]
token = sha_constructor(salt+email).hexdigest()
return token
class Invitation(models.Model):
""" Invitation model """
from_user = models.ForeignKey(User)
token = models.CharField(max_length=40)
name = models.CharField(blank=True, max_length=100)
email = models.EmailField()
message = models.TextField(blank=True)
status = models.PositiveSmallIntegerField(choices=INVITATION_STATUS_CHOICES, default=0)
site = models.ForeignKey(Site, default=settings.SITE_ID)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
objects = InvitationManager()
def __unicode__(self):
return '<Invite>'
@models.permalink
def get_absolute_url(self):
return ('invitations:invitation', [self.token])
class InvitationAllotment(models.Model):
""" InvitationAllotment model """
user = models.OneToOneField(User, related_name='invitation_allotment')
amount = models.IntegerField(default=INVITATION_ALLOTMENT)
site = models.ForeignKey(Site, default=settings.SITE_ID)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return '<Invitation Allotment>'
def decrement(self, amount=1):
self.amount = self.amount - amount
self.save()