models.py 2.82 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
class RegisteredUser(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
16
17
18
19
20
    """
    This is simply a wrapper model which, if an object exists, indicates
    that that user is registered.
    """

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

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

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

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

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

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

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

# 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):
  if created:
    RegisteredUser.objects.create(user=instance)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

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):
76
        return '<%s : %s>' % (self.owner.user, self.target)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

    class Meta:
        ordering = ['short']

    @staticmethod
    def generate_valid_short():
        if cache.get("hashids_counter") == None:
            cache.set("hashids_counter", URL.objects.count())
        cache.incr("hashids_counter")
        short = hashids.encrypt(cache.get("hashids_counter"))
        tries = 1
        while tries < 100:
            try:
                urls = URL.objects.get(short__iexact=short)
                tries += 1
                cache.incr("hashids_counter")
            except URL.DoesNotExist:
                return short
        return None