2016-09-03 11:05:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2016-09-13 13:45:10 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2016-09-03 11:05:50 +00:00
|
|
|
from rest_framework import serializers
|
2017-12-12 04:19:45 +00:00
|
|
|
from rest_framework_bulk import BulkListSerializer
|
2016-09-03 11:05:50 +00:00
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
from common.utils import get_signer, validate_ssh_public_key
|
2017-12-12 04:19:45 +00:00
|
|
|
from common.mixins import BulkSerializerMixin
|
2016-09-03 11:05:50 +00:00
|
|
|
from .models import User, UserGroup
|
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
signer = get_signer()
|
|
|
|
|
2016-09-03 11:05:50 +00:00
|
|
|
|
2016-11-09 11:29:15 +00:00
|
|
|
class UserSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
2016-11-14 16:48:48 +00:00
|
|
|
groups_display = serializers.SerializerMethodField()
|
2018-03-27 10:34:41 +00:00
|
|
|
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all(), required=False)
|
2016-11-09 11:29:15 +00:00
|
|
|
|
2016-09-06 08:40:44 +00:00
|
|
|
class Meta:
|
|
|
|
model = User
|
2016-11-09 11:29:15 +00:00
|
|
|
list_serializer_class = BulkListSerializer
|
|
|
|
exclude = ['first_name', 'last_name', 'password', '_private_key', '_public_key']
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
fields = super(UserSerializer, self).get_field_names(declared_fields, info)
|
2016-11-14 16:48:48 +00:00
|
|
|
fields.extend(['groups_display', 'get_role_display', 'is_valid'])
|
2016-11-09 11:29:15 +00:00
|
|
|
return fields
|
|
|
|
|
|
|
|
@staticmethod
|
2016-11-14 16:48:48 +00:00
|
|
|
def get_groups_display(obj):
|
2016-11-09 11:29:15 +00:00
|
|
|
return " ".join([group.name for group in obj.groups.all()])
|
|
|
|
|
2016-09-06 13:44:23 +00:00
|
|
|
|
2016-09-13 13:45:10 +00:00
|
|
|
class UserPKUpdateSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2016-09-18 06:28:34 +00:00
|
|
|
fields = ['id', '_public_key']
|
|
|
|
|
2016-10-15 09:14:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def validate__public_key(value):
|
2016-11-09 15:49:10 +00:00
|
|
|
if not validate_ssh_public_key(value):
|
2016-09-18 06:28:34 +00:00
|
|
|
raise serializers.ValidationError(_('Not a valid ssh public key'))
|
2016-09-13 13:45:10 +00:00
|
|
|
return value
|
2016-09-21 07:48:21 +00:00
|
|
|
|
|
|
|
|
2016-11-14 16:48:48 +00:00
|
|
|
class UserUpdateGroupSerializer(serializers.ModelSerializer):
|
2016-10-02 03:09:27 +00:00
|
|
|
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['id', 'groups']
|
|
|
|
|
|
|
|
|
2016-11-14 16:48:48 +00:00
|
|
|
class UserGroupSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
2018-01-30 10:07:51 +00:00
|
|
|
users = serializers.SerializerMethodField()
|
2016-11-14 16:48:48 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserGroup
|
|
|
|
list_serializer_class = BulkListSerializer
|
2017-01-08 05:35:59 +00:00
|
|
|
fields = '__all__'
|
2016-11-14 16:48:48 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2018-01-30 10:07:51 +00:00
|
|
|
def get_users(obj):
|
|
|
|
return [user.name for user in obj.users.all()]
|
2016-11-14 16:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserGroupUpdateMemeberSerializer(serializers.ModelSerializer):
|
|
|
|
users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserGroup
|
|
|
|
fields = ['id', 'users']
|
|
|
|
|
2018-01-23 02:36:20 +00:00
|
|
|
|
|
|
|
class ChangeUserPasswordSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['password']
|