models.py 2.2 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
8

# Other Imports
import string
9
from hashids import Hashids
10
11


12
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
Jean Michel Rouly's avatar
Jean Michel Rouly committed
13

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
    username = models.CharField(
63
64
65
        blank=False,
        max_length=30,
        primary_key=True
66
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
67

68
    full_name = models.CharField(
69
70
        blank=False,
        max_length=100,
71
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
72

73
74
75
76
77
    organization = models.CharField(
        blank=False,
        max_length=100,
    )

78
    description = models.TextField(blank=True)
79

80
81
    approved = models.BooleanField()

82
    def __unicode__(self):
83
        return '<Registered User: %s - Approval Status: %s>' % (self.username, self.approved)