From c85388e3cadf4c21736ed1d2bc2ff3f1135f05ad Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 29 Jun 2024 16:48:08 +0200 Subject: [PATCH] stash --- core/authentication/api_v2.py | 41 +++++---------------------- core/authentication/serializers.py | 32 +++++++++++++++++++++ core/notifications/api_v2.py | 3 ++ web/src/views/admin/Notifications.vue | 2 +- 4 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 core/authentication/serializers.py diff --git a/core/authentication/api_v2.py b/core/authentication/api_v2.py index 514a697..2547b6d 100644 --- a/core/authentication/api_v2.py +++ b/core/authentication/api_v2.py @@ -12,25 +12,7 @@ from knox.models import AuthToken from knox.views import LoginView as KnoxLoginView from authentication.models import ExtendedUser - - -class UserSerializer(serializers.ModelSerializer): - permissions = serializers.SerializerMethodField() - groups = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name') - - class Meta: - model = ExtendedUser - fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups') - read_only_fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups') - - def get_permissions(self, obj): - return list(set(obj.get_permissions())) - - -@receiver(post_save, sender=ExtendedUser) -def create_auth_token(sender, instance=None, created=False, **kwargs): - if created: - AuthToken.objects.create(user=instance) +from authentication.serializers import UserSerializer, GroupSerializer class UserViewSet(viewsets.ModelViewSet): @@ -38,26 +20,17 @@ class UserViewSet(viewsets.ModelViewSet): serializer_class = UserSerializer -class GroupSerializer(serializers.ModelSerializer): - permissions = serializers.SerializerMethodField() - members = serializers.SerializerMethodField() - - class Meta: - model = Group - fields = ('id', 'name', 'permissions', 'members') - - def get_permissions(self, obj): - return ["*:" + p.codename for p in obj.permissions.all()] - - def get_members(self, obj): - return [u.username for u in obj.user_set.all()] - - class GroupViewSet(viewsets.ModelViewSet): queryset = Group.objects.all() serializer_class = GroupSerializer +@receiver(post_save, sender=ExtendedUser) +def create_auth_token(sender, instance=None, created=False, **kwargs): + if created: + AuthToken.objects.create(user=instance) + + @api_view(['GET']) @permission_classes([IsAuthenticated]) def selfUser(request): diff --git a/core/authentication/serializers.py b/core/authentication/serializers.py new file mode 100644 index 0000000..0581865 --- /dev/null +++ b/core/authentication/serializers.py @@ -0,0 +1,32 @@ +from rest_framework import serializers +from django.contrib.auth.models import Group + +from authentication.models import ExtendedUser + + +class UserSerializer(serializers.ModelSerializer): + permissions = serializers.SerializerMethodField() + groups = serializers.SlugRelatedField(many=True, read_only=True, slug_field='name') + + class Meta: + model = ExtendedUser + fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups') + read_only_fields = ('id', 'username', 'email', 'first_name', 'last_name', 'permissions', 'groups') + + def get_permissions(self, obj): + return list(set(obj.get_permissions())) + + +class GroupSerializer(serializers.ModelSerializer): + permissions = serializers.SerializerMethodField() + members = serializers.SerializerMethodField() + + class Meta: + model = Group + fields = ('id', 'name', 'permissions', 'members') + + def get_permissions(self, obj): + return ["*:" + p.codename for p in obj.permissions.all()] + + def get_members(self, obj): + return [u.username for u in obj.user_set.all()] diff --git a/core/notifications/api_v2.py b/core/notifications/api_v2.py index e459d01..a9492f5 100644 --- a/core/notifications/api_v2.py +++ b/core/notifications/api_v2.py @@ -9,6 +9,7 @@ from notifications.models import MessageTemplate, UserNotificationChannel from rest_framework import serializers from notifications.templates import TEMPLATE_VARS +from authentication.serializers import UserSerializer class MessageTemplateSerializer(serializers.ModelSerializer): @@ -18,6 +19,8 @@ class MessageTemplateSerializer(serializers.ModelSerializer): class UserNotificationChannelSerializer(serializers.ModelSerializer): + user = UserSerializer() + class Meta: model = UserNotificationChannel fields = '__all__' diff --git a/web/src/views/admin/Notifications.vue b/web/src/views/admin/Notifications.vue index 5a451fc..312a902 100644 --- a/web/src/views/admin/Notifications.vue +++ b/web/src/views/admin/Notifications.vue @@ -1,7 +1,7 @@