aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2017-09-24 17:16:10 +0200
committerAlban Gruin2017-09-24 17:16:10 +0200
commite30b0c673cae607175c5bae6df6e87d8b100bdf3 (patch)
tree2ffc269ad5e56ff93e2ecd3bded5843a585c4fe7
parent77748ec01d1ef59d8436597a9b52675b309e1c4d (diff)
Suppression du modèle LastUpdate, utilisation du champ last_update à
la place.
-rw-r--r--admin.py8
-rw-r--r--feeds.py16
-rw-r--r--management/commands/cleancourses.py4
-rw-r--r--management/commands/timetables.py18
-rw-r--r--models.py18
-rw-r--r--views.py12
6 files changed, 26 insertions, 50 deletions
diff --git a/admin.py b/admin.py
index 4a238be..8a409e3 100644
--- a/admin.py
+++ b/admin.py
@@ -14,7 +14,7 @@
# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>.
from django.contrib import admin
-from .models import Timetable, LastUpdate, Group, Room, Course, Year
+from .models import Timetable, Group, Room, Course, Year
@admin.register(Year)
@@ -32,12 +32,6 @@ class TimetableAdmin(admin.ModelAdmin):
ordering = ("year", "name",)
-@admin.register(LastUpdate)
-class LastUpdateAdmin(admin.ModelAdmin):
- list_display = ("timetable", "week", "year", "date", "updated_at",)
- list_filter = ("timetable__name",)
-
-
@admin.register(Group)
class GroupAdmin(admin.ModelAdmin):
fieldsets = (
diff --git a/feeds.py b/feeds.py
index 6dfb851..d7f3ae3 100644
--- a/feeds.py
+++ b/feeds.py
@@ -16,7 +16,7 @@
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, Q
from django.db.models.functions import ExtractWeek, ExtractYear
from django.template import loader
from django.urls import reverse
@@ -24,7 +24,7 @@ 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
@@ -70,13 +70,13 @@ class IcalFeed(Feed):
return ""
def items(self, obj):
- return Course.objects.get_courses_for_group(obj).annotate(year=ExtractYear("begin"), week=ExtractWeek("begin"))
+ return Course.objects.get_courses_for_group(obj)
def item_extra_kwargs(self, item):
return {"uid": "{0}@celcatsanitizer".format(item.id),
"dtstart": item.begin,
"dtend": item.end,
- "dtstamp": LastUpdate.objects.get(timetable=item.timetable, year=item.year, week=item.week).updated_at,
+ "dtstamp": item.last_update,
"summary": item.name,
"location": format_rooms(item.rooms.all())}
@@ -84,12 +84,14 @@ class IcalFeed(Feed):
class RSSFeed(Feed):
def get_object(self, request, year_slug, timetable_slug, group_slug):
year, week = get_current_or_next_week()
+ begin, 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]
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):
@@ -115,9 +117,9 @@ class RSSFeed(Feed):
group = obj[0]
for update in obj[1]:
- start, end = get_week(update.year, update.week)
+ 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}
+ 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)
diff --git a/management/commands/cleancourses.py b/management/commands/cleancourses.py
index ca2ef94..f6041ef 100644
--- a/management/commands/cleancourses.py
+++ b/management/commands/cleancourses.py
@@ -15,7 +15,7 @@
from django.core.management.base import BaseCommand
from django.db import transaction
-from edt.models import Course, Group, LastUpdate
+from edt.models import Course, Group
class Command(BaseCommand):
@@ -29,10 +29,8 @@ class Command(BaseCommand):
if options["timetable"] is None:
Course.objects.all().delete()
Group.objects.all().delete()
- LastUpdate.objects.all().delete()
else:
Course.objects.filter(timetable__id__in=options["timetable"]).delete()
Group.objects.filter(timetable__id__in=options["timetable"]).delete()
- LastUpdate.objects.filter(timetable__id__in=options["timetable"]).delete()
self.stdout.write(self.style.SUCCESS("Done."))
diff --git a/management/commands/timetables.py b/management/commands/timetables.py
index c82b0e4..45a57ee 100644
--- a/management/commands/timetables.py
+++ b/management/commands/timetables.py
@@ -17,20 +17,19 @@ from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils import timezone
-from edt.models import Timetable, LastUpdate, Course
+from edt.models import Timetable, Course
+from edt.utils import get_week
from ._private import delete_courses_in_week, get_events, get_update_date, get_weeks, get_xml
import datetime
@transaction.atomic
def process_timetable_week(timetable, year, week, soup, weeks_in_soup):
- last_update_date = None
+ begin, end = get_weeks(year, week)
+
+ last_update_date = Course.objects.filter(begin__gte=start, begin__lt=end).aggregate(Max("last_update"))["last_update__max"]
new_update_date = get_update_date(soup)
- try:
- last_update = LastUpdate.objects.get(timetable=timetable, year=year, week=week)
- last_update_date = last_update.updated_at
- except:
- last_update = LastUpdate(timetable=timetable, year=year, week=week)
+ updated_at = timezone.make_aware(datetime.datetime.now())
if last_update_date is not None and new_update_date is not None and \
last_update_date >= new_update_date:
@@ -43,6 +42,7 @@ def process_timetable_week(timetable, year, week, soup, weeks_in_soup):
course.name = name
course.type = type_
course.notes = notes
+ course.updated_at = updated_at
course.groups.add(*groups)
if rooms is not None:
@@ -50,10 +50,6 @@ def process_timetable_week(timetable, year, week, soup, weeks_in_soup):
course.save()
- last_update.date = timezone.make_aware(datetime.datetime.now())
- last_update.updated_at = new_update_date
- last_update.save()
-
def process_timetable(timetable, year, weeks):
soup = get_xml(timetable.url)
weeks_in_soup = get_weeks(soup)
diff --git a/models.py b/models.py
index 4a25b01..5128c8a 100644
--- a/models.py
+++ b/models.py
@@ -62,24 +62,6 @@ class Timetable(SlugModel):
verbose_name_plural = "emplois du temps"
-class LastUpdate(models.Model):
- timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, verbose_name="emploi du temps")
- week = models.IntegerField(verbose_name="semaine")
- year = models.IntegerField(verbose_name="année")
- date = models.DateTimeField(verbose_name="date de mise à jour")
-
- updated_at = models.DateTimeField(verbose_name="date de publication", null=True)
-
- def __str__(self):
- return "{0}, semaine {1} de {2}".format(self.timetable, self.week, self.year)
-
-
- class Meta:
- unique_together = ("timetable", "week", "year",)
- verbose_name = "dernière mise à jour"
- verbose_name_plural = "dernières mises à jour"
-
-
class GroupManager(Manager):
def get_relevant_groups(self, *args, **criteria):
return self.get_queryset().filter(*args, **criteria).annotate(children_count=Count("children")).filter(children_count=0)
diff --git a/views.py b/views.py
index 302b375..eb6a7ac 100644
--- a/views.py
+++ b/views.py
@@ -14,9 +14,11 @@
# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>.
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 .models import Timetable, Group, Course, Year
from .utils import get_current_week, get_week, group_courses
def index(request):
@@ -60,12 +62,14 @@ def timetable(request, year_slug, timetable_slug, group_slug, year=None, week=No
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)
+
+ 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)})
def contact(request):
return render(request, "contact.html", {"email": settings.ADMINS[0][1]})