models.py 2.93 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
21
22
23
24
25
26
27
28
29
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
30
    def get_queryset(self):
Daniel W Bond's avatar
Daniel W Bond committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        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))

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

    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)

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

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

96
97
        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
98

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


102
103
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username