Switch database location and use mixins for CRUD
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user