diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 7f495bd..6e20ff6 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -642,14 +642,14 @@ class Event(models.Model): self.download_image() if self.is_in_importation_process(): - # try to detect category - CategorisationRule.apply_rules(self) # try to detect location if not self.exact_location: for p in Place.objects.all(): if p.match(self): self.exact_location = p break + # try to detect category + CategorisationRule.apply_rules(self) def save(self, *args, **kwargs): self.prepare_save() @@ -1438,6 +1438,15 @@ class CategorisationRule(models.Model): default=False, ) + place = models.ForeignKey( + Place, + verbose_name=_("Place"), + help_text=_("Location from place"), + null=True, + on_delete=models.SET_NULL, + blank=True, + ) + class Meta: verbose_name = _("Categorisation rule") verbose_name_plural = _("Categorisation rules") @@ -1497,6 +1506,11 @@ class CategorisationRule(models.Model): if not result: return False + if self.place: + if not event.exact_location == self.place: + return False + + return True diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index d11712d..fdbfaa7 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -50,6 +50,7 @@ from .models import ( from django.utils import timezone from django.utils.html import escape from datetime import date, timedelta +from django.utils.timezone import datetime from django.db.models import Q from django.urls import reverse_lazy @@ -1519,14 +1520,14 @@ def apply_categorisation_rules(request): else: # first we check if events are not correctly categorised to_categorise = [] - for e in Event.objects.exclude(category=Category.get_default_category_id()): + for e in Event.objects.filter(start_day__gte=datetime.now()).exclude(category=Category.get_default_category_id()): c = CategorisationRule.match_rules(e) if c and c != e.category: to_categorise.append((e, c)) # then we apply rules on events without category nb = 0 - for e in Event.objects.filter(category=Category.get_default_category_id()): + for e in Event.objects.filter(start_day__gte=datetime.now()).filter(category=Category.get_default_category_id()): success = CategorisationRule.apply_rules(e) if success: nb += 1