aboutsummaryrefslogtreecommitdiff
path: root/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'views.py')
-rw-r--r--views.py55
1 files changed, 40 insertions, 15 deletions
diff --git a/views.py b/views.py
index 302b375..bd93712 100644
--- a/views.py
+++ b/views.py
@@ -13,33 +13,39 @@
# You should have received a copy of the GNU Affero General Public License
# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>.
+import datetime
+
from django.conf import settings
+from django.db.models import Max
+from django.http import Http404
from django.shortcuts import get_object_or_404, render
-from .models import Timetable, LastUpdate, Group, Course, Year
-from .utils import get_current_week, get_week, group_courses
+from .models import Timetable, Group, Course, Year
+from .utils import get_current_week, get_current_or_next_week, get_week, group_courses
def index(request):
years = Year.objects.order_by("name")
- return render(request, "index.html", {"years": years})
+ return render(request, "index.html", {"elements": years})
def mention_list(request, year_slug):
year = get_object_or_404(Year, slug=year_slug)
timetables = Timetable.objects.order_by("name").filter(year=year)
- return render(request, "mention_list.html", {"year": year, "timetables": timetables})
+ return render(request, "index.html", {"year": year, "elements": timetables})
-def group_list(request, year_slug, timetable_slug):
- timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug)
- groups = Group.objects.get_relevant_groups(timetable=timetable).order_by("name")
+def group_list_common(request, timetable, groups):
+ start, _ = get_week(*get_current_week())
+ end = start + datetime.timedelta(weeks=4)
- year, week = get_current_week()
- start, _ = get_week(year, week)
- groups_weeks = Course.objects.get_weeks(begin__gte=start, timetable=timetable).values("groups__mention", "groups__subgroup", "groups__td", "groups__tp", "year", "week")
+ groups_weeks = Course.objects.get_weeks(begin__gte=start, begin__lt=end, timetable=timetable) \
+ .values("groups__mention", "groups__subgroup",
+ "groups__td", "groups__tp", "year", "week")
for group in groups:
for group_week in groups_weeks:
- if group.corresponds_to(timetable.id, group_week["groups__mention"], group_week["groups__subgroup"], group_week["groups__td"], group_week["groups__tp"]):
+ if group.corresponds_to(timetable.id, group_week["groups__mention"],
+ group_week["groups__subgroup"], group_week["groups__td"],
+ group_week["groups__tp"]):
if not hasattr(group, "weeks"):
group.weeks = []
@@ -52,20 +58,39 @@ def group_list(request, year_slug, timetable_slug):
return render(request, "group_list.html", {"timetable": timetable, "groups": groups})
+def group_list(request, year_slug, timetable_slug):
+ timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug)
+ groups = Group.objects.get_relevant_groups(timetable=timetable).order_by("name")
+ return group_list_common(request, timetable, groups)
+
def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=None):
+ current_year, current_week = get_current_or_next_week()
+ is_old_timetable = False
+
if year is None or week is None:
- year, week = get_current_week()
+ year, week = current_year, current_week
+ elif (int(year), int(week)) < (current_year, current_week):
+ is_old_timetable = True
start, end = get_week(int(year), int(week))
timetable = get_object_or_404(Timetable, year__slug=year_slug, slug=timetable_slug)
group = get_object_or_404(Group, slug=group_slug, timetable=timetable)
- last_update = get_object_or_404(LastUpdate, timetable=timetable, week=week, year=year)
- courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end)
+
+ if group.children.count():
+ return group_list_common(request, timetable, Group.objects.get_relevant_children(group))
+
+ courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end) \
+ .annotate(Max("last_update"))
+ if courses.count() == 0:
+ raise Http404
grouped_courses = group_courses(courses)
- return render(request, "timetable.html", {"group": group, "courses": grouped_courses, "last_update": last_update.date, "year": year, "week": int(week)})
+ return render(request, "timetable.html", {"group": group, "courses": grouped_courses,
+ "last_update": courses.first().last_update__max,
+ "year": year, "week": int(week),
+ "is_old_timetable": is_old_timetable})
def contact(request):
return render(request, "contact.html", {"email": settings.ADMINS[0][1]})