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

28 lines
850 B
Python
Raw Normal View History

2014-07-08 16:37:38 +00:00
from rest_framework import serializers
from orchestra.api import router
from .models import Resource, ResourceAllocation
class ResourceSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField('get_name')
current = serializers.SerializerMethodField('get_current')
allocation = serializers.IntegerField(source='value')
class Meta:
model = ResourceAllocation
fields = ('name', 'current', 'allocation')
def get_name(self, instance):
return instance.resource.name
def get_current(self, instance):
return instance.resource.get_current()
for resources in Resource.group_by_content_type():
model = resources[0].content_type.model_class()
router.insert(model, 'resources', ResourceSerializer, required=False,
source='allocations')