From df273a6009ca1b9892138ce96264cf8d2a482694 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 10 Feb 2023 20:57:17 +0000 Subject: [PATCH] Switch database location and use mixins for CRUD --- core/forms.py | 31 +- core/templates/modals/context.html | 2 +- core/templates/partials/results_load.html | 2 +- core/templates/partials/rule-list.html | 2 +- core/templates/ui/insights/info.html | 2 +- core/templates/ui/insights/insights.html | 2 +- .../templates/window-content/object-form.html | 2 +- core/templates/window-content/object.html | 2 +- core/templates/window-content/objects.html | 2 +- core/templates/window-content/results.html | 2 +- core/views/helpers.py | 753 ------------------ core/views/notifications.py | 4 +- docker-compose.yml | 8 +- 13 files changed, 16 insertions(+), 798 deletions(-) delete mode 100644 core/views/helpers.py diff --git a/core/forms.py b/core/forms.py index 135e79e..11278f7 100644 --- a/core/forms.py +++ b/core/forms.py @@ -2,6 +2,7 @@ from django import forms from django.contrib.auth.forms import UserCreationForm from django.core.exceptions import FieldDoesNotExist from django.forms import ModelForm +from mixins.restrictions import RestrictedFormMixin from core.db.storage import db from core.lib.parsing import QueryError @@ -12,36 +13,6 @@ from .models import NotificationRule, NotificationSettings, User # flake8: noqa: E501 -class RestrictedFormMixin: - """ - This mixin is used to restrict the queryset of a form to the current user. - The request object is passed from the view. - Fieldargs is used to pass additional arguments to the queryset filter. - """ - - fieldargs = {} - - def __init__(self, *args, **kwargs): - # self.fieldargs = {} - self.request = kwargs.pop("request") - super().__init__(*args, **kwargs) - for field in self.fields: - # Check it's not something like a CharField which has no queryset - if not hasattr(self.fields[field], "queryset"): - continue - - model = self.fields[field].queryset.model - # Check if the model has a user field - try: - model._meta.get_field("user") - # Add the user to the queryset filters - self.fields[field].queryset = model.objects.filter( - user=self.request.user, **self.fieldargs.get(field, {}) - ) - except FieldDoesNotExist: - pass - - class NewUserForm(UserCreationForm): email = forms.EmailField(required=True) diff --git a/core/templates/modals/context.html b/core/templates/modals/context.html index 99d56b4..2b0c7a4 100644 --- a/core/templates/modals/context.html +++ b/core/templates/modals/context.html @@ -42,7 +42,7 @@ {% endblock %} {% block modal_content %} - {% include 'partials/notify.html' %} + {% include 'mixins/partials/notify.html' %}