-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathviews.py
123 lines (105 loc) · 3.71 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
69
70
71
72
73
74
75
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import codecs
import datetime
from collections import defaultdict
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views.decorators.cache import cache_page
from django.views.decorators.http import require_safe
from django.views.generic.base import TemplateView
from apps.ldap.utils import add_officer, is_officer, is_root, user_exists
from .constants import DAYS_OF_WEEK, OH_TIMES
from .forms import OfficerCreationForm
from .models import (
Event,
Notice,
Officer,
Officership,
Person,
Politburo,
PolitburoMembership,
Semester,
Sponsor,
Sponsorship,
UcbClass,
)
# @cache_page(3 * 60)
def officers(request, semester_id=None):
if semester_id is None:
semester = Semester.objects.filter(current=True).get()
else:
semester = get_object_or_404(Semester, id=semester_id)
semesters = Semester.objects.exclude(id=semester.id)
officerships = (
Officership.objects.filter(semester=semester)
.select_related("officer__person__user")
.order_by("officer__person__user__first_name")
)
office_hours_calendar = [
[hour]
+ [officerships.filter(office_hours=day + " " + hour) for day in DAYS_OF_WEEK]
for hour in OH_TIMES
]
calendar = {
"days": DAYS_OF_WEEK,
"hours": OH_TIMES,
"contents": office_hours_calendar,
}
return render(
request,
"officers.html",
{
"officer_list": officerships,
"calendar": calendar,
"semester": semester,
"semesters": semesters,
},
)
def politburo(request, semester_id=None):
if semester_id is None:
semester = Semester.objects.filter(current=True).get()
else:
semester = get_object_or_404(Semester, id=semester_id)
semesters = Semester.objects.exclude(id=semester.id)
pb = (
PolitburoMembership.objects.filter(semester=semester)
.select_related("person__user")
.order_by("id")
)
return render(request, "politburo.html", {"pb": pb, "semesters": semesters})
def semester_ordering_key(semester):
return (semester.id[2:] + codecs.encode(semester.id[:2], "rot13"),)
def sponsors(request):
semesters = Semester.objects.all()
sponsorships = Sponsorship.objects.all()
sponsorships_by_semester = defaultdict(list)
for sponsorship in sponsorships:
sponsorships_by_semester[sponsorship.semester].append(sponsorship)
sponsorships_by_semester = sorted(
sponsorships_by_semester.items(),
key=lambda pair: semester_ordering_key(pair[0]),
reverse=True,
)
for semester, sponsorships in sponsorships_by_semester:
sponsorships.sort(key=lambda sponsorship: sponsorship.sponsor.name)
return render(
request, "sponsors.html", {"sponsorships_by_semester": sponsorships_by_semester}
)
def tutoring(request, semester_id=None):
if semester_id is None:
semester = Semester.objects.filter(current=True).get()
else:
semester = get_object_or_404(Semester, id=semester_id)
officerships = Officership.objects.select_related("officer").filter(
semester=semester
)
all_tutoring_subjects = UcbClass.objects.all()
tutors_by_subject = {}
for subject in all_tutoring_subjects:
tutors_by_subject[str(subject)] = [
officership.officer
for officership in officerships
if subject in officership.tutor_subjects.all()
]
return render(request, "tutoring.html", {"tutors_by_subject": tutors_by_subject})