models.py 3.48 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
# http://hashids.org/python/
12
from hashids import Hashids
13

14
# generate the salt and initialize Hashids
15
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
Jean Michel Rouly's avatar
Jean Michel Rouly committed
16

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

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

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

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

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

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

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

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

51

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

59

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

67
    # Who is the owner of this Go link
68
    owner = models.ForeignKey(RegisteredUser)
69
    # When was this link created?
70
71
    date_created = models.DateTimeField(default=timezone.now)

72
    # What is the target URL for this Go link
73
    target = models.URLField(max_length=1000)
74
75
    # What is the actual go link (short url) for this URL
    short = models.SlugField(max_length=20)
76

77
78
79
    # how many people have visited this Go link
    clicks = models.IntegerField(default=0)
    # how many people have visited this Go link through the qr code
80
    qrclicks = models.IntegerField(default=0)
81
    # how many people have visited the go link through social media
82
83
    socialclicks = models.IntegerField(default=0)

84
    # does this Go link expire on a certain date
85
86
    expires = models.DateTimeField(blank=True, null=True)

87
    # print(URL)
88
    def __unicode__(self):
89
        return '<%s : %s>' % (self.owner.user, self.target)
90

91
    # metadata for URL's
92
    class Meta:
93
        # they should be ordered by their short links
94
95
        ordering = ['short']

96
97
    # legacy method to ensure that generated short URL's are valid
    # should be updated to be simpler 
98
99
    @staticmethod
    def generate_valid_short():
100
        if cache.get("hashids_counter") is None:
101
102
103
104
105
106
            cache.set("hashids_counter", URL.objects.count())
        cache.incr("hashids_counter")
        short = hashids.encrypt(cache.get("hashids_counter"))
        tries = 1
        while tries < 100:
            try:
107
                URL.objects.get(short__iexact=short)
108
109
110
111
112
                tries += 1
                cache.incr("hashids_counter")
            except URL.DoesNotExist:
                return short
        return None