models.py 2.88 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
19
20
21
22
23
24
25
26
class StudentQuerySet(models.query.QuerySet):
    def floor(self):
        return self.filter(privacy=FLOOR)

    def building(self):
        return self.filter(privacy=BUILDING)

    def students(self):
        return self.filter(privacy=STUDENTS)

class StudentManager(models.Manager):
Daniel W Bond's avatar
Daniel W Bond committed
27
    def get_queryset(self):
Daniel W Bond's avatar
Daniel W Bond committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        return StudentQuerySet(self.model, using=self._db)

    def floor(self):
        return self.get_query_set().floor()

    def building(self):
        return self.get_query_set().building()

    def students(self):
        return self.get_query_set().students()

    # when a student is not on a floor, but in a building
    def building_students(self):
        building = self.get_query_set().building()
        students = self.get_query_set().students()
        return building + list(set(students) - set(building))
 
    # when a student is on a floor
    def floor_building_students(self):
        floor = self.get_query_set().floor()
        building = self.get_query_set().building()
        students = self.get_query_set().students()

        building_students = building_students()

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

55
56
57
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
58
59
60
61
62
63
64
65
66
67
68
69
70

    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)

71
72
73
74
75
76
77
78
    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
79
80
    objects = StudentManager()

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

93
94
        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
95

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


99
100
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username