Skip to content

Commit

Permalink
move notification settings to settings area, rework settings permissi…
Browse files Browse the repository at this point in the history
…on checking (#767)
  • Loading branch information
jeriox committed Jul 17, 2022
1 parent dcf6f05 commit 8529070
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 290 deletions.
8 changes: 4 additions & 4 deletions ephios/core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
Receivers will receive a ``request`` keyword argument.
"""

administration_settings_section = PluginSignal()
management_settings_sections = PluginSignal()
"""
This signal is sent out to get sections for administration settings. Receivers should return a list of dicts
containing key-value-pairs for 'label', 'url' and a boolean flag 'active'.
Receivers will receive a ``request`` keyword argument.
This signal is sent out to get sections for management settings. Receivers should return a list of dicts
containing key-value-pairs for 'label', 'url' and a boolean flag 'active'. Only views that the current user is
allowed to view should be returned. Receivers will receive a ``request`` keyword argument.
"""

participant_from_request = PluginSignal()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
{% extends "base.html" %}
{% extends "core/settings/settings_base.html" %}
{% load crispy_forms_filters %}
{% load i18n %}

{% block title %}
{% trans "Change password" %}
{% endblock %}

{% block content %}
<h2>{% trans "Change password" %}</h2>
{% block settings_content %}
<h3>{% trans "Change password" %}</h3>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">{% translate "Submit" %}</button>
</form>
{% endblock %}
{% endblock %}
10 changes: 9 additions & 1 deletion ephios/core/templates/core/settings/settings_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ <h1>{% translate "Settings" %} {% block settings_title %}{% endblock %}</h1>
<div class="d-flex flex-column flex-sm-row">
<div class="me-3 mb-2">
<ul class="nav nav-pills flex-column">
{% for section in settings_sections %}
<li class="nav-item"><h5>{% translate "Personal" %}</h5></li>
<li class="nav-item"><a class="nav-link {% if request.resolver_match.url_name == "settings_notifications" %}active{% endif %}"
href="{% url "core:settings_notifications" %}">{% translate "Notifications" %}</a></li>
<li class="nav-item"><a class="nav-link {% if request.resolver_match.url_name == "settings_password_change" %}active{% endif %}"
href="{% url "core:settings_password_change" %}">{% translate "Change password" %}</a></li>
{% if management_settings_sections %}
<li class="nav-item mt-2"><h5>{% translate "Management" %}</h5></li>
{% for section in management_settings_sections %}
<li class="nav-item">
<a class="nav-link {% if section.active %}active{% endif %}"
href="{{ section.url }}">{{ section.label }}</a>
</li>
{% endfor %}
{% endif %}
</ul>
</div>
<div class="flex-fill">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
{% extends "base.html" %}
{% load webpush_notifications %}
{% load webpush_notifications %}
{% load utils %}
{% extends "core/settings/settings_base.html" %}
{% load crispy_forms_filters %}
{% load i18n %}

{% block title %}
{% translate "Settings" %}
{% endblock %}
{% load webpush_notifications %}

{% block html_head %}
{% webpush_header %}
{% endblock %}

{% block content %}
<div class="page-header">
<h1>{% trans "Notifications" %}</h1>
</div>
{% block settings_content %}
<h3 class="mt-3">{% translate "Notifications" %}</h3>

<p>{% translate "You can configure whether you want to receive notifications for the following occasions:" %}</p>
<form method="post">
Expand Down
26 changes: 17 additions & 9 deletions ephios/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
UserProfileDeleteView,
UserProfileDetailView,
UserProfileListView,
UserProfileNotificationsView,
UserProfilePasswordResetView,
UserProfileUpdateView,
)
Expand Down Expand Up @@ -45,7 +44,11 @@
)
from ephios.core.views.log import LogView
from ephios.core.views.pwa import manifest, offline, serviceworker
from ephios.core.views.settings import GeneralSettingsView
from ephios.core.views.settings import (
InstanceSettingsView,
NotificationSettingsView,
PasswordChangeSettingsView,
)
from ephios.core.views.shift import (
ShiftConfigurationFormView,
ShiftCreateView,
Expand All @@ -56,7 +59,7 @@

app_name = "core"
urlpatterns = [
path("", HomeView.as_view(), name="index"),
path("", HomeView.as_view(), name="home"),
path("events/", current_event_list_view, name="event_list"),
path(
"events/<int:pk>/edit/",
Expand Down Expand Up @@ -150,7 +153,17 @@
RRuleOccurrenceView.as_view(),
name="rrule_occurrences",
),
path("settings/general/", GeneralSettingsView.as_view(), name="settings_general"),
path(
"settings/notifications/",
NotificationSettingsView.as_view(),
name="settings_notifications",
),
path(
"settings/password_change/",
PasswordChangeSettingsView.as_view(),
name="settings_password_change",
),
path("settings/instance/", InstanceSettingsView.as_view(), name="settings_instance"),
path("settings/eventtypes/", EventTypeListView.as_view(), name="settings_eventtype_list"),
path(
"settings/eventtypes/create/",
Expand All @@ -168,11 +181,6 @@
name="settings_eventtype_delete",
),
path("profile/", ProfileView.as_view(), name="profile"),
path(
"profile/notifications/",
UserProfileNotificationsView.as_view(),
name="profile_notifications",
),
path("groups/", GroupListView.as_view(), name="group_list"),
path("groups/<int:pk>/edit/", GroupUpdateView.as_view(), name="group_edit"),
path("groups/<int:pk>/delete/", GroupDeleteView.as_view(), name="group_delete"),
Expand Down
24 changes: 1 addition & 23 deletions ephios/core/views/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import Group
from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Prefetch
from django.shortcuts import redirect
from django.urls import reverse
Expand All @@ -11,20 +10,14 @@
CreateView,
DeleteView,
DetailView,
FormView,
ListView,
TemplateView,
UpdateView,
)
from django.views.generic.detail import SingleObjectMixin
from dynamic_preferences.registries import global_preferences_registry

from ephios.core.forms.users import (
GroupForm,
QualificationGrantFormset,
UserNotificationPreferenceForm,
UserProfileForm,
)
from ephios.core.forms.users import GroupForm, QualificationGrantFormset, UserProfileForm
from ephios.core.models import QualificationGrant, UserProfile
from ephios.core.services.notifications.types import (
NewProfileNotification,
Expand Down Expand Up @@ -203,21 +196,6 @@ def post(self, request, *args, **kwargs):
return self.render_to_response({"userprofile": self.object})


class UserProfileNotificationsView(LoginRequiredMixin, SuccessMessageMixin, FormView):
template_name = "core/userprofile_notifications.html"
success_message = _("Settings succesfully saved.")

def get_form(self, form_class=None):
return UserNotificationPreferenceForm(self.request.POST or None, user=self.request.user)

def get_success_url(self):
return reverse("core:profile_notifications")

def form_valid(self, form):
form.update_preferences()
return super().form_valid(form)


class GroupListView(CustomPermissionRequiredMixin, ListView):
model = Group
permission_required = "auth.view_group"
Expand Down
73 changes: 53 additions & 20 deletions ephios/core/views/settings.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
import typing

from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.views import PasswordChangeView
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext as _
from django.views.generic import FormView
from dynamic_preferences.forms import global_preference_form_builder

from ephios.core.signals import administration_settings_section
from ephios.core.forms.users import UserNotificationPreferenceForm
from ephios.core.signals import management_settings_sections
from ephios.extra.mixins import StaffRequiredMixin


def get_available_administration_settings_sections(request):
sections = [
{
"label": _("General"),
"url": reverse("core:settings_general"),
"active": request.resolver_match.url_name == "settings_general",
},
{
"label": _("Event types"),
"url": reverse("core:settings_eventtype_list"),
"active": request.resolver_match.url_name.startswith("settings_eventtype"),
},
]
for __, result in administration_settings_section.send(None, request=request):
def get_available_management_settings_sections(request):
sections = []
if request.user.is_staff:
sections.append(
{
"label": _("ephios instance"),
"url": reverse("core:settings_instance"),
"active": request.resolver_match.url_name == "settings_instance",
}
)
if request.user.has_perm("core.view_eventtype"):
sections.append(
{
"label": _("Event types"),
"url": reverse("core:settings_eventtype_list"),
"active": request.resolver_match.url_name.startswith("settings_eventtype"),
}
)
for __, result in management_settings_sections.send(None, request=request):
sections += result
return sections


class SettingsViewMixin(FormView if typing.TYPE_CHECKING else object):
def get_context_data(self, **kwargs):
kwargs["settings_sections"] = get_available_administration_settings_sections(self.request)
kwargs["management_settings_sections"] = get_available_management_settings_sections(
self.request
)
return super().get_context_data(**kwargs)


class GeneralSettingsView(StaffRequiredMixin, SuccessMessageMixin, SettingsViewMixin, FormView):
template_name = "core/settings/general.html"
class InstanceSettingsView(StaffRequiredMixin, SuccessMessageMixin, SettingsViewMixin, FormView):
template_name = "core/settings/settings_instance.html"
success_message = _("Settings saved successfully.")

def get_form_class(self):
Expand All @@ -46,4 +56,27 @@ def form_valid(self, form):
return super().form_valid(form)

def get_success_url(self):
return reverse("core:settings_general")
return reverse("core:settings_instance")


class NotificationSettingsView(
LoginRequiredMixin, SuccessMessageMixin, SettingsViewMixin, FormView
):
template_name = "core/settings/settings_notifications.html"
success_message = _("Settings succesfully saved.")

def get_form(self, form_class=None):
return UserNotificationPreferenceForm(self.request.POST or None, user=self.request.user)

def get_success_url(self):
return reverse("core:settings_notifications")

def form_valid(self, form):
form.update_preferences()
return super().form_valid(form)


class PasswordChangeSettingsView(SuccessMessageMixin, SettingsViewMixin, PasswordChangeView):
template_name = "core/settings/password_change_form.html"
success_url = reverse_lazy("core:home")
success_message = _("Password changed successfully.")
Loading

0 comments on commit 8529070

Please sign in to comment.