From 0bc73445a719f6c8c3948d0b452dccb0d1442900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?= <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de> Date: Sun, 23 Oct 2022 23:44:46 +0200 Subject: [PATCH] Increase performance Move loop-based action code to mixin that will only be used when more complex actions should be performed Use queryset update function on existing actions to perform the necessary updating in only one db call instead of one query per entity Additionally, visualize manually_resolved status in admin interface of CV --- AKModel/admin.py | 2 +- AKModel/views.py | 52 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/AKModel/admin.py b/AKModel/admin.py index 1bc4a653..b26c9d63 100644 --- a/AKModel/admin.py +++ b/AKModel/admin.py @@ -349,7 +349,7 @@ class ConstraintViolationAdminForm(forms.ModelForm): @admin.register(ConstraintViolation) class ConstraintViolationAdmin(admin.ModelAdmin): - list_display = ['type', 'level', 'get_details'] + list_display = ['type', 'level', 'get_details', 'manually_resolved'] list_filter = ['event'] readonly_fields = ['timestamp'] form = ConstraintViolationAdminForm diff --git a/AKModel/views.py b/AKModel/views.py index a766d896..ea9435af 100644 --- a/AKModel/views.py +++ b/AKModel/views.py @@ -376,6 +376,7 @@ class ExportSlidesView(EventSlugMixin, IntermediateAdminView): class IntermediateAdminActionView(IntermediateAdminView, ABC): form_class = AdminIntermediateActionForm + entities = None def get_queryset(self, pks=None): if pks is None: @@ -388,34 +389,51 @@ class IntermediateAdminActionView(IntermediateAdminView, ABC): return initial def get_preview(self): - entities = self.get_queryset() - joined_entities = '\n'.join(str(e) for e in entities) + self.entities = self.get_queryset() + joined_entities = '\n'.join(str(e) for e in self.entities) return f"{self.confirmation_message}:\n\n {joined_entities}" def get_success_url(self): return reverse(f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_changelist") @abstractmethod - def perform_action(self, entity): + def action(self, form): pass def form_valid(self, form): - entities = self.get_queryset(pks=form.cleaned_data['pks']) - for entity in entities: - self.perform_action(entity) - entity.save() + self.entities = self.get_queryset(pks=form.cleaned_data['pks']) + self.action(form) messages.add_message(self.request, messages.SUCCESS, self.success_message) return super().form_valid(form) +class LoopActionMixin(ABC): + def action(self, form): + self.pre_action() + for entity in self.entities: + self.perform_action(entity) + entity.save() + self.post_action() + + @abstractmethod + def perform_action(self, entity): + pass + + def pre_action(self): + pass + + def post_action(self): + pass + + class CVMarkResolvedView(IntermediateAdminActionView): title = _('Mark Constraint Violations as manually resolved') model = ConstraintViolation confirmation_message = _("The following Constraint Violations will be marked as manually resolved") success_message = _("Constraint Violations marked as resolved") - def perform_action(self, entity): - entity.manually_resolved = True + def action(self, form): + self.entities.update(manually_resolved=True) class CVSetLevelViolationView(IntermediateAdminActionView): @@ -424,8 +442,8 @@ class CVSetLevelViolationView(IntermediateAdminActionView): confirmation_message = _("The following Constraint Violations will be set to level 'violation'") success_message = _("Constraint Violations set to level 'violation'") - def perform_action(self, entity): - entity.level = ConstraintViolation.ViolationLevel.VIOLATION + def action(self, form): + self.entities.update(level=ConstraintViolation.ViolationLevel.VIOLATION) class CVSetLevelWarningView(IntermediateAdminActionView): @@ -434,8 +452,8 @@ class CVSetLevelWarningView(IntermediateAdminActionView): confirmation_message = _("The following Constraint Violations will be set to level 'warning'") success_message = _("Constraint Violations set to level 'warning'") - def perform_action(self, entity): - entity.level = ConstraintViolation.ViolationLevel.WARNING + def action(self, form): + self.entities.update(level=ConstraintViolation.ViolationLevel.WARNING) class AKResetInterestView(IntermediateAdminActionView): @@ -444,8 +462,8 @@ class AKResetInterestView(IntermediateAdminActionView): confirmation_message = _("Interest of the following AKs will be set to not filled (-1):") success_message = _("Reset of interest in AKs successful.") - def perform_action(self, entity): - entity.interest = -1 + def action(self, form): + self.entities.update(interest=-1) class AKResetInterestCounterView(IntermediateAdminActionView): @@ -454,5 +472,5 @@ class AKResetInterestCounterView(IntermediateAdminActionView): confirmation_message = _("Interest counter of the following AKs will be set to 0:") success_message = _("AKs' interest counters set back to 0.") - def perform_action(self, entity): - entity.interest_counter = 0 + def action(self, form): + self.entities.update(interest_counter=0) -- GitLab