views.py
1 |
|
2 |
from . import bus |
3 |
from collections import OrderedDict |
4 |
from django.http import HttpResponseRedirect |
5 |
import datetime |
6 |
from django.urls import reverse # Why? |
7 |
from django.utils.translation import gettext as _ |
8 |
from .models import * |
9 |
from .forms import UserDataForm |
10 |
from .new_roster import create_roster_rows |
11 |
import administration |
12 |
from django.contrib.auth.decorators import login_required |
13 |
from django.contrib.auth import authenticate |
14 |
from django.contrib.auth import login as login_auth |
15 |
|
16 |
@login_required |
17 |
def roster(request, begin=None, end=None): |
18 |
"""Collects and renders the data that has to be displayed in the roster. |
19 |
|
20 |
The begin and end date can be specified. Only roster points in that range |
21 |
will be included in the response. If no begin and end are specified, it will |
22 |
take the current week as begin and end point. If it's |
23 |
weekend, it will take next week.""" |
24 |
|
25 |
# TODO Handle given begin and end |
26 |
context = dict() |
27 |
#context = {'money' : update_balance(None)} |
28 |
template = "administration/roster.djhtml" |
29 |
|
30 |
if begin is None or end is None: |
31 |
today = datetime.date.today() |
32 |
if today.isoweekday() in {6,7}: # Weekend |
33 |
begin = today + datetime.timedelta(days=8-today.isoweekday()) |
34 |
end = today + datetime.timedelta(days=13-today.isoweekday()) |
35 |
else: # Same week |
36 |
begin = today - datetime.timedelta(days=today.weekday()) |
37 |
end = today + datetime.timedelta(days=5-today.isoweekday()) |
38 |
else: # Changing regexes to date objects |
39 |
b = begin.split("-") |
40 |
e = end.split("-") |
41 |
begin = datetime.datetime(int(b[2]),int(b[1]),int(b[0])) |
42 |
end = datetime.datetime(int(e[2]),int(e[1]),int(e[0])) |
43 |
|
44 |
context['begin'] = begin |
45 |
context['end'] = end |
46 |
|
47 |
context['prev_begin'] = (begin - datetime.timedelta(days=7)).strftime("%d-%m-%Y") |
48 |
context['prev_end'] = (begin - datetime.timedelta(days=2)).strftime("%d-%m-%Y") |
49 |
context['next_begin'] = (end + datetime.timedelta(days=2)).strftime("%d-%m-%Y") |
50 |
context['next_end'] = (end + datetime.timedelta(days=7)).strftime("%d-%m-%Y") |
51 |
|
52 |
days = [begin] |
53 |
while (end-days[-1]).days != 0: |
54 |
# Human translation: Keep adding days until the last day in the array of |
55 |
# days is the same day as the last day the user wants to see the roster for. |
56 |
days.append(days[-1] + datetime.timedelta(days=1)) |
57 |
context['days'] = days |
58 |
|
59 |
# Collecting events |
60 |
course_events = CourseEvent.objects.filter(begin_time__gte=begin).filter(end_time__lte=end).order_by("begin_time") |
61 |
#university_events = UniversityEvent.objects.filter(begin_time__gte=begin).filter(end_time__lte=end) |
62 |
#study_events = StudyEvent.objects.filter(begin_time__gte=begin).filter(end_time__lte=end) |
63 |
#events = Event.objects.filter(begin_time__gte=begin).filter(end_time__lte=end) |
64 |
conflicts, table_code = create_roster_rows(course_events) |
65 |
|
66 |
context['time_blocks'] = table_code |
67 |
context['conflicts'] = conflicts |
68 |
#print(time_blocks) |
69 |
return render(request, template, context) |
70 |
# TODO Finish! |
71 |
|
72 |
def roster_ics(request, user_slug): |
73 |
template = "administration/roster.ics" |
74 |
context = dict() |
75 |
context['events'] = CourseEvent.objects.all() # FIXME: Filter to personal calendar items! |
76 |
return render(request, template, context) |
77 |
|
78 |
def index(request): |
79 |
template = "administration/index.djhtml" |
80 |
#context = {'money': update_balance(None)} |
81 |
context = dict() |
82 |
context['links'] = [ |
83 |
("administration-settings", |
84 |
_("Personal settings"), |
85 |
_("Edit your personal information, billing address, home address, and so on.")), |
86 |
("administration-curriculum", |
87 |
_("Curricula"), |
88 |
_("View all information related to your curricula, including exam results.<br />" |
89 |
"You can also change your current curriculum here, or request a change.")), |
90 |
("administration-buses", |
91 |
_("Bus departures"), |
92 |
_("If you're leaving Hasselt University, you can check the bus departures from " |
93 |
"your campus here.")), |
94 |
("administration-forms", |
95 |
_("Forms"), |
96 |
_("All forms for special services can be found on this page.")), |
97 |
("administration-rooms", |
98 |
_("Rooms"), |
99 |
_("Room occupancy, free rooms, properties, ... <br />" |
100 |
"All this and much more is available on this page.")), |
101 |
("administration-roster", |
102 |
_("Personal roster"), |
103 |
_("Everything about your roster and events at Hasselt University is available here.")), |
104 |
("administration-bulletin-board", |
105 |
_("Bulletin board"), |
106 |
_("From time to time, UHasselt publishes announcements regarding changes, events, ..." |
107 |
"<br />All publications are neatly organized here for easy reference.")), |
108 |
] |
109 |
|
110 |
return render(request, template, context) |
111 |
|
112 |
pass |
113 |
|
114 |
def pre_registration(request): |
115 |
user_data_form = UserDataForm() |
116 |
template = "administration/pre_registration.djhtml" |
117 |
context = dict() |
118 |
|
119 |
if request.method == 'POST': |
120 |
user_data_form = UserDataForm(request.POST) |
121 |
context['user_data_form'] = user_data_form |
122 |
if user_data_form.is_valid(): |
123 |
user_data_form.save() |
124 |
context['messsage'] = _("Your registration has been completed. You will receive an e-mail shortly.") |
125 |
else: |
126 |
context['messsage'] = _("The data you supplied had errors. Please review your submission.") |
127 |
else: |
128 |
context['user_data_form'] = UserDataForm(instance = user_data_form) |
129 |
|
130 |
return render(request, template, context) |
131 |
pass |
132 |
|
133 |
@login_required |
134 |
def settings(request): |
135 |
user_data = UserData.objects.get(user=request.user) |
136 |
user_data_form = UserDataForm(instance = user_data) |
137 |
template = "administration/settings.djhtml" |
138 |
context = dict() |
139 |
#context = {'money' : update_balance(None)} |
140 |
|
141 |
if request.method == 'POST': |
142 |
user_data_form = UserDataForm(request.POST, instance = user_data) |
143 |
context['user_data_form'] = user_data_form |
144 |
if user_data_form.is_valid(): |
145 |
user_data_form.save() |
146 |
context['messsage'] = _("Your settings were successfully updated.") |
147 |
else: |
148 |
context['messsage'] = _("The data you supplied had errors. Please review your submission.") |
149 |
else: |
150 |
context['user_data_form'] = UserDataForm(instance = user_data) |
151 |
|
152 |
return render(request, template, context) |
153 |
|
154 |
@login_required |
155 |
def bulletin_board(request): |
156 |
context = dict() |
157 |
#context = {'money' : update_balance(None)} |
158 |
context['exam_commission_decisions'] = ExamCommissionDecision.objects.filter(user=request.user) |
159 |
context['education_department_messages'] = EducationDepartmentMessages.objects.all() |
160 |
for item in context['education_department_messages']: |
161 |
print(item.text) |
162 |
template = "administration/bulletin_board.djhtml" |
163 |
return render(request, template, context) |
164 |
|
165 |
def jobs(request): |
166 |
context = dict() |
167 |
#context = {'money' : update_balance(None)} |
168 |
template = "administration/jobs.djhtml" |
169 |
#@context['decisions'] = ExamCommissionDecision.objects.filter(user=request.user) |
170 |
return render(request, template, context) |
171 |
|
172 |
|
173 |
@login_required |
174 |
def curriculum(request): |
175 |
context = dict() |
176 |
#context = {'money' : update_balance(None)} |
177 |
template = "administration/curriculum.djhtml" |
178 |
context['curricula'] = Curriculum.objects.filter(student=request.user) |
179 |
for item in context['curricula']: |
180 |
for co in item.course_programmes_results(): |
181 |
print(co) |
182 |
return render(request, template, context) |
183 |
|
184 |
def result(request): |
185 |
return render(request, template, context) |
186 |
|
187 |
@login_required |
188 |
def results(request): |
189 |
results = CourseResult.objects.filter(student=request.user) |
190 |
template = "administration/results.djhtml" |
191 |
# TODO |
192 |
return render(request, template, context) |
193 |
|
194 |
def forms(request): |
195 |
context = dict() |
196 |
#context = {'money' : update_balance(None)} |
197 |
template = "administration/forms.djhtml" |
198 |
return render(request, template, context) |
199 |
|
200 |
def user(request, slug_name): |
201 |
pass |
202 |
|
203 |
def rooms(request): |
204 |
context = dict() |
205 |
#context = {'money' : update_balance(None)} |
206 |
context['rooms'] = Room.objects.all() |
207 |
context['room_reservations'] = RoomReservation.objects.all() |
208 |
context['course_events'] = CourseEvent.objects.all() |
209 |
context['blocks'] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] |
210 |
|
211 |
# Collecting all rooms that are free for at least one two hours from now |
212 |
now = datetime.datetime.now(datetime.timezone.utc) |
213 |
end = now + datetime.timedelta(hours=2) |
214 |
free_rooms = dict() |
215 |
for room in context['rooms']: |
216 |
if room.reservation_possible(now, end): |
217 |
event = room.next_event(end) |
218 |
reservation = room.next_reservation(end) |
219 |
if event is None and reservation is None: |
220 |
free_rooms[room] = None |
221 |
elif reservation is not None: |
222 |
free_rooms[room] = event.begin_time |
223 |
elif event is not None: |
224 |
free_rooms[room] = reservation.begin_time |
225 |
elif event.begin_time < reservation.begin_time: |
226 |
free_rooms[room] = event.begin_time |
227 |
else: |
228 |
free_rooms[room] = reservation.begin_time |
229 |
context['free_rooms'] = free_rooms |
230 |
|
231 |
template = "administration/rooms.djhtml" |
232 |
return render(request, template, context) |
233 |
|
234 |
def room_detail(request, room): |
235 |
template = "administration/room_detail.djhtml" |
236 |
context = dict() |
237 |
#context = {'money' : update_balance(None)} |
238 |
room = Room.objects.get(name=room) |
239 |
context['room'] = room |
240 |
context['reservations'] = RoomReservation.objects.filter(room=room).filter(begin_time__gte=datetime.datetime.now()) |
241 |
context['course_events'] = CourseEvent.objects.filter(room=room).filter(begin_time__gte=datetime.datetime.now()) |
242 |
# Building the room occupancy of today: |
243 |
today = datetime.date.today() |
244 |
if today.isoweekday() in {6,7}: # Weekend |
245 |
today = today + datetime.timedelta(days=8-today.isoweekday()) |
246 |
|
247 |
context['days'] = [today] |
248 |
|
249 |
# Collecting events |
250 |
course_events = CourseEvent.objects.filter(room=room).filter(begin_time__date=today) |
251 |
print(course_events) |
252 |
#university_events = UniversityEvent.objects.filter(begin_time__gte=begin).filter(end_time__lte=end) |
253 |
#study_events = StudyEvent.objects.filter(begin_time__gte=begin).filter(end_time__lte=end) |
254 |
#events = Event.objects.filter(begin_time__gte=begin).filter(end_time__lte=end) |
255 |
|
256 |
conflicts, table_code = create_roster_rows(course_events) |
257 |
context['time_blocks'] = table_code |
258 |
context['conflicts'] = conflicts |
259 |
print(context['time_blocks']) |
260 |
return render(request, template, context) |
261 |
|
262 |
def buses(request): |
263 |
context = dict() |
264 |
context["departures_diepenbeek"] = bus.arrivals(["401474","401475"]) |
265 |
#for testing: context["departures_diepenbeek"] = bus.arrivals(["205919","205916","205915"]) |
266 |
context["departures_dusart"] = bus.arrivals(["403041","403040"]) |
267 |
template = 'administration/buses.djhtml' |
268 |
return render(request, template, context) |
269 |
|
270 |
|
271 |
|
272 |
|
273 |
def login(request): |
274 |
context = dict() |
275 |
#context = {'money' : update_balance(None)} |
276 |
if request.method == "POST": |
277 |
name = request.POST['name'] |
278 |
passphrase = request.POST['pass'] |
279 |
user = authenticate(username=name, password=passphrase) |
280 |
if user is not None: # The user was successfully authenticated |
281 |
print("YA") |
282 |
login_auth(request, user) |
283 |
return HttpResponseRedirect(request.POST['next']) |
284 |
else: # User credentials were wrong |
285 |
context['next'] = request.POST['next'] |
286 |
context['message'] = _("The given credentials were not correct.") |
287 |
else: |
288 |
context['next'] = request.GET.get('next', None) |
289 |
if context['next'] is None: |
290 |
context['next'] = reverse('administration-index') |
291 |
|
292 |
template = 'administration/login.djhtml' |
293 |
|
294 |
return render(request, template, context) |
295 |