views.py 3.77 KB
Newer Older
1
# core django imports
2
from django.shortcuts import render, get_object_or_404
3
from django.http import HttpResponseForbidden
Daniel W Bond's avatar
Daniel W Bond committed
4
from django.views.generic import DetailView, ListView, UpdateView, UpdateView, DeleteView
5
# third party imports
6
from braces.views import LoginRequiredMixin
7
8
9
# imports from your apps
from .models import Student

10

Daniel W Bond's avatar
Daniel W Bond committed
11
12
# update a student (students are *created* on first login via CAS)
class UpdateStudent(LoginRequiredMixin, UpdateView):
Daniel W Bond's avatar
Daniel W Bond committed
13
    model = Student
Daniel W Bond's avatar
Daniel W Bond committed
14
    fields = ['room', 'privacy',]
15
16
    context_object_name = 'student'
    template_name = 'updateStudent.html'
Daniel W Bond's avatar
Daniel W Bond committed
17

18
    login_url = 'login'
Daniel W Bond's avatar
Daniel W Bond committed
19

20
21
22
23
24
25
26
27
28
29
30
31
    def get(self, request, *args, **kwargs):

        current_url = self.request.get_full_path()
        url_uname = current_url.split('/')[3]

        print url_uname, self.request.user.username

        if not(url_uname == self.request.user.username):
            print "I'm sorry, what now?"
            return HttpResponseForbidden()
        else:
            return super(UpdateStudent, self).get(request, *args, **kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
32

33
34
35
class UpdateStudentMajor(LoginRequiredMixin, UpdateView):
    models = Student
    fields = ['major',]
36
    template_name = 'updateStudentMajor.html'
37

38
    login_url = 'login'
39
40
41
42
43

    # copied from below
#    def get_object(self):
#        return get_object_or_404(Student, pk=self.request.session['_auth_user_id'])

44
45
46
# details about the student
class DetailStudent(LoginRequiredMixin, DetailView):
    model = Student
47
48
49
50
    context_object_name = 'student'
    template_name = 'detailStudent.html'

    login_url = 'login'
Daniel W Bond's avatar
Daniel W Bond committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    def get_context_data(self, **kwargs):
        context = super(DetailStudent, self).get_context_data(**kwargs)

        # requesting_student = Student.objects.get(user=self.request.user)
        requesting_student_filter = Student.objects.filter(user=self.request.user)
        requesting_student = requesting_student_filter[0]

        def onFloor():
            floor_status = False
            if requesting_student.get_floor() == self.get_object().get_floor():
                floor_status = True
            return floor_status

        def inBuilding():
            floor_status = False
            if requesting_student.get_building() == self.get_object().get_building():
                floor_status = True
            return floor_status

        def shares():
            student_shares = False
            # if the student's privacy is floor and the requesting user is on their floor
            if(self.get_object().privacy == 'floor') and onFloor():
                student_shares = True
            # if the student's privacy is building and the requesting users is on their floor or in their building
            elif(self.get_object().privacy == 'building') and inBuilding():
                student_shares = True
            # if the student's privacy is set to 'student'
            elif(self.get_object().privacy == 'students'):
                student_shares = True
            return student_shares
           
        context['shares'] = shares()
        return context

87
class DetailCurrentStudent(LoginRequiredMixin, DetailView):
88
89
90
91
92
    model = Student
    context_object_name = 'student'
    template_name = 'detailStudent.html'

    login_url = 'login'
93
94
95
96

    def get_object(self):
        return get_object_or_404(Student, pk=self.request.session['_auth_user_id'])

97
98
99
# changeable student settings
class DetailStudentSettings(LoginRequiredMixin, DetailView):
    model = Student
100
101
102
103
    context_object_name = 'student'
    template_name = 'studentSettings.html'

    login_url = 'login'
104

105
class DetailCurrentStudentSettings(LoginRequiredMixin, DetailView):
106
    model = Student
107
108
109
110
    context_object_name = 'student'
    template_name = 'studentSettings.html'

    login_url = 'login'
111
112
113

    def get_object(self):
        return get_object_or_404(Student, pk=self.request.session['_auth_user_id'])