models.py 2.83 KB
Newer Older
1
# Django Imports
2
from django.db import models
Jean Michel Rouly's avatar
Jean Michel Rouly committed
3
4
from django.contrib.auth.models import User
from django.utils import timezone
5
from django.core.cache import cache
6
7
from django.db.models.signals import post_save
from django.dispatch import receiver
8
9
10

# Other Imports
import string
11
from hashids import Hashids
12

13
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
Jean Michel Rouly's avatar
Jean Michel Rouly committed
14

15

16
class RegisteredUser(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
17
18
19
20
21
    """
    This is simply a wrapper model which, if an object exists, indicates
    that that user is registered.
    """

22
23
    # Let's associate a User to this RegisteredUser
    user = models.OneToOneField(User)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
24

25
    # What is your name?
26
    full_name = models.CharField(
27
28
        blank=False,
        max_length=100,
29
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
30

31
    # What organization are you associated with?
32
33
34
35
36
    organization = models.CharField(
        blank=False,
        max_length=100,
    )

37
    # Why do you want to use Go?
38
    description = models.TextField(blank=True)
39

40
    # Have you filled out the registration form?
David Haynes's avatar
David Haynes committed
41
    registered = models.BooleanField(default=False)
42

43
44
45
46
    # Are you approved to use Go?
    approved = models.BooleanField(default=False)

    # print(RegisteredUser)
47
    def __unicode__(self):
48
49
        return '<Registered User: %s - Approval Status: %s>' % (self.user, self.approved)

50

51
52
53
54
# When a post_save is called on a User object (and it is newly created), this is
# called to create an associated RegisteredUser
@receiver(post_save, sender=User)
def handle_regUser_creation(sender, instance, created, **kwargs):
55
56
57
    if created:
        RegisteredUser.objects.create(user=instance)

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

class URL(models.Model):
    """
    This model represents a stored URL redirection rule. Each URL has an
    owner, target url, short identifier, click counter, and expiration
    date.
    """

    owner = models.ForeignKey(RegisteredUser)
    date_created = models.DateTimeField(default=timezone.now)

    target = models.URLField(max_length=1000)
    short = models.SlugField(primary_key=True, max_length=20)
    clicks = models.IntegerField(default=0)

    qrclicks = models.IntegerField(default=0)
    socialclicks = models.IntegerField(default=0)

    expires = models.DateTimeField(blank=True, null=True)

    def __unicode__(self):
79
        return '<%s : %s>' % (self.owner.user, self.target)
80
81
82
83
84
85

    class Meta:
        ordering = ['short']

    @staticmethod
    def generate_valid_short():
86
        if cache.get("hashids_counter") is None:
87
88
89
90
91
92
            cache.set("hashids_counter", URL.objects.count())
        cache.incr("hashids_counter")
        short = hashids.encrypt(cache.get("hashids_counter"))
        tries = 1
        while tries < 100:
            try:
93
                URL.objects.get(short__iexact=short)
94
95
96
97
98
                tries += 1
                cache.incr("hashids_counter")
            except URL.DoesNotExist:
                return short
        return None