models.py 3.26 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 6 7 8
from django.contrib.auth.models import User

from autoslug import AutoSlugField

from housing.models import Building, Room, Class

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

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

Daniel W Bond's avatar
Daniel W Bond committed
16 17 18
    def __str__(self):
        return self.name

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

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

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

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

    # this 'duplication' allows for queryset chaining

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

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

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

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

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

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

    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)

85 86 87 88 89 90 91 92
    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
93 94
    objects = StudentManager()

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

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

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


113 114
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username