This commit is contained in:
j3d1 2024-06-29 16:48:08 +02:00
parent b6d1422a77
commit 381061a2fd
4 changed files with 43 additions and 35 deletions

View file

@ -12,25 +12,7 @@ from knox.models import AuthToken
from knox.views import LoginView as KnoxLoginView from knox.views import LoginView as KnoxLoginView
from authentication.models import ExtendedUser from authentication.models import ExtendedUser
from authentication.serializers import UserSerializer, GroupSerializer
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)
class UserViewSet(viewsets.ModelViewSet): class UserViewSet(viewsets.ModelViewSet):
@ -38,26 +20,17 @@ class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer 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): class GroupViewSet(viewsets.ModelViewSet):
queryset = Group.objects.all() queryset = Group.objects.all()
serializer_class = GroupSerializer 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']) @api_view(['GET'])
@permission_classes([IsAuthenticated]) @permission_classes([IsAuthenticated])
def selfUser(request): def selfUser(request):

View file

@ -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()]

View file

@ -9,6 +9,7 @@ from notifications.models import MessageTemplate, UserNotificationChannel
from rest_framework import serializers from rest_framework import serializers
from notifications.templates import TEMPLATE_VARS from notifications.templates import TEMPLATE_VARS
from authentication.serializers import UserSerializer
class MessageTemplateSerializer(serializers.ModelSerializer): class MessageTemplateSerializer(serializers.ModelSerializer):
@ -18,6 +19,8 @@ class MessageTemplateSerializer(serializers.ModelSerializer):
class UserNotificationChannelSerializer(serializers.ModelSerializer): class UserNotificationChannelSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta: class Meta:
model = UserNotificationChannel model = UserNotificationChannel
fields = '__all__' fields = '__all__'

View file

@ -1,7 +1,7 @@
<template> <template>
<ul> <ul>
<li v-for="channel in userNotificationChannels" :key="channel.id"> <li v-for="channel in userNotificationChannels" :key="channel.id">
{{ channel.name }} {{ channel.id }} - {{ channel.channel_type }} - {{ channel.channel_target }} - {{ channel.event_filter }} - {{ channel.active }} - {{ channel.created }} - {{ channel.user }}
</li> </li>
</ul> </ul>
</template> </template>