Skip to content
Snippets Groups Projects
Commit 0bc73445 authored by Benjamin Hättasch's avatar Benjamin Hättasch
Browse files

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
parent 19e46342
No related branches found
No related tags found
No related merge requests found
...@@ -349,7 +349,7 @@ class ConstraintViolationAdminForm(forms.ModelForm): ...@@ -349,7 +349,7 @@ class ConstraintViolationAdminForm(forms.ModelForm):
@admin.register(ConstraintViolation) @admin.register(ConstraintViolation)
class ConstraintViolationAdmin(admin.ModelAdmin): class ConstraintViolationAdmin(admin.ModelAdmin):
list_display = ['type', 'level', 'get_details'] list_display = ['type', 'level', 'get_details', 'manually_resolved']
list_filter = ['event'] list_filter = ['event']
readonly_fields = ['timestamp'] readonly_fields = ['timestamp']
form = ConstraintViolationAdminForm form = ConstraintViolationAdminForm
......
...@@ -376,6 +376,7 @@ class ExportSlidesView(EventSlugMixin, IntermediateAdminView): ...@@ -376,6 +376,7 @@ class ExportSlidesView(EventSlugMixin, IntermediateAdminView):
class IntermediateAdminActionView(IntermediateAdminView, ABC): class IntermediateAdminActionView(IntermediateAdminView, ABC):
form_class = AdminIntermediateActionForm form_class = AdminIntermediateActionForm
entities = None
def get_queryset(self, pks=None): def get_queryset(self, pks=None):
if pks is None: if pks is None:
...@@ -388,34 +389,51 @@ class IntermediateAdminActionView(IntermediateAdminView, ABC): ...@@ -388,34 +389,51 @@ class IntermediateAdminActionView(IntermediateAdminView, ABC):
return initial return initial
def get_preview(self): def get_preview(self):
entities = self.get_queryset() self.entities = self.get_queryset()
joined_entities = '\n'.join(str(e) for e in entities) joined_entities = '\n'.join(str(e) for e in self.entities)
return f"{self.confirmation_message}:\n\n {joined_entities}" return f"{self.confirmation_message}:\n\n {joined_entities}"
def get_success_url(self): def get_success_url(self):
return reverse(f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_changelist") return reverse(f"admin:{self.model._meta.app_label}_{self.model._meta.model_name}_changelist")
@abstractmethod @abstractmethod
def perform_action(self, entity): def action(self, form):
pass pass
def form_valid(self, form): def form_valid(self, form):
entities = self.get_queryset(pks=form.cleaned_data['pks']) self.entities = self.get_queryset(pks=form.cleaned_data['pks'])
for entity in entities: self.action(form)
self.perform_action(entity)
entity.save()
messages.add_message(self.request, messages.SUCCESS, self.success_message) messages.add_message(self.request, messages.SUCCESS, self.success_message)
return super().form_valid(form) 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): class CVMarkResolvedView(IntermediateAdminActionView):
title = _('Mark Constraint Violations as manually resolved') title = _('Mark Constraint Violations as manually resolved')
model = ConstraintViolation model = ConstraintViolation
confirmation_message = _("The following Constraint Violations will be marked as manually resolved") confirmation_message = _("The following Constraint Violations will be marked as manually resolved")
success_message = _("Constraint Violations marked as resolved") success_message = _("Constraint Violations marked as resolved")
def perform_action(self, entity): def action(self, form):
entity.manually_resolved = True self.entities.update(manually_resolved=True)
class CVSetLevelViolationView(IntermediateAdminActionView): class CVSetLevelViolationView(IntermediateAdminActionView):
...@@ -424,8 +442,8 @@ class CVSetLevelViolationView(IntermediateAdminActionView): ...@@ -424,8 +442,8 @@ class CVSetLevelViolationView(IntermediateAdminActionView):
confirmation_message = _("The following Constraint Violations will be set to level 'violation'") confirmation_message = _("The following Constraint Violations will be set to level 'violation'")
success_message = _("Constraint Violations set to level 'violation'") success_message = _("Constraint Violations set to level 'violation'")
def perform_action(self, entity): def action(self, form):
entity.level = ConstraintViolation.ViolationLevel.VIOLATION self.entities.update(level=ConstraintViolation.ViolationLevel.VIOLATION)
class CVSetLevelWarningView(IntermediateAdminActionView): class CVSetLevelWarningView(IntermediateAdminActionView):
...@@ -434,8 +452,8 @@ class CVSetLevelWarningView(IntermediateAdminActionView): ...@@ -434,8 +452,8 @@ class CVSetLevelWarningView(IntermediateAdminActionView):
confirmation_message = _("The following Constraint Violations will be set to level 'warning'") confirmation_message = _("The following Constraint Violations will be set to level 'warning'")
success_message = _("Constraint Violations set to level 'warning'") success_message = _("Constraint Violations set to level 'warning'")
def perform_action(self, entity): def action(self, form):
entity.level = ConstraintViolation.ViolationLevel.WARNING self.entities.update(level=ConstraintViolation.ViolationLevel.WARNING)
class AKResetInterestView(IntermediateAdminActionView): class AKResetInterestView(IntermediateAdminActionView):
...@@ -444,8 +462,8 @@ class AKResetInterestView(IntermediateAdminActionView): ...@@ -444,8 +462,8 @@ class AKResetInterestView(IntermediateAdminActionView):
confirmation_message = _("Interest of the following AKs will be set to not filled (-1):") confirmation_message = _("Interest of the following AKs will be set to not filled (-1):")
success_message = _("Reset of interest in AKs successful.") success_message = _("Reset of interest in AKs successful.")
def perform_action(self, entity): def action(self, form):
entity.interest = -1 self.entities.update(interest=-1)
class AKResetInterestCounterView(IntermediateAdminActionView): class AKResetInterestCounterView(IntermediateAdminActionView):
...@@ -454,5 +472,5 @@ class AKResetInterestCounterView(IntermediateAdminActionView): ...@@ -454,5 +472,5 @@ class AKResetInterestCounterView(IntermediateAdminActionView):
confirmation_message = _("Interest counter of the following AKs will be set to 0:") confirmation_message = _("Interest counter of the following AKs will be set to 0:")
success_message = _("AKs' interest counters set back to 0.") success_message = _("AKs' interest counters set back to 0.")
def perform_action(self, entity): def action(self, form):
entity.interest_counter = 0 self.entities.update(interest_counter=0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment