views.py 14.8 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 7
from django.contrib import messages
from django.utils.safestring import mark_safe
8
# third party imports
9
from braces.views import LoginRequiredMixin
10
from cas.views import login as cas_login
11
# imports from your apps
12 13
from .models import Student, Major, Room
from .forms import StudentUpdateForm, WelcomeNameForm
14

Daniel W Bond's avatar
Daniel W Bond committed
15

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 60 61 62 63 64 65 66 67 68
not_started = """Welcome to SRCT Roomlist! <a href="%s">Click here</a> to walk through
                 your profile setup."""

# 1 or 2
started = """Welcome back to SRCT Roomlist! It looks like you're not quite finished with
             setting up your profile. <a href="%s">Click here</a> to return to your
             welcome walkthrough."""

# 3
almost = """Welcome back to SRCT Roomlist! It looks like you're almost finished
            with setting up your profile. <a href="%s">Click here</a> to return
            to the last page of your welcome walkthrough."""

# walkthrough finished but Room is None
no_room = """It looks like you haven't set your room yet. Head to <a href="%s"> your
             settings page</a> to get that taken care of."""


def custom_cas_login(request, *args, **kwargs):
    response = cas_login(request, *args, **kwargs)
    # returns HttpResponseRedirect
    if request.user.is_authenticated():

        if request.user.student.completedName is False:
            rendered_url = reverse('welcomeName', args=[request.user.username])
            add_url = not_started % rendered_url
            messages.add_message(request, messages.INFO, mark_safe(add_url))

        elif request.user.student.completedPrivacy is False:
            rendered_url = reverse('welcomePrivacy', args=[request.user.username])
            add_url = started % rendered_url
            messages.add_message(request, messages.INFO, mark_safe(add_url))

        elif request.user.student.completedMajor is False:
            rendered_url = reverse('welcomeMajor', args=[request.user.username])
            add_url = started % rendered_url
            messages.add_message(request, messages.INFO, mark_safe(add_url))

        elif request.user.student.completedName is False:
            rendered_url = reverse('welcomeSocial', args=[request.user.username])
            add_url = started % rendered_url
            messages.add_message(request, messages.INFO, mark_safe(add_url))

        elif request.user.student.room is None:
            rendered_url = reverse('updateStudent', args=[request.user.username])
            add_url = started % rendered_url
            messages.add_message(request, messages.INFO, mark_safe(add_url))
        # eventually add a reminder if the privacy is set to students
        # one in ten change will display a reminder and link to change

    return response


69 70 71
# details about the student
class DetailStudent(LoginRequiredMixin, DetailView):
    model = Student
72 73 74 75
    context_object_name = 'student'
    template_name = 'detailStudent.html'

    login_url = 'login'
Daniel W Bond's avatar
Daniel W Bond committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    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
101 102
            # 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
103 104 105 106 107 108
            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
109

Daniel W Bond's avatar
Daniel W Bond committed
110 111 112
        context['shares'] = shares()
        return context

Daniel W Bond's avatar
Daniel W Bond committed
113

114
class DetailCurrentStudent(LoginRequiredMixin, DetailView):
115 116 117 118 119
    model = Student
    context_object_name = 'student'
    template_name = 'detailStudent.html'

    login_url = 'login'
120 121 122 123

    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
124

125 126 127
# changeable student settings
class DetailStudentSettings(LoginRequiredMixin, DetailView):
    model = Student
128 129 130 131
    context_object_name = 'student'
    template_name = 'studentSettings.html'

    login_url = 'login'
132

Daniel W Bond's avatar
Daniel W Bond committed
133

134
class DetailCurrentStudentSettings(LoginRequiredMixin, DetailView):
135
    model = Student
136 137 138 139
    context_object_name = 'student'
    template_name = 'studentSettings.html'

    login_url = 'login'
140 141 142

    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
143

144

145 146
# update a student, but FormView to allow name update on same page
class UpdateStudent(LoginRequiredMixin, FormView):
147
    template_name = 'updateStudent.html'
148
    form_class = StudentUpdateForm
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    login_url = 'login'

    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):
            return HttpResponseForbidden()
        else:
            return super(UpdateStudent, self).get(request, *args, **kwargs)

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    def get_context_data(self, **kwargs):
        context = super(UpdateStudent, self).get_context_data(**kwargs)

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

        def pk_or_none(me, obj):
            if obj is None:
                return None
            else:
                return obj.pk

        form = StudentUpdateForm(initial={'first_name': me.user.first_name,
                                        'last_name': me.user.last_name,
                                        'gender': me.gender,
                                        'room': pk_or_none(me, me.room),
                                        'privacy': me.privacy,
179 180
                                        'major': pk_or_none(me, me.major),
                                        'graduating_year' : me.graduating_year,})
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        context['my_form'] = form
        return context

    def form_valid(self, form):
        me = Student.objects.get(user=self.request.user)

        print form.data['room']
        print form.data['major']

        me.user.first_name = form.data['first_name']
        me.user.last_name = form.data['last_name']
        me.gender = form.data.getlist('gender')
        me.room = Room.objects.get(pk=form.data['room'])
        me.privacy = form.data['privacy']
        me.major = Major.objects.get(pk=form.data['major'])
196 197
        me.graduating_year = form.data['graduating_year']

198 199 200 201 202 203 204 205 206

        me.user.save()
        me.save()

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

    def get_success_url(self):
        return reverse('detail_student',
                       kwargs={'slug':self.request.user.username})
207 208 209

# welcome pages
class WelcomeName(LoginRequiredMixin, FormView):
210
    template_name = 'welcome_name.html'
211 212 213
    form_class = WelcomeNameForm
    login_url = 'login'

214 215 216 217 218 219 220 221 222 223 224 225
    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):
            return HttpResponseForbidden()
        else:
            return super(WelcomeName, self).get(request, *args, **kwargs)

Daniel W Bond's avatar
Daniel W Bond committed
226 227 228 229 230 231 232 233 234 235 236
    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
        
237
    def form_valid(self, form):
Daniel W Bond's avatar
Daniel W Bond committed
238
        me = Student.objects.get(user=self.request.user)
239

Daniel W Bond's avatar
Daniel W Bond committed
240 241 242 243 244 245 246 247 248
        me.user.first_name = form.data['first_name']
        me.user.last_name = form.data['last_name']

        me.gender = form.data.getlist('gender')

        me.completedName = True

        me.user.save()
        me.save()
249 250 251

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

252 253 254 255
    def get_success_url(self):
        return reverse('welcomePrivacy',
                       kwargs={'slug':self.request.user.username})

256 257 258 259 260

class WelcomePrivacy(LoginRequiredMixin, UpdateView):
    model = Student
    fields = ['room', 'privacy', ]
    context_object_name = 'student'
261
    template_name = 'welcome_privacy.html'
262 263 264

    login_url = 'login'

265 266 267 268 269 270 271 272 273 274 275 276
    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):
            return HttpResponseForbidden()
        else:
            return super(WelcomePrivacy, self).get(request, *args, **kwargs)

277 278 279 280 281 282 283 284
    def form_valid(self, form):
        self.obj = self.get_object()

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

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

285 286 287 288
    def get_success_url(self):
        return reverse('welcomeMajor',
                       kwargs={'slug':self.request.user.username})

289 290 291

class WelcomeMajor(LoginRequiredMixin, UpdateView):
    model = Student
292
    fields = ['major', 'graduating_year', ]
293
    context_object_name = 'student'
294
    template_name = 'welcome_major.html'
295 296 297

    login_url = 'login'

298 299 300 301 302 303 304 305 306 307 308 309
    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):
            return HttpResponseForbidden()
        else:
            return super(WelcomeMajor, self).get(request, *args, **kwargs)

310 311 312 313 314 315 316 317
    def form_valid(self, form):
        self.obj = self.get_object()

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

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

318 319 320 321
    def get_success_url(self):
        return reverse('welcomeSocial',
                       kwargs={'slug':self.request.user.username})

322 323 324 325

class WelcomeSocial(LoginRequiredMixin, DetailView):
    model = Student
    context_object_name = 'student'
326
    template_name = 'welcome_social.html'
327 328 329

    login_url = 'login'

330 331
    # push to the message queue

332 333 334 335 336 337 338 339 340 341 342 343
    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):
            return HttpResponseForbidden()
        else:
            return super(WelcomeSocial, self).get(request, *args, **kwargs)

344 345 346 347 348 349 350 351
    def form_valid(self, form):
        self.obj = self.get_object()

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

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

352 353 354 355
    def get_success_url(self):
        return reverse('detail_student',
                       kwargs={'slug':self.request.user.username})

356 357

# majors pages
Daniel W Bond's avatar
Daniel W Bond committed
358 359 360 361 362 363
class ListMajors(LoginRequiredMixin, ListView):
    model = Major
    queryset = Major.objects.all().order_by('name')
    context_object_name = 'majors'
    template_name = 'list_majors.html'

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    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