views.py 9.47 KB
Newer Older
1
# core django imports
Daniel W Bond's avatar
Daniel W Bond committed
2
from django.shortcuts import get_object_or_404
3
from django.http import HttpResponseForbidden
4
from django.views.generic import ListView, DetailView, UpdateView, FormView
5
from django.core.urlresolvers import reverse
6
# third party imports
7
from braces.views import LoginRequiredMixin
8
# imports from your apps
Daniel W Bond's avatar
Daniel W Bond committed
9
from .models import Student, Major
10
from .forms import WelcomeNameForm
11

Daniel W Bond's avatar
Daniel W Bond committed
12

13
14
15
# details about the student
class DetailStudent(LoginRequiredMixin, DetailView):
    model = Student
16
17
18
19
    context_object_name = 'student'
    template_name = 'detailStudent.html'

    login_url = 'login'
Daniel W Bond's avatar
Daniel W Bond committed
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

    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
Daniel W Bond's avatar
Daniel W Bond committed
45
46
            # if the student's privacy is building and the requesting users is
            # on their floor or in their building
Daniel W Bond's avatar
Daniel W Bond committed
47
48
49
50
51
52
            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
Daniel W Bond's avatar
Daniel W Bond committed
53

Daniel W Bond's avatar
Daniel W Bond committed
54
55
56
        context['shares'] = shares()
        return context

Daniel W Bond's avatar
Daniel W Bond committed
57

58
class DetailCurrentStudent(LoginRequiredMixin, DetailView):
59
60
61
62
63
    model = Student
    context_object_name = 'student'
    template_name = 'detailStudent.html'

    login_url = 'login'
64
65
66
67

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

Daniel W Bond's avatar
Daniel W Bond committed
68

69
70
71
# changeable student settings
class DetailStudentSettings(LoginRequiredMixin, DetailView):
    model = Student
72
73
74
75
    context_object_name = 'student'
    template_name = 'studentSettings.html'

    login_url = 'login'
76

Daniel W Bond's avatar
Daniel W Bond committed
77

78
class DetailCurrentStudentSettings(LoginRequiredMixin, DetailView):
79
    model = Student
80
81
82
83
    context_object_name = 'student'
    template_name = 'studentSettings.html'

    login_url = 'login'
84
85
86

    def get_object(self):
        return get_object_or_404(Student, pk=self.request.session['_auth_user_id'])
Daniel W Bond's avatar
Daniel W Bond committed
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

# update a student (students are *created* on first login via CAS)
class UpdateStudent(LoginRequiredMixin, UpdateView):
    model = Student
    fields = ['room', 'privacy', 'major', 'gender', ]
    context_object_name = 'student'
    template_name = 'updateStudent.html'

    login_url = 'login'

    # change to formview to support changing name

    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)

# use the same forms with different templates, views, and urls

# welcome pages
class WelcomeName(LoginRequiredMixin, FormView):
117
    template_name = 'welcome_name.html'
118
119
120
    form_class = WelcomeNameForm
    login_url = 'login'

Daniel W Bond's avatar
Daniel W Bond committed
121
122
123
124
125
126
127
128
129
130
131
132
    def get_context_data(self, **kwargs):
        context = super(WelcomeName, self).get_context_data(**kwargs)

        me = Student.objects.get(user=self.request.user)

        form = WelcomeNameForm(initial={'first_name': me.user.first_name,
                                        'last_name': me.user.last_name,
                                        'gender': me.gender, })
        context['my_form'] = form
        return context
        

133
    def form_valid(self, form):
Daniel W Bond's avatar
Daniel W Bond committed
134
        me = Student.objects.get(user=self.request.user)
135

Daniel W Bond's avatar
Daniel W Bond committed
136
137
138
139
140
141
142
143
144
145
146
        me.user.first_name = form.data['first_name']
        me.user.last_name = form.data['last_name']

        #for identity in form.data['gender']
            
        me.gender = form.data.getlist('gender')

        me.completedName = True

        me.user.save()
        me.save()
147
148
149

        return super(WelcomeName, self).form_valid(form)

150
151
152
153
    def get_success_url(self):
        return reverse('welcomePrivacy',
                       kwargs={'slug':self.request.user.username})

154
155
156
157
158

class WelcomePrivacy(LoginRequiredMixin, UpdateView):
    model = Student
    fields = ['room', 'privacy', ]
    context_object_name = 'student'
159
    template_name = 'welcome_privacy.html'
160
161
162

    login_url = 'login'

163
164
165
166
167
168
169
170
    def form_valid(self, form):
        self.obj = self.get_object()

        self.obj.completedPrivacy = True
        self.obj.save()

        return super(WelcomePrivacy, self).form_valid(form)

171
172
173
174
    def get_success_url(self):
        return reverse('welcomeMajor',
                       kwargs={'slug':self.request.user.username})

175
176
177
178
179

class WelcomeMajor(LoginRequiredMixin, UpdateView):
    model = Student
    fields = ['major', ]
    context_object_name = 'student'
180
    template_name = 'welcome_major.html'
181
182
183

    login_url = 'login'

184
185
186
187
188
189
190
191
192
    def form_valid(self, form):

        self.obj = self.get_object()

        self.obj.completedMajor = True
        self.obj.save()

        return super(WelcomeMajor, self).form_valid(form)

193
194
195
196
    def get_success_url(self):
        return reverse('welcomeSocial',
                       kwargs={'slug':self.request.user.username})

197
198
199
200

class WelcomeSocial(LoginRequiredMixin, DetailView):
    model = Student
    context_object_name = 'student'
201
    template_name = 'welcome_social.html'
202
203
204

    login_url = 'login'

205
206
    # push to the message queue

207
208
209
210
211
212
213
214
    def form_valid(self, form):
        self.obj = self.get_object()

        self.obj.completedSocial = True
        self.obj.save()

        return super(WelcomeSocial, self).form_valid(form)

215
216
217
218
    def get_success_url(self):
        return reverse('detail_student',
                       kwargs={'slug':self.request.user.username})

219
220

# majors pages
Daniel W Bond's avatar
Daniel W Bond committed
221
222
223
224
225
226
class ListMajors(LoginRequiredMixin, ListView):
    model = Major
    queryset = Major.objects.all().order_by('name')
    context_object_name = 'majors'
    template_name = 'list_majors.html'

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    login_url = 'login'


class DetailMajor(LoginRequiredMixin, DetailView):
    model = Major
    context_object_name = 'major'
    template_name = 'detail_major.html'

    login_url = 'login'

    def get_context_data(self, **kwargs):
        context = super(DetailMajor, self).get_context_data(**kwargs)
        me = Student.objects.get(user=self.request.user)

        students = Student.objects.filter(major=self.get_object()).order_by('room__floor__building__name', 'user__last_name', 'user__first_name')

        def onFloor(me, student):
            floor_status = False
            if me.get_floor() == student.get_floor():
                floor_status = True
            return floor_status

        def inBuilding(me, student):
            floor_status = False
            if me.get_building() == student.get_building():
                floor_status = True
            return floor_status

        aq_location_visible = []
        ra_location_visible = []
        sh_location_visible = []
        location_hidden = []

        aq_students = students.filter(room__floor__building__neighbourhood='aq')

        for student in aq_students:
            if student.privacy == u'students':
                aq_location_visible.append(student)
            elif (student.privacy == u'building') and inBuilding(me, student):
                aq_location_visible.append(student)
            elif (student.privacy == u'floor') and onFloor(me, student):
                aq_location_visible.append(student)
            else:
                location_hidden.append(student)

        ra_students = students.filter(room__floor__building__neighbourhood='ra')

        for student in ra_students:
            if student.privacy == u'students':
                ra_location_visible.append(student)
            elif (student.privacy == u'building') and inBuilding(me, student):
                ra_location_visible.append(student)
            elif (student.privacy == u'floor') and onFloor(me, student):
                ra_location_visible.append(student)
            else:
                location_hidden.append(student)

        sh_students = students.filter(room__floor__building__neighbourhood='sh')

        for student in sh_students:
            if student.privacy == u'students':
                sh_location_visible.append(student)
            elif (student.privacy == u'building') and inBuilding(me, student):
                sh_location_visible.append(student)
            elif (student.privacy == u'floor') and onFloor(me, student):
                sh_location_visible.append(student)
            else:
                location_hidden.append(student)

        context['aq_location_visible'] = aq_location_visible
        context['ra_location_visible'] = ra_location_visible
        context['sh_location_visible'] = sh_location_visible
        context['location_hidden'] = location_hidden

        return context