models.py 3.67 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
12
13

# Other Imports
import string
14
# http://hashids.org/python/
15
from hashids import Hashids
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
class RegisteredUser(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
25

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

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

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

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

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

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

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

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

57

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

65

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

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

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

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

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

93
    # print(URL)
94
    def __unicode__(self):
95
        return '<%s : %s>' % (self.owner.user, self.target)
96

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

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