views.py 1.87 KB
Newer Older
1
2
3
4
5
6
7
8
# 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
9
10
# imports from your apps
from housing.views import shadowbanning
11

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

13
14
15
16
17
18
19
20
21
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
22

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

26
27
28
    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
29
        context['me'] = me
30
31

        # Create Dictionaries to store Students that meet criteria
Daniel W Bond's avatar
Daniel W Bond committed
32
33
        roomies = Student.objects.filter(room=me.room).exclude(user__username=me)
        floories = Student.objects.filter(room__floor=me.get_floor()).exclude(user__username=me).exclude(room=me.room).order_by('room')
34
35
36
37
38

        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]
Daniel W Bond's avatar
Daniel W Bond committed
39
        majormates = students_by_major
40

41
42
43
        context["roomies"] = shadowbanning(me, roomies)
        context["floories"] = shadowbanning(me, floories)
        context["majormates"] = shadowbanning(me, majormates)
44
45
        return context

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

47
48
class LandingPageNoAuth(DetailView):
    template_name = 'index.html'
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
49

50
51
    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)