django-orchestra/orchestra/contrib/systemusers/serializers.py

44 lines
1.6 KiB
Python
Raw Normal View History

from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
2015-05-18 15:21:42 +00:00
from orchestra.api.serializers import SetPasswordHyperlinkedSerializer, RelatedHyperlinkedModelSerializer
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.serializers import AccountSerializerMixin
2014-09-30 16:39:47 +00:00
from .models import SystemUser
from .validators import validate_home
2014-09-30 16:39:47 +00:00
2015-05-18 15:21:42 +00:00
class RelatedGroupSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
2014-10-07 13:08:59 +00:00
class Meta:
model = SystemUser
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'username',)
2014-10-07 13:08:59 +00:00
2015-04-23 14:34:04 +00:00
class SystemUserSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
2015-05-18 15:21:42 +00:00
groups = RelatedGroupSerializer(many=True, required=False)
class Meta:
2014-09-30 16:39:47 +00:00
model = SystemUser
fields = (
2015-04-30 09:51:55 +00:00
'url', 'id', 'username', 'password', 'home', 'directory', 'shell', 'groups', 'is_active',
)
2015-04-23 14:34:04 +00:00
postonly_fields = ('username', 'password')
2015-07-29 09:05:07 +00:00
def validate_directory(self, directory):
return directory.lstrip('/')
def validate(self, data):
data = super(SystemUserSerializer, self).validate(data)
user = SystemUser(
2015-07-29 09:05:07 +00:00
username=data.get('username') or self.instance.username,
shell=data.get('shell') or self.instance.shell,
)
2015-07-29 09:05:07 +00:00
validate_home(user, data, self.get_account())
groups = data.get('groups')
if groups:
for group in groups:
2015-07-29 09:05:07 +00:00
if group.username == data['username']:
raise serializers.ValidationError(
_("Do not make the user member of its group"))
2015-07-29 09:05:07 +00:00
return data