models.py 3.4 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 19
    def __str__(self):
        return self.name

Daniel W Bond's avatar
Daniel W Bond committed
20 21
class StudentQuerySet(models.query.QuerySet):
    def floor(self):
Daniel W Bond's avatar
Daniel W Bond committed
22
        return self.filter(privacy='floor')
Daniel W Bond's avatar
Daniel W Bond committed
23 24

    def building(self):
Daniel W Bond's avatar
Daniel W Bond committed
25
        return self.filter(privacy='building')
Daniel W Bond's avatar
Daniel W Bond committed
26 27

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

Daniel W Bond's avatar
Daniel W Bond committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    # 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
48
class StudentManager(models.Manager):
Daniel W Bond's avatar
Daniel W Bond committed
49 50 51

    # this 'duplication' allows for queryset chaining

Daniel W Bond's avatar
Daniel W Bond committed
52
    def get_queryset(self):
Daniel W Bond's avatar
Daniel W Bond committed
53 54 55
        return StudentQuerySet(self.model, using=self._db)

    def floor(self):
Daniel W Bond's avatar
Daniel W Bond committed
56
        return self.get_queryset().floor()
Daniel W Bond's avatar
Daniel W Bond committed
57 58

    def building(self):
Daniel W Bond's avatar
Daniel W Bond committed
59
        return self.get_queryset().building()
Daniel W Bond's avatar
Daniel W Bond committed
60 61

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

    def building_students(self):
Daniel W Bond's avatar
Daniel W Bond committed
65
        return self.get_queryset().building_students()
Daniel W Bond's avatar
Daniel W Bond committed
66 67
 
    def floor_building_students(self):
Daniel W Bond's avatar
Daniel W Bond committed
68
        return self.get_queryset().floor_building_students()
Daniel W Bond's avatar
Daniel W Bond committed
69

70 71 72
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
73 74 75 76 77 78 79 80 81 82 83 84 85

    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)

86 87 88 89 90 91 92 93
    room = models.OneToOneField(Room)
    clas = models.OneToOneField(Class)
    major = models.OneToOneField(Major)

    # social media accounts

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

Daniel W Bond's avatar
Daniel W Bond committed
94 95
    objects = StudentManager()

Daniel W Bond's avatar
Daniel W Bond committed
96 97 98 99 100 101 102 103
    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
104
    def profile_image_url(self):
105 106
        fb_uid = SocialAccount.objects.filter(user=self.user.id, provider='facebook')
        print("profile_image")
Jason D Yeomans's avatar
Jason D Yeomans committed
107

108 109
        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
110

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


Daniel W Bond's avatar
Daniel W Bond committed
114 115 116
    def get_absolute_url(self):
        return reverse('detail_student', kwargs={'slug':self.slug})

117 118
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username