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

28 lines
902 B
Python
Raw Normal View History

2014-07-08 16:37:38 +00:00
from rest_framework import serializers
from orchestra.api import router
2014-07-09 16:17:43 +00:00
from orchestra.utils import running_syncdb
2014-07-08 16:37:38 +00:00
2014-07-09 16:17:43 +00:00
from .models import Resource, ResourceData
2014-07-08 16:37:38 +00:00
class ResourceSerializer(serializers.ModelSerializer):
2014-07-10 15:19:06 +00:00
# TODO required allocation serializers (like resource form)
# TODO create missing ResourceData (like resource form)
# TODO make default allocation available on OPTIONS (like resource form)
2014-07-08 16:37:38 +00:00
name = serializers.SerializerMethodField('get_name')
class Meta:
2014-07-09 16:17:43 +00:00
model = ResourceData
fields = ('name', 'used', 'allocated')
read_only_fields = ('used',)
2014-07-08 16:37:38 +00:00
def get_name(self, instance):
return instance.resource.name
2014-07-09 16:17:43 +00:00
if not running_syncdb():
for resources in Resource.group_by_content_type():
model = resources[0].content_type.model_class()
router.insert(model, 'resources', ResourceSerializer, required=False)