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 URL(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
16
17
18
19
20
21
    """
    This model represents a stored URL redirection rule. Each URL has an
    owner, target url, short identifier, click counter, and expiration
    date.
    """

22
23
    owner = models.ForeignKey(User)
    date_created = models.DateTimeField(default=timezone.now)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
24

25
    target = models.URLField(max_length=1000)
David Haynes's avatar
David Haynes committed
26
    short = models.SlugField(primary_key=True, max_length=20)
27
    clicks = models.IntegerField(default=0)
28

29
30
    qrclicks = models.IntegerField(default=0)
    socialclicks = models.IntegerField(default=0)
31

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

34
    def __unicode__(self):
35
        return '<%s : %s>' % (self.owner.username, self.target)
36
37
38

    class Meta:
        ordering = ['short']
39
40
41

    @staticmethod
    def generate_valid_short():
42
43
44
45
        if cache.get("hashids_counter") == None:
            cache.set("hashids_counter", URL.objects.count())
        cache.incr("hashids_counter")
        short = hashids.encrypt(cache.get("hashids_counter"))
46
        tries = 1
Akshay Karthik's avatar
Akshay Karthik committed
47
        while tries < 100:
48
            try:
49
                urls = URL.objects.get(short__iexact=short)
50
                tries += 1
51
                cache.incr("hashids_counter")
52
53
            except URL.DoesNotExist:
                return short
Akshay Karthik's avatar
Akshay Karthik committed
54
        return None
55

56
class RegisteredUser(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
57
58
59
60
61
    """
    This is simply a wrapper model which, if an object exists, indicates
    that that user is registered.
    """

62
63
    # Let's associate a User to this RegisteredUser
    user = models.OneToOneField(User)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
64

65
    # What is your name?
66
    full_name = models.CharField(
67
68
        blank=False,
        max_length=100,
69
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
70

71
    # What organization are you associated with?
72
73
74
75
76
    organization = models.CharField(
        blank=False,
        max_length=100,
    )

77
    # Why do you want to use Go?
78
    description = models.TextField(blank=True)
79

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

83
84
85
86
    # Are you approved to use Go?
    approved = models.BooleanField(default=False)

    # print(RegisteredUser)
87
    def __unicode__(self):
88
89
90
91
92
93
94
95
        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)