diff options
author | Alban Gruin | 2017-10-03 09:28:49 +0200 |
---|---|---|
committer | Alban Gruin | 2017-10-03 09:28:49 +0200 |
commit | ae3358c1296a02352409910a9ffcc2307d5ea87a (patch) | |
tree | b47df3b99f639acc24d751cc0a6a73937c10069b /feeds.py | |
parent | 9641fc9bb6ef2897c0d70e6d5d5ed3f8e295dbec (diff) | |
parent | 175fcd46f56d8c1c5d10a50b401c09c25cfcdf82 (diff) |
Merge branch 'stable/0.10.z' into prod/pa1ch/0.y.zv0.10.0-pa1ch
Diffstat (limited to 'feeds.py')
-rw-r--r-- | feeds.py | 77 |
1 files changed, 57 insertions, 20 deletions
@@ -16,18 +16,20 @@ from django.core.exceptions import ObjectDoesNotExist from django.conf import settings from django.contrib.syndication.views import Feed -from django.db.models import Q +from django.db.models import Count, Max +from django.db.models.functions import ExtractWeek, ExtractYear from django.template import loader from django.urls import reverse from django.utils.feedgenerator import Atom1Feed, SyndicationFeed from icalendar import Calendar, Event -from .models import Course, Group, LastUpdate +from .models import Course, Group from .templatetags.rooms import format_rooms from .utils import get_current_or_next_week, get_week, group_courses -ICAL_NAMES = ["summary", "description", "location", "start", "dtstart", "dtend"] +ICAL_NAMES = ["uid", "summary", "description", "location", + "start", "dtstart", "dtend", "dtstamp"] class IcalFeedGenerator(SyndicationFeed): @@ -35,6 +37,7 @@ class IcalFeedGenerator(SyndicationFeed): def write(self, outfile, encoding): calendar = Calendar() + calendar.add("prodid", "-//celcatsanitizer//NONSGML v1.0//EN") calendar.add("version", "2.0") self.write_events(calendar) @@ -55,7 +58,9 @@ class IcalFeed(Feed): def get_object(self, request, year_slug, timetable_slug, group_slug): try: - group = Group.objects.get(timetable__year__slug=year_slug, timetable__slug=timetable_slug, slug=group_slug) + group = Group.objects.get(timetable__year__slug=year_slug, + timetable__slug=timetable_slug, + slug=group_slug) except: raise ObjectDoesNotExist else: @@ -68,55 +73,87 @@ class IcalFeed(Feed): return "" def items(self, obj): - return Course.objects.get_courses_for_group(obj).order_by("begin") + return Course.objects.get_courses_for_group(obj) def item_extra_kwargs(self, item): - return {"dtstart": item.begin, + return {"uid": "{0}@celcatsanitizer".format(item.id), + "dtstart": item.begin, "dtend": item.end, - "summary": item.name, + "dtstamp": item.last_update, + "summary": item.name + " (" + item.type + ")", "location": format_rooms(item.rooms.all())} class RSSFeed(Feed): def get_object(self, request, year_slug, timetable_slug, group_slug): year, week = get_current_or_next_week() + _, end = get_week(year, week) + try: - group = Group.objects.get(timetable__year__slug=year_slug, timetable__slug=timetable_slug, slug=group_slug) - updates = LastUpdate.objects.filter(Q(year=year, week__lte=week) | Q(year__lt=year), timetable__year__slug=year_slug, timetable__slug=timetable_slug).order_by("-year", "-week")[:5] + group = Group.objects.get(timetable__year__slug=year_slug, + timetable__slug=timetable_slug, + slug=group_slug) except: raise ObjectDoesNotExist else: + updates = Course.objects.get_courses_for_group(group, + begin__lt=end) \ + .annotate(year=ExtractYear("begin"), + week=ExtractWeek("begin")) \ + .values("year", "week") \ + .annotate(Count("year", distinct=True), + Max("last_update")) \ + .order_by("-year", "-week")[:5] return group, updates def link(self, obj): group = obj[0] - link = reverse("timetable", kwargs={"year_slug": group.timetable.year.slug, "timetable_slug": group.timetable.slug, "group_slug": group.slug}) + link = reverse("timetable", + kwargs={"year_slug": group.timetable.year.slug, + "timetable_slug": group.timetable.slug, + "group_slug": group.slug}) return link def title(self, obj): return "Emploi du temps du groupe {0}".format(obj[0]) def item_link(self, item): - group = item.group - return reverse("timetable", kwargs={"year_slug": group.timetable.year.slug, "timetable_slug": group.timetable.slug, "group_slug": group.slug, "year": item.year, "week": item.week}) + group = item["group"] + return reverse("timetable", + kwargs={"year_slug": group.timetable.year.slug, + "timetable_slug": group.timetable.slug, + "group_slug": group.slug, + "year": item["year"], + "week": item["week"]}) def item_description(self, item): - return item.description + return item["description"] + + def item_title(self, item): + return "{0}, semaine {1} de {2}".format(item["group"], + item["week"], + item["year"]) def item_updateddate(self, item): - return item.date + return item["last_update__max"] def items(self, obj): template = loader.get_template("timetable_common.html") group = obj[0] for update in obj[1]: - start, end = get_week(update.year, update.week) - courses = Course.objects.get_courses_for_group(group, begin__gte=start, begin__lt=end) - context = {"group": group, "courses": group_courses(courses), "last_update": update, "year": update.year, "week": update.week} - - update.group = group - update.description = template.render(context) + start, end = get_week(update["year"], update["week"]) + courses = Course.objects.get_courses_for_group(group, + begin__gte=start, + begin__lt=end) + context = {"group": group, + "courses": group_courses(courses), + "last_update": update["last_update__max"], + "year": update["year"], + "week": update["week"]} + + update["group"] = group + update["description"] = template.render(context) return obj[1] |