models.py 2.18 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.conf import settings
5
6
import random, string

7
8
9
from hashids import Hashids
hashids = Hashids(salt="srct.gmu.edu", alphabet=(string.ascii_lowercase + string.digits))
hashids_counter = None
Jean Michel Rouly's avatar
Jean Michel Rouly committed
10

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

Jean Michel Rouly's avatar
Jean Michel Rouly committed
18
    owner = models.ForeignKey( User )
Chris Reffett's avatar
Chris Reffett committed
19
    date_created = models.DateTimeField( default=timezone.now )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
20

Jean Michel Rouly's avatar
Jean Michel Rouly committed
21
    target = models.URLField( max_length = 1000 )
22
    short = models.CharField( primary_key = True, max_length = 20 )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
23
    clicks = models.IntegerField( default = 0 )
24

25
26
    qrclicks = models.IntegerField(default=0)
    socialclicks = models.IntegerField(default=0)
27

28
    expires = models.DateTimeField( blank = True, null = True )
29

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

    class Meta:
        ordering = ['short']
35
36
37

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

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

Jean Michel Rouly's avatar
Jean Michel Rouly committed
54

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

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

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

72
    description = models.TextField( blank=True )
73

74
75
    approved = models.BooleanField()

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