models.py 2.21 KB
Newer Older
1
from django.db import models
Jean Michel Rouly's avatar
Jean Michel Rouly committed
2
3
from django.contrib.auth.models import User
from django.utils import timezone
4
from django.core.cache import cache
5
6
# from django.conf import settings
import string  # random
7
from hashids import Hashids
8
9


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

12
13

class URL(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
14
15
16
17
18
19
    """
    This model represents a stored URL redirection rule. Each URL has an
    owner, target url, short identifier, click counter, and expiration
    date.
    """

20
21
    owner = models.ForeignKey(User)
    date_created = models.DateTimeField(default=timezone.now)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
22

23
    target = models.URLField(max_length=1000)
David Haynes's avatar
David Haynes committed
24
    short = models.SlugField(primary_key=True, max_length=20)
25
    clicks = models.IntegerField(default=0)
26

27
28
    qrclicks = models.IntegerField(default=0)
    socialclicks = models.IntegerField(default=0)
29

30
    expires = models.DateTimeField(blank=True, null=True)
31

32
    def __unicode__(self):
33
        return '<%s : %s>' % (self.owner.username, self.target)
34
35
36

    class Meta:
        ordering = ['short']
37
38
39

    @staticmethod
    def generate_valid_short():
40
41
42
43
        if cache.get("hashids_counter") == None:
            cache.set("hashids_counter", URL.objects.count())
        cache.incr("hashids_counter")
        short = hashids.encrypt(cache.get("hashids_counter"))
44
        tries = 1
Akshay Karthik's avatar
Akshay Karthik committed
45
        while tries < 100:
46
            try:
47
                urls = URL.objects.get(short__iexact=short)
48
                tries += 1
49
                cache.incr("hashids_counter")
50
51
            except URL.DoesNotExist:
                return short
Akshay Karthik's avatar
Akshay Karthik committed
52
        return None
53

54
class RegisteredUser(models.Model):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
55
56
57
58
59
    """
    This is simply a wrapper model which, if an object exists, indicates
    that that user is registered.
    """

60
    username = models.CharField(
61
62
63
        blank=False,
        max_length=30,
        primary_key=True
64
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
65

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

71
72
73
74
75
    organization = models.CharField(
        blank=False,
        max_length=100,
    )

76
    description = models.TextField(blank=True)
77

78
79
    approved = models.BooleanField()

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