models.py 2.07 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 )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
19
20
    date_created = models.DateTimeField( default=timezone.now() )

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
    expires = models.DateTimeField( blank = True, null = True )
25

26
27


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

    class Meta:
        ordering = ['short']
33
34
35

    @staticmethod
    def generate_valid_short():
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        global hashids_counter
        hashids_counter += 1
        short = hashids.encrypt(hashids_counter)
        tries = 1
        try:
            urls = URL.objects.get( short__iexact = short )
            tries += 1
            hashids_counter += 1
        except URL.DoesNotExist:
            return short
        if tries > 100:
            return None

# 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()
51

Jean Michel Rouly's avatar
Jean Michel Rouly committed
52

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

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

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

70
    description = models.TextField( blank=True )
71

72
73
    approved = models.BooleanField()

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