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

44 lines
1.6 KiB
Python
Raw Normal View History

2015-04-04 18:10:39 +00:00
from django.utils.translation import ugettext_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')
def validate(self, attrs):
2015-04-23 14:34:04 +00:00
attrs = super(SystemUserSerializer, self).validate(attrs)
user = SystemUser(
2015-04-23 14:34:04 +00:00
username=attrs.get('username') or self.instance.username,
shell=attrs.get('shell') or self.instance.shell,
)
2015-05-18 15:21:42 +00:00
validate_home(user, attrs, self.get_account())
return attrs
def validate_groups(self, attrs, source):
groups = attrs.get(source)
if groups:
for group in groups:
if group.username == attrs['username']:
raise serializers.ValidationError(
_("Do not make the user member of its group"))
return attrs