2014-05-08 16:59:35 +00:00
|
|
|
from django.forms import widgets
|
2014-10-16 15:11:52 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2015-04-04 18:10:39 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
from orchestra.api.serializers import HyperlinkedModelSerializer, SetPasswordHyperlinkedSerializer
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.serializers import AccountSerializerMixin
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
from .models import Database, DatabaseUser
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
class RelatedDatabaseUserSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
2014-10-14 13:50:19 +00:00
|
|
|
model = DatabaseUser
|
|
|
|
fields = ('url', 'username')
|
|
|
|
|
|
|
|
def from_native(self, data, files=None):
|
2014-10-16 15:11:52 +00:00
|
|
|
queryset = self.opts.model.objects.filter(account=self.account)
|
|
|
|
return get_object_or_404(queryset, username=data['username'])
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
class DatabaseSerializer(AccountSerializerMixin, HyperlinkedModelSerializer):
|
2015-04-23 14:34:04 +00:00
|
|
|
users = RelatedDatabaseUserSerializer(many=True) #allow_add_remove=True
|
2014-10-14 13:50:19 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
2014-10-14 13:50:19 +00:00
|
|
|
model = Database
|
|
|
|
fields = ('url', 'name', 'type', 'users')
|
2014-10-15 21:18:50 +00:00
|
|
|
postonly_fields = ('name', 'type')
|
2014-10-16 15:11:52 +00:00
|
|
|
|
|
|
|
def validate(self, attrs):
|
2015-04-23 14:34:04 +00:00
|
|
|
attrs = super(DatabaseSerializer, self).validate(attrs)
|
2014-10-16 15:11:52 +00:00
|
|
|
for user in attrs['users']:
|
|
|
|
if user.type != attrs['type']:
|
|
|
|
raise serializers.ValidationError("User type must be" % attrs['type'])
|
|
|
|
return attrs
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
class RelatedDatabaseSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = Database
|
2014-10-14 13:50:19 +00:00
|
|
|
fields = ('url', 'name',)
|
|
|
|
|
|
|
|
def from_native(self, data, files=None):
|
2014-10-16 15:11:52 +00:00
|
|
|
queryset = self.opts.model.objects.filter(account=self.account)
|
|
|
|
return get_object_or_404(queryset, name=data['name'])
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
class DatabaseUserSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
|
|
|
|
databases = RelatedDatabaseSerializer(many=True, required=False) # allow_add_remove=True
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
class Meta:
|
|
|
|
model = DatabaseUser
|
2014-10-14 13:50:19 +00:00
|
|
|
fields = ('url', 'username', 'password', 'type', 'databases')
|
2015-04-23 14:34:04 +00:00
|
|
|
postonly_fields = ('username', 'type', 'password')
|
2014-10-14 13:50:19 +00:00
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
def validate(self, attrs):
|
2015-04-23 14:34:04 +00:00
|
|
|
attrs = super(DatabaseUserSerializer, self).validate(attrs)
|
2014-10-16 15:11:52 +00:00
|
|
|
for database in attrs.get('databases', []):
|
|
|
|
if database.type != attrs['type']:
|
|
|
|
raise serializers.ValidationError("Database type must be" % attrs['type'])
|
|
|
|
return attrs
|