User menu
* adds user notifications and management
This commit is contained in:
@@ -1,3 +1,21 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
from user.models import UserNotification, KonovaUserExtension
|
||||
|
||||
|
||||
class UserNotificationAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
"id",
|
||||
"name",
|
||||
"is_active",
|
||||
]
|
||||
|
||||
|
||||
class KonovaUserExtensionAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
"user",
|
||||
]
|
||||
|
||||
|
||||
admin.site.register(UserNotification, UserNotificationAdmin)
|
||||
admin.site.register(KonovaUserExtension, KonovaUserExtensionAdmin)
|
||||
|
||||
17
user/enums.py
Normal file
17
user/enums.py
Normal file
@@ -0,0 +1,17 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 08.07.21
|
||||
|
||||
"""
|
||||
from konova.enums import BaseEnum
|
||||
|
||||
|
||||
class UserNotificationEnum(BaseEnum):
|
||||
NOTIFY_ON_NEW_RELATED_DATA = "NOTIFY_ON_NEW_RELATED_DATA" # notifies in case new data has been added which is related to the user's organisation
|
||||
NOTIFY_ON_SHARE_LINK_DISABLED = "NOTIFY_ON_SHARE_LINK_DISABLED" # notifies in case share link for data has been disabled
|
||||
NOTIFY_ON_SHARED_ACCESS_REMOVED = "NOTIFY_ON_SHARED_ACCESS_REMOVED" # notifies in case shared access to data has been removed
|
||||
NOTIFY_ON_SHARED_DATA_REGISTERED = "NOTIFY_ON_SHARED_DATA_REGISTERED" # notifies in case data has been "verzeichnet"
|
||||
NOTIFY_ON_SHARED_DATA_DELETED = "NOTIFY_ON_SHARED_DATA_DELETED" # notifies in case data has been deleted
|
||||
NOTIFY_ON_REGISTERED_DATA_EDITED = "NOTIFY_ON_REGISTERED_DATA_EDITED" # notifies in case registered ("verzeichnet") data has been edited
|
||||
70
user/forms.py
Normal file
70
user/forms.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 08.07.21
|
||||
|
||||
"""
|
||||
from django import forms
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from konova.forms import BaseForm
|
||||
from user.models import UserNotification, KonovaUserExtension
|
||||
|
||||
|
||||
class UserNotificationForm(BaseForm):
|
||||
""" Form for changing the notification settings of a user
|
||||
|
||||
"""
|
||||
notifications = forms.MultipleChoiceField(
|
||||
label_suffix="",
|
||||
label=_("Notifications"),
|
||||
required=False, # allow total disabling of all notifications
|
||||
help_text=_("Select the situations when you want to receive a notification"),
|
||||
widget=forms.CheckboxSelectMultiple(
|
||||
attrs={
|
||||
"class": "no-bullets",
|
||||
}
|
||||
),
|
||||
choices=[]
|
||||
)
|
||||
|
||||
def __init__(self, user: User, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
||||
self.form_title = _("Edit notifications")
|
||||
self.action_url = reverse("user:notifications")
|
||||
self.cancel_redirect = reverse("user:index")
|
||||
|
||||
# Insert all notifications into form field by creating choices as tuples
|
||||
notifications = UserNotification.objects.filter(
|
||||
is_active=True,
|
||||
)
|
||||
choices = []
|
||||
for n in notifications:
|
||||
choices.append(
|
||||
(n.id, _(n.name))
|
||||
)
|
||||
self.fields["notifications"].choices = choices
|
||||
|
||||
# Set currently selected notifications as initial
|
||||
self.konova_extension = KonovaUserExtension.objects.get_or_create(
|
||||
user=user
|
||||
)[0]
|
||||
users_current_notifications = self.konova_extension.notifications.all()
|
||||
users_current_notifications = [str(n.id) for n in users_current_notifications]
|
||||
self.fields["notifications"].initial = users_current_notifications
|
||||
|
||||
def save(self):
|
||||
""" Stores the changes in the user konova_extension
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
selected_notification_ids = self.cleaned_data.get("notifications", [])
|
||||
notifications = UserNotification.objects.filter(
|
||||
id__in=selected_notification_ids,
|
||||
)
|
||||
self.konova_extension.notifications.set(notifications)
|
||||
@@ -1,3 +1,38 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
from user.enums import UserNotificationEnum
|
||||
|
||||
|
||||
class UserNotification(models.Model):
|
||||
""" Notifications for users
|
||||
|
||||
"""
|
||||
id = models.CharField(
|
||||
max_length=500,
|
||||
null=False,
|
||||
blank=False,
|
||||
choices=UserNotificationEnum.as_choices(drop_empty_choice=True),
|
||||
primary_key=True,
|
||||
)
|
||||
name = models.CharField(
|
||||
max_length=500,
|
||||
null=False,
|
||||
blank=False,
|
||||
unique=True,
|
||||
help_text="Human readable name"
|
||||
)
|
||||
is_active = models.BooleanField(default=True, help_text="Can be toggle to enable/disable this notification for all users")
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class KonovaUserExtension(models.Model):
|
||||
""" Extension model for additional ksp features
|
||||
|
||||
Extends the default user model for some extras
|
||||
|
||||
"""
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
notifications = models.ManyToManyField(UserNotification, related_name="+")
|
||||
|
||||
@@ -1,22 +1,60 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load i18n %}
|
||||
{% load i18n fontawesome_5 %}
|
||||
|
||||
{% block body %}
|
||||
<div class="row">
|
||||
<div class="col-md-3 border">
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>{% trans 'Username' %}</th>
|
||||
<th scope="row">{% trans 'Username' %}</th>
|
||||
<td>{{user.username}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{% trans 'Name' %}</th>
|
||||
<th scope="row">{% trans 'Name' %}</th>
|
||||
<td>{{user.first_name}} {{user.last_name}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans 'E-Mail' %}</th>
|
||||
<td>{{user.email}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans 'Groups' %}</th>
|
||||
<td>
|
||||
{% for group in user.groups.all %}
|
||||
<span class="badge badge-pill rlp-r">{% trans group.name %}</span>
|
||||
{% endfor %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div class="col-md">
|
||||
|
||||
<h4>{% trans 'Settings' %}</h4>
|
||||
<article>
|
||||
<small>
|
||||
{% blocktrans %}
|
||||
Please note: Personal data can only be edited in the login portal. The settings in here are KSP specific.
|
||||
{% endblocktrans %}
|
||||
</small>
|
||||
</article>
|
||||
<hr>
|
||||
<div class="col-sm">
|
||||
<div class="row mb-2">
|
||||
<a href="{% url 'user:index' %}" title="{% trans 'Change default configuration for your KSP map' %}">
|
||||
<button class="btn btn-default">
|
||||
{% fa5_icon 'layer-group' %}
|
||||
<span>{% trans 'Map settings' %}</span>
|
||||
</button>
|
||||
</a>
|
||||
</div>
|
||||
<div class="row mb-2">
|
||||
<a href="{% url 'user:notifications' %}" title="{% trans 'Change notification configurations' %}">
|
||||
<button class="btn btn-default">
|
||||
{% fa5_icon 'bell' %}
|
||||
<span>{% trans 'Notification settings' %}</span>
|
||||
</button>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
5
user/templates/user/notifications.html
Normal file
5
user/templates/user/notifications.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block body %}
|
||||
{% include 'generic_table_form.html' %}
|
||||
{% endblock %}
|
||||
@@ -7,9 +7,10 @@ Created on: 08.07.21
|
||||
"""
|
||||
from django.urls import path
|
||||
|
||||
from user.views import index_view
|
||||
from user.views import *
|
||||
|
||||
app_name="user"
|
||||
app_name = "user"
|
||||
urlpatterns = [
|
||||
path("", index_view, name="index"),
|
||||
path("notifications/", notifications_view, name="notifications"),
|
||||
]
|
||||
@@ -1,8 +1,12 @@
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import render, redirect
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
from user.forms import UserNotificationForm
|
||||
from user.models import KonovaUserExtension
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -21,3 +25,43 @@ def index_view(request: HttpRequest):
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
def notifications_view(request: HttpRequest):
|
||||
""" Renders the notifications settings view
|
||||
|
||||
Args:
|
||||
request ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "user/notifications.html"
|
||||
user = request.user
|
||||
konova_ext = KonovaUserExtension.objects.get_or_create(
|
||||
user=user
|
||||
)[0]
|
||||
|
||||
form = UserNotificationForm(user=user, data=request.POST or None)
|
||||
if request.method == "POST":
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(
|
||||
request,
|
||||
_("Notifications edited")
|
||||
)
|
||||
return redirect("user:index")
|
||||
elif request.method == "GET":
|
||||
# Implicit
|
||||
pass
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
context = {
|
||||
"user": user,
|
||||
"form": form,
|
||||
"konova_ext": konova_ext,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
Reference in New Issue
Block a user