From 8861bddccf458846e12e679704a8514ec2dc2510 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 3 Feb 2017 10:43:13 +0100 Subject: Ajout d'un système de renommage de groupe à la volée pour corriger les défauts de nom de la source --- admin.py | 4 ++-- management/commands/_private.py | 11 ++++++----- models.py | 6 +++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/admin.py b/admin.py index 95b4fd7..509ed42 100644 --- a/admin.py +++ b/admin.py @@ -17,11 +17,11 @@ class LastUpdateAdmin(admin.ModelAdmin): @admin.register(Group) class GroupAdmin(admin.ModelAdmin): fieldsets = ( - (None, {"fields": ("name", "timetable",)}), + (None, {"fields": ("name", "celcat_name", "timetable",)}), ("Groupes", {"fields": ("mention", "subgroup", "td", "tp",)}),) list_display = ("name", "timetable",) list_filter = ("timetable__name",) - readonly_fields = ("mention", "subgroup", "td", "tp",) + readonly_fields = ("celcat_name", "mention", "subgroup", "td", "tp",) @admin.register(Subscription) diff --git a/management/commands/_private.py b/management/commands/_private.py index f5a3ebc..3ff9316 100644 --- a/management/commands/_private.py +++ b/management/commands/_private.py @@ -29,14 +29,14 @@ def delete_courses_in_week(timetable, year, week): Course.objects.filter(begin__gte=start, begin__lt=end, timetable=timetable).delete() -def get_from_db_or_create(cls, name, timetable=None): - obj = cls.objects.all().filter(name=name) +def get_from_db_or_create(cls, timetable=None, **kwargs): + obj = cls.objects.all().filter(**kwargs) if timetable is not None: obj = obj.filter(timetable=timetable) obj = obj.first() if obj is None: - obj = cls(name=name) + obj = cls(**kwargs) obj.timetable = timetable obj.save() @@ -61,7 +61,8 @@ def get_events(soup, weeks, year, week, timetable): end = add_time(date, datetime.datetime.strptime( event.endtime.text, "%H:%M")) - groups = [get_from_db_or_create(Group, item.text, timetable) + groups = [get_from_db_or_create(Group, timetable, + celcat_name=item.text) for item in event.resources.group.find_all("item")] if event.notes is not None: @@ -79,7 +80,7 @@ def get_events(soup, weeks, year, week, timetable): type_ = event.category.text if event.resources.room is not None: - rooms = [get_from_db_or_create(Room, item.text) + rooms = [get_from_db_or_create(Room, name=item.text) for item in event.resources.room.find_all("item")] yield title, type_, groups, rooms, notes, begin, end diff --git a/models.py b/models.py index a2d4281..536c3b8 100644 --- a/models.py +++ b/models.py @@ -39,6 +39,7 @@ class LastUpdate(models.Model): class Group(models.Model): name = models.CharField(max_length=255, verbose_name="nom") + celcat_name = models.CharField(max_length=255, verbose_name="nom dans Celcat") timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE, verbose_name="emploi du temps") mention = models.CharField(max_length=32) @@ -69,13 +70,16 @@ class Group(models.Model): except: print("Malformed name: {0}. Ignoring".format(self.name)) finally: + if self.name is None: + self.name = self.celcat_name + self.slug = slugify(self.name) super(Group, self).save() class Meta: index_together = ("mention", "subgroup", "td", "tp",) - unique_together = ("name", "timetable",) + unique_together = (("name", "timetable",), ("celcat_name", "timetable",),) verbose_name = "groupe" verbose_name_plural = "groupes" -- cgit v1.2.1