models.py 3.61 KB
Newer Older
Jason D Yeomans's avatar
Jason D Yeomans committed
1
from django.db import models
2
from model_utils.models import TimeStampedModel
3 4 5
from django.contrib.auth.models import User

from autoslug import AutoSlugField
Daniel W Bond's avatar
Daniel W Bond committed
6
from django.core.urlresolvers import reverse
7 8 9

from housing.models import Building, Room, Class

Jason D Yeomans's avatar
Jason D Yeomans committed
10 11
from allauth.socialaccount.models import SocialAccount
import hashlib
Jason D Yeomans's avatar
Jason D Yeomans committed
12

13
class Major(TimeStampedModel):
14 15
    name = models.CharField(max_length = 50)
    # I believe the longest is "Government and International Politics"
16

Daniel W Bond's avatar
Daniel W Bond committed
17 18
    def __str__(self):
        return self.name
Daniel W Bond's avatar
Daniel W Bond committed
19 20 21 22 23
    def __unicode__(self):
        return unicode(self.name)

    class Meta:
        ordering = ['name']
Daniel W Bond's avatar
Daniel W Bond committed
24

Daniel W Bond's avatar
Daniel W Bond committed
25 26
class StudentQuerySet(models.query.QuerySet):
    def floor(self):
Daniel W Bond's avatar
Daniel W Bond committed
27
        return self.filter(privacy='floor')
Daniel W Bond's avatar
Daniel W Bond committed
28 29

    def building(self):
Daniel W Bond's avatar
Daniel W Bond committed
30
        return self.filter(privacy='building')
Daniel W Bond's avatar
Daniel W Bond committed
31 32

    def students(self):
Daniel W Bond's avatar
Daniel W Bond committed
33
        return self.filter(privacy='students')
Daniel W Bond's avatar
Daniel W Bond committed
34

Daniel W Bond's avatar
Daniel W Bond committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    # when a student is not on a floor, but in a building
    def building_students(self):
        building = self.building()
        students = self.students()
        return list(building) + list(set(students) - set(building))

    # when a student is on a floor
    def floor_building_students(self):
        floor = self.floor()
        building = self.building()
        students = self.students()

        # using the function above results in UnboundLocalError excpetion
        #building_students = building_students()
        building_students = list(building) + list(set(students) - set(building))

        return list(floor) + list(set(building_students) - set(floor))

Daniel W Bond's avatar
Daniel W Bond committed
53
class StudentManager(models.Manager):
Daniel W Bond's avatar
Daniel W Bond committed
54 55 56

    # this 'duplication' allows for queryset chaining

Daniel W Bond's avatar
Daniel W Bond committed
57
    def get_queryset(self):
Daniel W Bond's avatar
Daniel W Bond committed
58 59 60
        return StudentQuerySet(self.model, using=self._db)

    def floor(self):
Daniel W Bond's avatar
Daniel W Bond committed
61
        return self.get_queryset().floor()
Daniel W Bond's avatar
Daniel W Bond committed
62 63

    def building(self):
Daniel W Bond's avatar
Daniel W Bond committed
64
        return self.get_queryset().building()
Daniel W Bond's avatar
Daniel W Bond committed
65 66

    def students(self):
Daniel W Bond's avatar
Daniel W Bond committed
67
        return self.get_queryset().students()
Daniel W Bond's avatar
Daniel W Bond committed
68 69

    def building_students(self):
Daniel W Bond's avatar
Daniel W Bond committed
70
        return self.get_queryset().building_students()
Daniel W Bond's avatar
Daniel W Bond committed
71 72
 
    def floor_building_students(self):
Daniel W Bond's avatar
Daniel W Bond committed
73
        return self.get_queryset().floor_building_students()
Daniel W Bond's avatar
Daniel W Bond committed
74

75 76 77
class Student(TimeStampedModel):
    user = models.OneToOneField(User)
    # Django user includes a username, password, email, first name, and last name
Daniel W Bond's avatar
Daniel W Bond committed
78 79 80 81 82 83 84 85 86 87 88 89 90

    FLOOR = 'floor'
    BUILDING = 'building'
    STUDENTS = 'students'

    PRIVACY_CHOICES = (
        (FLOOR, 'My Floor'),
        (BUILDING, 'My Building'),
        (STUDENTS, 'All Students'),
    )

    privacy = models.CharField(max_length=100, choices=PRIVACY_CHOICES, default=FLOOR)

Daniel W Bond's avatar
Daniel W Bond committed
91 92 93
    room = models.ForeignKey(Room)
    clas = models.ForeignKey(Class)
    major = models.ForeignKey('Major')
94 95 96 97 98

    # social media accounts

    slug = AutoSlugField(populate_from='user', unique=True)

Daniel W Bond's avatar
Daniel W Bond committed
99 100
    objects = StudentManager()

Daniel W Bond's avatar
Daniel W Bond committed
101 102 103 104 105 106 107 108
    def get_floor(self):
        floor = self.room.floor
        return floor

    def get_building(self):
        building = self.room.floor.building
        return building

Jason D Yeomans's avatar
Jason D Yeomans committed
109
    def profile_image_url(self):
110 111
        fb_uid = SocialAccount.objects.filter(user=self.user.id, provider='facebook')
        print("profile_image")
Jason D Yeomans's avatar
Jason D Yeomans committed
112

113 114
        if len(fb_uid)>0:
            return "http://graph.facebook.com/{}/picture?width=175&height=175".format(fb_uid[0].uid)
Jason D Yeomans's avatar
Jason D Yeomans committed
115

116
        return "http://www.gravatar.com/avatar/{}?s=175".format(hashlib.md5(self.user.email).hexdigest())
Jason D Yeomans's avatar
Jason D Yeomans committed
117 118


Daniel W Bond's avatar
Daniel W Bond committed
119 120 121
    def get_absolute_url(self):
        return reverse('detail_student', kwargs={'slug':self.slug})

Daniel W Bond's avatar
Daniel W Bond committed
122 123 124
    class Meta:
        ordering = ['user']

125 126
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username
Daniel W Bond's avatar
Daniel W Bond committed
127 128
    def __unicode__(self):
        return unicode(self.user.username)