# 63 Refactoring
* refactors django User model to custom User model to provide further attributes and methods directly on the user model
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from user.models import UserNotification, KonovaUserExtension, UserActionLogEntry
|
||||
from user.models import UserNotification, UserActionLogEntry, User
|
||||
|
||||
|
||||
class UserNotificationAdmin(admin.ModelAdmin):
|
||||
@@ -11,9 +11,13 @@ class UserNotificationAdmin(admin.ModelAdmin):
|
||||
]
|
||||
|
||||
|
||||
class KonovaUserExtensionAdmin(admin.ModelAdmin):
|
||||
class UserAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
"user",
|
||||
"id",
|
||||
"username",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"email",
|
||||
]
|
||||
|
||||
|
||||
@@ -27,5 +31,5 @@ class UserActionLogEntryAdmin(admin.ModelAdmin):
|
||||
|
||||
|
||||
admin.site.register(UserNotification, UserNotificationAdmin)
|
||||
admin.site.register(KonovaUserExtension, KonovaUserExtensionAdmin)
|
||||
admin.site.register(UserActionLogEntry, UserActionLogEntryAdmin)
|
||||
admin.site.register(UserActionLogEntry, UserActionLogEntryAdmin)
|
||||
admin.site.register(User, UserAdmin)
|
||||
@@ -9,9 +9,8 @@ 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_RECORDED = "NOTIFY_ON_SHARED_DATA_RECORDED" # 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
|
||||
NOTIFY_ON_REGISTERED_DATA_EDITED = "NOTIFY_ON_REGISTERED_DATA_EDITED" # notifies in case registered ("verzeichnet") data has been edited
|
||||
NOTIFY_ON_SHARED_ACCESS_GAINED = "NOTIFY_ON_SHARED_ACCESS_GAINED" # notifies in case new access has been gained
|
||||
@@ -8,10 +8,10 @@ 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 user.models import User
|
||||
|
||||
from konova.forms import BaseForm, BaseModalForm
|
||||
from user.models import UserNotification, KonovaUserExtension
|
||||
from user.models import UserNotification
|
||||
|
||||
|
||||
class UserNotificationForm(BaseForm):
|
||||
@@ -50,11 +50,7 @@ class UserNotificationForm(BaseForm):
|
||||
)
|
||||
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 = self.user.notifications.all()
|
||||
users_current_notifications = [str(n.id) for n in users_current_notifications]
|
||||
self.fields["notifications"].initial = users_current_notifications
|
||||
|
||||
@@ -68,7 +64,7 @@ class UserNotificationForm(BaseForm):
|
||||
notifications = UserNotification.objects.filter(
|
||||
id__in=selected_notification_ids,
|
||||
)
|
||||
self.konova_extension.notifications.set(notifications)
|
||||
self.user.notifications.set(notifications)
|
||||
|
||||
|
||||
class UserContactForm(BaseModalForm):
|
||||
|
||||
@@ -6,5 +6,5 @@ Created on: 15.11.21
|
||||
|
||||
"""
|
||||
from .user_action import *
|
||||
from .konova_user import *
|
||||
from .user import *
|
||||
from .notification import *
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 15.11.21
|
||||
|
||||
"""
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
|
||||
|
||||
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("user.UserNotification", related_name="+")
|
||||
28
user/models/user.py
Normal file
28
user/models/user.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 15.11.21
|
||||
|
||||
"""
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
|
||||
from django.db import models
|
||||
|
||||
from konova.utils.mailer import Mailer
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
notifications = models.ManyToManyField("user.UserNotification", related_name="+", blank=True)
|
||||
|
||||
def send_mail_shared_access_removed(self, obj):
|
||||
""" Sends a mail to the user in case of removed shared access
|
||||
|
||||
Args:
|
||||
obj ():
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
mailer = Mailer()
|
||||
mailer.send_mail_shared_access_removed(obj, self)
|
||||
@@ -7,7 +7,6 @@ Created on: 15.11.21
|
||||
"""
|
||||
import uuid
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@@ -34,7 +33,7 @@ class UserActionLogEntry(models.Model):
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
)
|
||||
user = models.ForeignKey(User, related_name='+', on_delete=models.CASCADE, help_text="Performing user")
|
||||
user = models.ForeignKey("user.User", related_name='+', on_delete=models.CASCADE, help_text="Performing user")
|
||||
timestamp = models.DateTimeField(auto_now_add=True, help_text="Timestamp of performed action")
|
||||
action = models.CharField(
|
||||
max_length=255,
|
||||
@@ -69,7 +68,7 @@ class UserActionLogEntry(models.Model):
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_created_action(cls, user: User, comment: str = None):
|
||||
def get_created_action(cls, user, comment: str = None):
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.CREATED,
|
||||
@@ -78,7 +77,7 @@ class UserActionLogEntry(models.Model):
|
||||
return action
|
||||
|
||||
@classmethod
|
||||
def get_edited_action(cls, user: User, comment: str = None):
|
||||
def get_edited_action(cls, user, comment: str = None):
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.EDITED,
|
||||
@@ -87,7 +86,7 @@ class UserActionLogEntry(models.Model):
|
||||
return action
|
||||
|
||||
@classmethod
|
||||
def get_deleted_action(cls, user: User, comment: str = None):
|
||||
def get_deleted_action(cls, user, comment: str = None):
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.DELETED,
|
||||
@@ -96,7 +95,7 @@ class UserActionLogEntry(models.Model):
|
||||
return action
|
||||
|
||||
@classmethod
|
||||
def get_checked_action(cls, user: User, comment: str = None):
|
||||
def get_checked_action(cls, user, comment: str = None):
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.CHECKED,
|
||||
@@ -105,7 +104,7 @@ class UserActionLogEntry(models.Model):
|
||||
return action
|
||||
|
||||
@classmethod
|
||||
def get_recorded_action(cls, user: User, comment: str = None):
|
||||
def get_recorded_action(cls, user, comment: str = None):
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.RECORDED,
|
||||
@@ -114,7 +113,7 @@ class UserActionLogEntry(models.Model):
|
||||
return action
|
||||
|
||||
@classmethod
|
||||
def get_unrecorded_action(cls, user: User, comment: str = None):
|
||||
def get_unrecorded_action(cls, user, comment: str = None):
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.UNRECORDED,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.models import User
|
||||
from user.models import User
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -8,7 +8,6 @@ from django.utils.translation import gettext_lazy as _
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import any_group_check
|
||||
from user.forms import UserNotificationForm, UserContactForm
|
||||
from user.models import KonovaUserExtension
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -43,9 +42,6 @@ def notifications_view(request: HttpRequest):
|
||||
"""
|
||||
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":
|
||||
@@ -65,7 +61,6 @@ def notifications_view(request: HttpRequest):
|
||||
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