-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathviews.py
More file actions
158 lines (130 loc) · 4.68 KB
/
Copy pathviews.py
File metadata and controls
158 lines (130 loc) · 4.68 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import codecs
from collections import defaultdict
from datetime import datetime
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_TIME_MAP, 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,
"ohTimeMap": OH_TIME_MAP,
}
current_hour = datetime.now().strftime("%H")
current_timeslot = OH_TIME_MAP.get(int(current_hour))
current_minute = datetime.now().minute
current_hour_pct = (current_minute / 60) * 100
return render(
request,
"officers.html",
{
"officer_list": officerships,
"calendar": calendar,
"semester": semester,
"semesters": semesters,
"current_timeslot": current_timeslot,
"current_hour_pct": current_hour_pct,
},
)
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})
# acts as a sort key for semesters(terms) so they're in the correct chronological order (sp21 < fa22)
# term follows pattern <semester><year>, e.g. sp21, fa22
def term_sort_key(term):
term_id = term.id
semester = term_id[:2]
year = int(term_id[2:])
semester_order = 0 if semester == "sp" else 1
return year, semester_order
def archives(request):
semesters = Semester.objects.all()
sorted_semesters = sorted(semesters, key=term_sort_key, reverse=True)
return render(
request,
"archives.html",
context={
"sorted_semesters": sorted_semesters,
},
)