Implement custom notification settings
This commit is contained in:
@@ -343,9 +343,13 @@ class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
|
||||
|
||||
model = None
|
||||
submit_url_name = None
|
||||
submit_url_args = ["type", "pk"]
|
||||
|
||||
request = None
|
||||
|
||||
# Whether pk is required in the get request
|
||||
pk_required = True
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.title = "Update " + self.context_object_name_singular
|
||||
@@ -376,7 +380,8 @@ class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
|
||||
if not type:
|
||||
return HttpResponseBadRequest("No type specified")
|
||||
if not pk:
|
||||
return HttpResponseBadRequest("No pk specified")
|
||||
if self.pk_required:
|
||||
return HttpResponseBadRequest("No pk specified")
|
||||
if type not in self.allowed_types:
|
||||
return HttpResponseBadRequest("Invalid type specified")
|
||||
self.template_name = f"wm/{type}.html"
|
||||
@@ -385,7 +390,15 @@ class ObjectUpdate(RestrictedViewMixin, ObjectNameMixin, UpdateView):
|
||||
type = "modal"
|
||||
|
||||
self.object = self.get_object()
|
||||
submit_url = reverse(self.submit_url_name, kwargs={"type": type, "pk": pk})
|
||||
|
||||
submit_url_args = {}
|
||||
for arg in self.submit_url_args:
|
||||
if arg in locals():
|
||||
submit_url_args[arg] = locals()[arg]
|
||||
elif arg in kwargs:
|
||||
submit_url_args[arg] = kwargs[arg]
|
||||
submit_url = reverse(self.submit_url_name, kwargs=submit_url_args)
|
||||
|
||||
context = self.get_context_data()
|
||||
form = kwargs.get("form", None)
|
||||
if form:
|
||||
|
||||
26
core/views/notifications.py
Normal file
26
core/views/notifications.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
|
||||
from core.forms import NotificationSettingsForm
|
||||
from core.models import NotificationSettings
|
||||
from core.views import ObjectUpdate
|
||||
|
||||
|
||||
# Notifications - we create a new notification settings object if there isn't one
|
||||
# Hence, there is only an update view, not a create view.
|
||||
class NotificationsUpdate(LoginRequiredMixin, ObjectUpdate):
|
||||
model = NotificationSettings
|
||||
form_class = NotificationSettingsForm
|
||||
|
||||
# list_url_name = "notifications"
|
||||
# list_url_args = ["type"]
|
||||
|
||||
submit_url_name = "notifications_update"
|
||||
submit_url_args = ["type"]
|
||||
|
||||
pk_required = False
|
||||
|
||||
def get_object(self, **kwargs):
|
||||
notification_settings, _ = NotificationSettings.objects.get_or_create(
|
||||
user=self.request.user
|
||||
)
|
||||
return notification_settings
|
||||
Reference in New Issue
Block a user