models.py 2.48 KB
Newer Older
1
# core django imports
2
from django.db import models
Daniel W Bond's avatar
Daniel W Bond committed
3
from django.contrib.auth.models import User
Daniel W Bond's avatar
Daniel W Bond committed
4
#from django.core.validators import RegexValidator
Daniel W Bond's avatar
Daniel W Bond committed
5
from django.core.urlresolvers import reverse
6
7
8
# third-party imports
from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel
9

Daniel W Bond's avatar
Daniel W Bond committed
10

11
12
class Student(TimeStampedModel):
    # django user includes username, password, first name, and last name
13
    user = models.OneToOneField(User)
14

15
16
17
    pf_first_name = models.CharField(max_length=255, blank=True)
    pf_last_name = models.CharField(max_length=255, blank=True)

Daniel W Bond's avatar
Daniel W Bond committed
18
    slug = AutoSlugField(populate_from='user', unique=True)
19

20
21
    emails_sent = models.PositiveIntegerField(default=0)

22
    def get_full_name(self):
Mark Stenglein's avatar
Mark Stenglein committed
23
        if not(self.user.get_full_name()):
24
25
26
27
            return self.user.username
        else:
            return self.user.get_full_name()

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    def has_nickname(self):
        pf_name = "%s %s" % (self.pf_first_name, self.pf_last_name)

        if (self.user.get_full_name() != pf_name) and (pf_name != " "):
            return True
        else:
            return False

    def get_nickname(self):
        # different first name
        if (self.user.first_name != self.pf_first_name) and (self.user.last_name == self.pf_last_name):
            return "%s \"%s\" %s" % (self.pf_first_name, self.user.first_name, self.user.last_name)
        # different last name
        elif (self.user.first_name == self.pf_first_name) and (self.user.last_name != self.pf_last_name):
            return "%s %s \"%s\"" % (self.user.first_name, self.pf_last_name, self.user.last_name)
        # both
        elif (self.user.first_name != self.pf_first_name) and (self.user.last_name != self.pf_last_name):
            return "%s \"%s %s\" %s" % (self.pf_first_name, self.user.first_name, self.user.last_name, self.pf_last_name)
        # failing gracefully
        else:
            return self.user.get_full_name()

Daniel W Bond's avatar
Daniel W Bond committed
50
    def get_absolute_url(self):
Daniel W Bond's avatar
Daniel W Bond committed
51
        return reverse('profile', kwargs={'slug': self.slug})
Daniel W Bond's avatar
Daniel W Bond committed
52

53
54
    def __unicode__(self):
        return '%s' % self.user.username
55

Daniel W Bond's avatar
Daniel W Bond committed
56

57
class Course(TimeStampedModel):
Daniel W Bond's avatar
Daniel W Bond committed
58
59
60
    name = models.CharField(max_length=255)
    department = models.CharField(max_length=255)
    departmentAbbreviation = models.CharField(max_length=4)
Daniel W Bond's avatar
Daniel W Bond committed
61
62
    # number = models.CharField(max_length=255,
                              # validators=RegexValidator('[0-9]{3,}'))
Daniel W Bond's avatar
Daniel W Bond committed
63
64
65
66
    number = models.CharField(max_length=3)

    def __unicode__(self):
        return "%s %s" % (self.departmentAbbreviation, self.number)