models.py 3.8 KB
Newer Older
1
2
3
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division

4
# Django Imports
5
from django.db import models
Jean Michel Rouly's avatar
Jean Michel Rouly committed
6
7
from django.contrib.auth.models import User
from django.utils import timezone
8
from django.core.cache import cache
9
10
from django.db.models.signals import post_save
from django.dispatch import receiver
11
from django.utils.encoding import python_2_unicode_compatible
12
13

# Other Imports
14
from hashids import Hashids # http://hashids.org/python/
15
import string
16

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

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

27
    # Is this User Blocked?
28
    blocked = models.BooleanField(default = False)
Zosman's avatar
draft 1    
Zosman committed
29

30
31
    # Let's associate a User to this RegisteredUser
    user = models.OneToOneField(User)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
32

33
    # What is your name?
34
    full_name = models.CharField(
35
36
        blank = False,
        max_length = 100,
37
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
38

39
    # What organization are you associated with?
40
    organization = models.CharField(
41
42
        blank = False,
        max_length = 100,
43
44
    )

45
    # Why do you want to use Go?
46
    description = models.TextField(blank = True)
47

48
    # Have you filled out the registration form?
49
    registered = models.BooleanField(default = False)
50

51
    # Are you approved to use Go?
52
    approved = models.BooleanField(default = False)
53
54

    # print(RegisteredUser)
55
    def __str__(self):
56
57
        return '<Registered User: %s - Approval Status: %s>' % (self.user, self.approved)

58

59
60
61
62
# 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):
63
64
65
    if created:
        RegisteredUser.objects.create(user=instance)

66

67
"""
68
69
70
    This model represents a stored URL redirection rule. Each URL has an
    owner, target url, short identifier, click counter, and expiration
    date.
71
"""
72
@python_2_unicode_compatible
73
class URL(models.Model):
74

75
    # Who is the owner of this Go link
76
    owner = models.ForeignKey(RegisteredUser)
77
    # When was this link created?
78
79
    date_created = models.DateTimeField(default=timezone.now)

80
    # What is the target URL for this Go link
81
    target = models.URLField(max_length=1000)
82
    # What is the actual go link (short url) for this URL
David Haynes's avatar
David Haynes committed
83
    short = models.SlugField(max_length=20, primary_key=True)
84

85
86
87
    # 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
88
    qrclicks = models.IntegerField(default=0)
89
    # how many people have visited the go link through social media
90
91
    socialclicks = models.IntegerField(default=0)

92
    # does this Go link expire on a certain date
93
94
    expires = models.DateTimeField(blank=True, null=True)

95
    # print(URL)
96
    def __str__(self):
97
        return '<%s : %s>' % (self.owner.user, self.target)
98

99
    # metadata for URL's
100
    class Meta:
101
        # they should be ordered by their short links
102
103
        ordering = ['short']

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