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

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

19
20
21
22
23
24
25
26
27
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
28

29
30
class LandingPage(LoginRequiredMixin, TemplateView):
    template_name = 'landing.html'
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
31

32
33
34
    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
35
        context['me'] = me
36
37

        # Create Dictionaries to store Students that meet criteria
38
39
        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')
40
41
42
43

        my_majors = tuple(me.major.all())
        students_by_major = {}
        for major in my_majors:
44
45
46
            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
47

48
49
        context["roomies"] = shadowbanning(me, roomies)
        context["floories"] = shadowbanning(me, floories)
50
        context["majormates"] = students_by_major
51
52
        return context

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

54
55
class LandingPageNoAuth(DetailView):
    template_name = 'index.html'
Daniel W Bond's avatar
pep8 me    
Daniel W Bond committed
56

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


# 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()
82
        # print(self.request)
83
84
        # [u'', u'gmason']
        slug = current_url.split('/')[1]
85
        # print(slug)
86
87

        try:
88
            # print('trying student')
89
90
91
92
            student = Student.objects.get(user__username=slug)
            return reverse('detail_student',
                           kwargs={'slug': slug})
        except ObjectDoesNotExist:
93
            # print('trying major')
94
95
96
97
98
99
            try:
                major = Major.objects.get(slug=slug)
                return reverse('detail_major',
                               kwargs={'slug': slug})
            except ObjectDoesNotExist:
                raise Http404
100
101
        else:
            return Http404
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121


class BlockingMiddleware(object):
    """Prevent students who have been blocked from accessing any
       login-protected content.

       This is implemented as middleware to immediately force a user's logout,
       not just to block them once they've logged out and are logging back in."""

    def process_view(self, request, view_func, view_args, view_kwargs):

        if request.user.is_authenticated():
            if request.user.student.blocked:
                msg = """You are not permitted to use Roomlist. Please contact us at
                         <a href="mailto:roomlist@lists.srct.gmu.edu">
                         roomlist@lists.srct.gmu.edu</a>
                         if you believe this to be an error."""
                messages.add_message(request, messages.ERROR, mark_safe(msg))

                logout(request)