django-orchestra/orchestra/apps/databases/serializers.py

41 lines
1.3 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.forms import widgets
from django.utils.translation import ugettext, ugettext_lazy as _
from rest_framework import serializers
from orchestra.apps.accounts.serializers import AccountSerializerMixin
from orchestra.core.validators import validate_password
from .models import Database, DatabaseUser, Role
2014-10-09 17:04:12 +00:00
class UserRoleSerializer(serializers.HyperlinkedModelSerializer):
2014-05-08 16:59:35 +00:00
class Meta:
model = Role
fields = ('user', 'is_owner',)
2014-10-07 13:08:59 +00:00
class RoleSerializer(serializers.HyperlinkedModelSerializer):
2014-05-08 16:59:35 +00:00
class Meta:
model = Role
fields = ('database', 'is_owner',)
class DatabaseSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
2014-10-09 17:04:12 +00:00
roles = UserRoleSerializer(many=True)
2014-05-08 16:59:35 +00:00
class Meta:
model = Database
2014-10-09 17:04:12 +00:00
fields = ('url', 'name', 'type', 'roles')
2014-05-08 16:59:35 +00:00
class DatabaseUserSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
password = serializers.CharField(max_length=128, label=_('Password'),
validators=[validate_password], write_only=True,
widget=widgets.PasswordInput)
2014-10-07 13:08:59 +00:00
roles = RoleSerializer(many=True, read_only=True)
2014-05-08 16:59:35 +00:00
class Meta:
model = DatabaseUser
2014-10-07 13:08:59 +00:00
fields = ('url', 'username', 'password', 'type', 'roles')
2014-05-08 16:59:35 +00:00
write_only_fields = ('username',)