models.py 1.55 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

# Create your models here.
13
class Major(TimeStampedModel):
14
15
    name = models.CharField(max_length = 50)
    # I believe the longest is "Government and International Politics"
16
17
18
19

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
20
21
22
23
24
25
26
27
28
29
30
31
32

    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)

33
34
35
36
37
38
39
40
    room = models.OneToOneField(Room)
    clas = models.OneToOneField(Class)
    major = models.OneToOneField(Major)

    # social media accounts

    slug = AutoSlugField(populate_from='user', unique=True)

Jason D Yeomans's avatar
Jason D Yeomans committed
41
    def profile_image_url(self):
42
43
        fb_uid = SocialAccount.objects.filter(user=self.user.id, provider='facebook')
        print("profile_image")
Jason D Yeomans's avatar
Jason D Yeomans committed
44

45
46
        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
47

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


51
52
    def __str__(self):              # __unicode__ on Python 2
        return self.user.username