models.py 2.89 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
27
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
55
56
57
58
59
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):
    def get_query_set(self):
        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()

    def floor_building(self):
        floor = self.get_query_set().floor()
        building = self.get_query_set().building()
        return floor + list(set(building) - set(floor))

    # 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))

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()

Jason D Yeomans's avatar
Jason D Yeomans committed
86
    def profile_image_url(self):
87
88
        fb_uid = SocialAccount.objects.filter(user=self.user.id, provider='facebook')
        print("profile_image")
Jason D Yeomans's avatar
Jason D Yeomans committed
89

90
91
        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
92

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


96
97
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username