models.py 3.09 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 29

class StudentManager(models.Manager):
Daniel W Bond's avatar
Daniel W Bond committed
30
    def get_queryset(self):
Daniel W Bond's avatar
Daniel W Bond committed
31 32 33
        return StudentQuerySet(self.model, using=self._db)

    def floor(self):
Daniel W Bond's avatar
Daniel W Bond committed
34
        return self.get_queryset().floor()
Daniel W Bond's avatar
Daniel W Bond committed
35 36

    def building(self):
Daniel W Bond's avatar
Daniel W Bond committed
37
        return self.get_queryset().building()
Daniel W Bond's avatar
Daniel W Bond committed
38 39

    def students(self):
Daniel W Bond's avatar
Daniel W Bond committed
40
        return self.get_queryset().students()
Daniel W Bond's avatar
Daniel W Bond committed
41 42 43

    # when a student is not on a floor, but in a building
    def building_students(self):
Daniel W Bond's avatar
Daniel W Bond committed
44 45 46
        building = self.get_queryset().building()
        students = self.get_queryset().students()
        return list(building) + list(set(students) - set(building))
Daniel W Bond's avatar
Daniel W Bond committed
47 48 49
 
    # when a student is on a floor
    def floor_building_students(self):
Daniel W Bond's avatar
Daniel W Bond committed
50 51 52
        floor = self.get_queryset().floor()
        building = self.get_queryset().building()
        students = self.get_queryset().students()
Daniel W Bond's avatar
Daniel W Bond committed
53

Daniel W Bond's avatar
Daniel W Bond committed
54 55 56
        # using the function above results in UnboundLocalError excpetion
        #building_students = building_students()
        building_students = list(building) + list(set(students) - set(building))
Daniel W Bond's avatar
Daniel W Bond committed
57

Daniel W Bond's avatar
Daniel W Bond committed
58
        return list(floor) + list(set(building_students) - set(floor))
Daniel W Bond's avatar
Daniel W Bond committed
59

60 61 62
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
63 64 65 66 67 68 69 70 71 72 73 74 75

    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)

76 77 78 79 80 81 82 83
    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
84 85
    objects = StudentManager()

Daniel W Bond's avatar
Daniel W Bond committed
86 87 88 89 90 91 92 93
    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
94
    def profile_image_url(self):
95 96
        fb_uid = SocialAccount.objects.filter(user=self.user.id, provider='facebook')
        print("profile_image")
Jason D Yeomans's avatar
Jason D Yeomans committed
97

98 99
        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
100

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


104 105
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username