views.py 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.shortcuts import render
from django.views.generic import (View, DetailView, TemplateView)
# third party imports
from braces.views import LoginRequiredMixin
from accounts.models import Student

Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
10

11 12 13 14 15 16 17 18 19
class HomePageView(View):
    def get(self, request, *args, **kwargs):
        if self.request.user.is_authenticated():
            view = LandingPage.as_view()
            return view(request, *args, **kwargs)
        else:
            view = LandingPageNoAuth.as_view()
            return view(request, *args, **kwargs)

Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
20

21 22
class LandingPage(LoginRequiredMixin, TemplateView):
    template_name = 'landing.html'
Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
23

24 25 26
    def get_context_data(self, **kwargs):
        context = super(LandingPage, self).get_context_data(**kwargs)
        me = Student.objects.get(user=self.request.user)
Daniel W Bond's avatar
Daniel W Bond committed
27
        context['me'] = me
28 29 30

        # Create Dictionaries to store Students that meet criteria
        context["roomies"] = Student.objects.filter(room=me.room).exclude(user__username=me)
David Haynes's avatar
David Haynes committed
31
        context["floories"] = Student.objects.filter(room__floor=me.get_floor()).exclude(user__username=me).exclude(room=me.room).order_by('room')
32 33 34 35 36 37

        my_majors = tuple(me.major.all())
        students_by_major = {}
        for major in my_majors:
            students_by_major[major] = Student.objects.filter(major__in=[major]).exclude(user__username=me).order_by('?')[:8]
        context["majormates"] = students_by_major
38 39 40

        return context

Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
41

42 43
class LandingPageNoAuth(DetailView):
    template_name = 'index.html'
Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
44

45 46
    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)