models.py 2.76 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
5
from django.urls 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 14
    user = models.OneToOneField(User,
                                on_delete=models.CASCADE)
15

16 17 18
    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
19
    slug = AutoSlugField(populate_from='user', unique=True)
20

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

23 24 25 26 27 28 29
    def get_first_name_or_uname(self):
        if not(self.user.first_name):
            return self.user.username
        else:
            return self.user.first_name

    def get_full_name_or_uname(self):
Mark Stenglein's avatar
Mark Stenglein committed
30
        if not(self.user.get_full_name()):
31 32 33 34
            return self.user.username
        else:
            return self.user.get_full_name()

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    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
57
    def get_absolute_url(self):
Daniel W Bond's avatar
Daniel W Bond committed
58
        return reverse('profile', kwargs={'slug': self.slug})
Daniel W Bond's avatar
Daniel W Bond committed
59

60 61 62
    def __str__(self):
        return self.get_full_name_or_uname()

63 64
    def __unicode__(self):
        return '%s' % self.user.username
65

Daniel W Bond's avatar
Daniel W Bond committed
66

67
class Course(TimeStampedModel):
Daniel W Bond's avatar
Daniel W Bond committed
68 69 70
    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
71 72
    # number = models.CharField(max_length=255,
                              # validators=RegexValidator('[0-9]{3,}'))
Daniel W Bond's avatar
Daniel W Bond committed
73 74 75 76
    number = models.CharField(max_length=3)

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