models.py 3.57 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
    # Is this User Blocked?
Zosman's avatar
draft 1    
Zosman committed
24
25
    blocked = models.BooleanField(default=False)

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

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

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

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

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

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

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

54

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

62

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

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

75
    # What is the target URL for this Go link
76
    target = models.URLField(max_length=1000)
77
    # What is the actual go link (short url) for this URL
David Haynes's avatar
David Haynes committed
78
    short = models.SlugField(max_length=20, primary_key=True)
79

80
81
82
    # 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
83
    qrclicks = models.IntegerField(default=0)
84
    # how many people have visited the go link through social media
85
86
    socialclicks = models.IntegerField(default=0)

87
    # does this Go link expire on a certain date
88
89
    expires = models.DateTimeField(blank=True, null=True)

90
    # print(URL)
91
    def __unicode__(self):
92
        return '<%s : %s>' % (self.owner.user, self.target)
93

94
    # metadata for URL's
95
    class Meta:
96
        # they should be ordered by their short links
97
98
        ordering = ['short']

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