models.py 2.15 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
5
# from django.conf import settings
import string  # random
6
from hashids import Hashids
7
8


9
10
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
hashids_counter = None
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
        global hashids_counter
        hashids_counter += 1
        short = hashids.encrypt(hashids_counter)
        tries = 1
Akshay Karthik's avatar
Akshay Karthik committed
44
        while tries < 100:
45
            try:
46
                # urls = URL.objects.get(short__iexact=short)
47
                tries += 1
Akshay Karthik's avatar
Akshay Karthik committed
48
                hashids_counter += 1
49
50
            except URL.DoesNotExist:
                return short
Akshay Karthik's avatar
Akshay Karthik committed
51
        return None
52

53
54
# this needs to be here instead of at the top because the model's manager
# must be available before this line
55
hashids_counter = URL.objects.count()
56

Jean Michel Rouly's avatar
Jean Michel Rouly committed
57

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

64
    username = models.CharField(
65
66
67
        blank=False,
        max_length=30,
        primary_key=True
68
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
69

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

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

77
78
    approved = models.BooleanField()

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