views.py 3.42 KB
Newer Older
1
2
3
4
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.shortcuts import render
5
6
7
8
from django.views.generic import View, DetailView, TemplateView, RedirectView
from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
from django.core.urlresolvers import reverse
9
10
# third party imports
from braces.views import LoginRequiredMixin
11
from accounts.models import Student, Major
12
13
# imports from your apps
from housing.views import shadowbanning
14

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

16
17
18
19
20
21
22
23
24
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
25

26
27
class LandingPage(LoginRequiredMixin, TemplateView):
    template_name = 'landing.html'
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
28

29
30
31
    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
32
        context['me'] = me
33
34

        # Create Dictionaries to store Students that meet criteria
35
36
        roomies = Student.objects.filter(room=me.room).exclude(user=me.user)
        floories = Student.objects.filter(room__floor=me.get_floor()).exclude(user=me.user).exclude(room=me.room).order_by('room')
37
38
39
40

        my_majors = tuple(me.major.all())
        students_by_major = {}
        for major in my_majors:
41
42
43
            major_students = Student.objects.filter(major__in=[major]).exclude(user=me.user).order_by('?')[:8]
            censored_major = shadowbanning(me, major_students)
            students_by_major[major] = censored_major
44

45
46
        context["roomies"] = shadowbanning(me, roomies)
        context["floories"] = shadowbanning(me, floories)
47
        context["majormates"] = students_by_major
48
49
        return context

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

51
52
class LandingPageNoAuth(DetailView):
    template_name = 'index.html'
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
53

54
55
    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78


# redirecting urls
class RedirectSettings(RedirectView):

    permanent = True

    # we're not including loginrequired, because that's already part of where we're
    # sending the student to-- this is just about changing the url
    def get_redirect_url(self, *args, **kwargs):
        slug = self.request.user.username
        return reverse('update_student',
                       kwargs={'slug': slug})


class RedirectSlug(RedirectView):

    permanent = True

    # we're not including loginrequired, because that's already part of where we're
    # sending the student to-- this is just about changing the url
    def get_redirect_url(self, *args, **kwargs):
        current_url = self.request.get_full_path()
79
        # print(self.request)
80
81
        # [u'', u'gmason']
        slug = current_url.split('/')[1]
82
        # print(slug)
83
84

        try:
85
            # print('trying student')
86
87
88
89
            student = Student.objects.get(user__username=slug)
            return reverse('detail_student',
                           kwargs={'slug': slug})
        except ObjectDoesNotExist:
90
            # print('trying major')
91
92
93
94
95
96
            try:
                major = Major.objects.get(slug=slug)
                return reverse('detail_major',
                               kwargs={'slug': slug})
            except ObjectDoesNotExist:
                raise Http404
97
98
        else:
            return Http404