models.py 2.9 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.
    """

Zosman's avatar
draft 1    
Zosman committed
22
23
24
    # Is this User Bloacked?
    blocked = models.BooleanField(default=False)

25
26
    # Let's associate a User to this RegisteredUser
    user = models.OneToOneField(User)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
27

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

34
    # What organization are you associated with?
35
36
37
38
39
    organization = models.CharField(
        blank=False,
        max_length=100,
    )

40
    # Why do you want to use Go?
41
    description = models.TextField(blank=True)
42

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

46
47
48
49
    # Are you approved to use Go?
    approved = models.BooleanField(default=False)

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

53

54
55
56
57
# 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):
58
59
60
    if created:
        RegisteredUser.objects.create(user=instance)

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

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):
82
        return '<%s : %s>' % (self.owner.user, self.target)
83
84
85
86
87
88

    class Meta:
        ordering = ['short']

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