From 7f1bbabebfd24d02c7e27b741fe6900f895b969b Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Fri, 29 Nov 2024 19:35:45 +0100 Subject: [PATCH] On enregistre l'auteur d'une modification Fix #228 --- src/agenda_culturel/celery.py | 8 +-- src/agenda_culturel/db_importer.py | 6 ++- src/agenda_culturel/forms.py | 6 ++- ...by_user_event_imported_by_user_and_more.py | 36 +++++++++++++ src/agenda_culturel/models.py | 52 ++++++++++++++++++- .../agenda_culturel/event-info-inc.html | 8 +-- .../agenda_culturel/event_form_moderate.html | 2 +- src/agenda_culturel/views.py | 11 ++-- 8 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 src/agenda_culturel/migrations/0123_event_created_by_user_event_imported_by_user_and_more.py diff --git a/src/agenda_culturel/celery.py b/src/agenda_culturel/celery.py index f3ffaeb..67672bb 100644 --- a/src/agenda_culturel/celery.py +++ b/src/agenda_culturel/celery.py @@ -291,7 +291,7 @@ def weekly_imports(self): run_recurrent_imports_from_list([imp.pk for imp in imports]) @app.task(base=ChromiumTask, bind=True) -def import_events_from_url(self, url, cat, tags, force=False): +def import_events_from_url(self, url, cat, tags, force=False, user_id=None): from .db_importer import DBImporterEvents from agenda_culturel.models import RecurrentImport, BatchImportation from agenda_culturel.models import Event, Category @@ -337,7 +337,7 @@ def import_events_from_url(self, url, cat, tags, force=False): json_events = json.dumps(events, default=str) # import events (from json) - success, error_message = importer.import_events(json_events) + success, error_message = importer.import_events(json_events, user_id) # finally, close task close_import_task(self.request.id, success, error_message, importer) @@ -354,14 +354,14 @@ def import_events_from_url(self, url, cat, tags, force=False): @app.task(base=ChromiumTask, bind=True) -def import_events_from_urls(self, urls_cat_tags): +def import_events_from_urls(self, urls_cat_tags, user_id=None): for ucat in urls_cat_tags: if ucat is not None: url = ucat[0] cat = ucat[1] tags = ucat[2] - import_events_from_url.delay(url, cat, tags) + import_events_from_url.delay(url, cat, tags, user_id=user_id) app.conf.beat_schedule = { diff --git a/src/agenda_culturel/db_importer.py b/src/agenda_culturel/db_importer.py index de38ce8..24e9c22 100644 --- a/src/agenda_culturel/db_importer.py +++ b/src/agenda_culturel/db_importer.py @@ -11,6 +11,7 @@ class DBImporterEvents: def __init__(self, celery_id): self.celery_id = celery_id self.error_message = "" + self.user_id = None self.init_result_properties() self.today = timezone.now().date().isoformat() @@ -34,9 +35,10 @@ class DBImporterEvents: def get_nb_removed_events(self): return self.nb_removed - def import_events(self, json_structure): + def import_events(self, json_structure, user_id=None): print(json_structure) self.init_result_properties() + self.user_id = user_id try: structure = json.loads(json_structure) @@ -95,7 +97,7 @@ class DBImporterEvents: def save_imported(self): self.db_event_objects, self.nb_updated, self.nb_removed = Event.import_events( - self.event_objects, remove_missing_from_source=self.url + self.event_objects, remove_missing_from_source=self.url, user_id=self.user_id ) def is_valid_event_structure(self, event): diff --git a/src/agenda_culturel/forms.py b/src/agenda_culturel/forms.py index f93b4db..314ad20 100644 --- a/src/agenda_culturel/forms.py +++ b/src/agenda_culturel/forms.py @@ -205,7 +205,11 @@ class EventForm(GroupFormMixin, ModelForm): "modified_date", "moderated_date", "import_sources", - "image" + "image", + "moderated_by_user", + "modified_by_user", + "created_by_user", + "imported_by_user" ] widgets = { "start_day": TextInput( diff --git a/src/agenda_culturel/migrations/0123_event_created_by_user_event_imported_by_user_and_more.py b/src/agenda_culturel/migrations/0123_event_created_by_user_event_imported_by_user_and_more.py new file mode 100644 index 0000000..a8359ce --- /dev/null +++ b/src/agenda_culturel/migrations/0123_event_created_by_user_event_imported_by_user_and_more.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.9 on 2024-11-29 18:18 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('agenda_culturel', '0122_alter_recurrentimport_processor'), + ] + + operations = [ + migrations.AddField( + model_name='event', + name='created_by_user', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='created_events', to=settings.AUTH_USER_MODEL, verbose_name='Author of the event creation'), + ), + migrations.AddField( + model_name='event', + name='imported_by_user', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='imported_events', to=settings.AUTH_USER_MODEL, verbose_name='Author of the last importation'), + ), + migrations.AddField( + model_name='event', + name='moderated_by_user', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='moderated_events', to=settings.AUTH_USER_MODEL, verbose_name='Author of the last moderation'), + ), + migrations.AddField( + model_name='event', + name='modified_by_user', + field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_DEFAULT, related_name='modified_events', to=settings.AUTH_USER_MODEL, verbose_name='Author of the last modification'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 03aeba3..40792ae 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -10,6 +10,7 @@ from colorfield.fields import ColorField from django_ckeditor_5.fields import CKEditor5Field from urllib.parse import urlparse from django.core.cache import cache +from django.contrib.auth.models import User import emoji import hashlib @@ -558,6 +559,39 @@ class Event(models.Model): modified_date = models.DateTimeField(blank=True, null=True) moderated_date = models.DateTimeField(blank=True, null=True) + created_by_user = models.ForeignKey( + User, + verbose_name=_("Author of the event creation"), + null=True, + default=None, + on_delete=models.SET_DEFAULT, + related_name="created_events" + ) + imported_by_user = models.ForeignKey( + User, + verbose_name=_("Author of the last importation"), + null=True, + default=None, + on_delete=models.SET_DEFAULT, + related_name="imported_events" + ) + modified_by_user = models.ForeignKey( + User, + verbose_name=_("Author of the last modification"), + null=True, + default=None, + on_delete=models.SET_DEFAULT, + related_name="modified_events" + ) + moderated_by_user = models.ForeignKey( + User, + verbose_name=_("Author of the last moderation"), + null=True, + default=None, + on_delete=models.SET_DEFAULT, + related_name="moderated_events" + ) + recurrence_dtstart = models.DateTimeField(editable=False, blank=True, null=True) recurrence_dtend = models.DateTimeField(editable=False, blank=True, null=True) @@ -692,6 +726,10 @@ class Event(models.Model): blank=True, ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.processing_user = None + def get_consolidated_end_day(self, intuitive=True): if intuitive: end_day = self.get_consolidated_end_day(False) @@ -896,6 +934,9 @@ class Event(models.Model): def set_no_modification_date_changed(self): self.no_modification_date_changed = True + def set_processing_user(self, user): + self.processing_user = user + def set_in_moderation_process(self): self.in_moderation_process = True @@ -906,12 +947,16 @@ class Event(models.Model): now = timezone.now() if not self.id: self.created_date = now + self.created_by_user = self.processing_user if self.is_in_importation_process(): self.imported_date = now + self.imported_by_user = self.processing_user if self.modified_date is None or not self.is_no_modification_date_changed(): self.modified_date = now + self.modified_by_user = self.processing_user if self.is_in_moderation_process(): self.moderated_date = now + self.moderated_by_user = self.processing_user def get_recurrence_at_date(self, year, month, day): dtstart = timezone.make_aware( @@ -1412,7 +1457,11 @@ class Event(models.Model): self.import_sources.append(source) # Limitation: the given events should not be considered similar one to another... - def import_events(events, remove_missing_from_source=None): + def import_events(events, remove_missing_from_source=None, user_id=None): + + user = None + if user_id: + user = User.objects.filter(pk=user_id).first() to_import = [] to_update = [] @@ -1439,6 +1488,7 @@ class Event(models.Model): # imported events should be updated event.set_in_importation_process() + event.set_processing_user(user) event.prepare_save() # check if the event has already be imported (using uuid) diff --git a/src/agenda_culturel/templates/agenda_culturel/event-info-inc.html b/src/agenda_culturel/templates/agenda_culturel/event-info-inc.html index fd968dc..4f1df6a 100644 --- a/src/agenda_culturel/templates/agenda_culturel/event-info-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/event-info-inc.html @@ -1,10 +1,10 @@