jumpserver/apps/users/serializers.py

98 lines
3.0 KiB
Python
Raw Normal View History

2016-09-03 11:05:50 +00:00
# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext_lazy as _
2016-09-03 11:05:50 +00:00
from rest_framework import serializers
2016-09-21 07:48:21 +00:00
from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin
2016-09-03 11:05:50 +00:00
2016-10-15 09:14:56 +00:00
from common.utils import unsign
2016-09-03 11:05:50 +00:00
from .models import User, UserGroup
class UserDetailSerializer(serializers.ModelSerializer):
2016-09-06 08:40:44 +00:00
class Meta:
model = User
fields = ['avatar', 'wechat', 'phone', 'enable_otp', 'comment', 'is_active', 'name']
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-09-18 06:28:34 +00:00
from sshpubkeys import SSHKey
from sshpubkeys.exceptions import InvalidKeyException
ssh = SSHKey(value)
try:
ssh.parse()
except InvalidKeyException as e:
print e
raise serializers.ValidationError(_('Not a valid ssh public key'))
except NotImplementedError as e:
print e
raise serializers.ValidationError(_('Not a valid ssh public key'))
return value
2016-09-21 07:48:21 +00:00
class UserAndGroupSerializer(serializers.ModelSerializer):
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all())
class Meta:
model = User
fields = ['id', 'groups']
class GroupDetailSerializer(serializers.ModelSerializer):
class Meta:
model = UserGroup
fields = ['id', 'name', 'comment', 'date_created', 'created_by', 'users']
2016-09-21 07:48:21 +00:00
class UserBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2016-09-22 08:06:46 +00:00
group_display = serializers.SerializerMethodField()
active_display = serializers.SerializerMethodField()
2016-09-26 11:39:43 +00:00
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all())
2016-09-21 07:48:21 +00:00
class Meta(object):
model = User
list_serializer_class = BulkListSerializer
2016-09-21 08:43:41 +00:00
fields = ['id', 'is_active', 'username', 'name', 'email', 'role', 'avatar',
2016-09-22 08:06:46 +00:00
'enable_otp', 'comment', 'groups', 'get_role_display',
'group_display', 'active_display']
2016-10-15 09:14:56 +00:00
@staticmethod
def get_group_display(obj):
2016-09-22 08:06:46 +00:00
return " ".join([group.name for group in obj.groups.all()])
2016-10-15 09:14:56 +00:00
@staticmethod
def get_active_display(obj):
# TODO: user active state
2016-09-22 08:06:46 +00:00
return not (obj.is_expired and obj.is_active)
class GroupBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer):
user_amount = serializers.SerializerMethodField()
class Meta:
model = UserGroup
list_serializer_class = BulkListSerializer
fields = ['id', 'name', 'comment', 'user_amount']
2016-10-15 09:14:56 +00:00
@staticmethod
def get_user_amount(obj):
return obj.users.count()
2016-10-15 09:14:56 +00:00
class AppUserRegisterSerializer(serializers.Serializer):
username = serializers.CharField(max_length=20)
def create(self, validated_data):
sign = validated_data('username', '')
username = unsign(sign)
pass
def update(self, instance, validated_data):
pass