|
@@ -2,47 +2,50 @@ from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
-from extras.models import CF_TYPE_SELECT, CustomField, CustomFieldChoice
|
|
|
+from extras.models import CF_TYPE_SELECT, CustomField, CustomFieldChoice, CustomFieldValue
|
|
|
|
|
|
|
|
|
#
|
|
|
# Custom fields
|
|
|
#
|
|
|
|
|
|
-class CustomFieldSerializer(serializers.BaseSerializer):
|
|
|
- """
|
|
|
- Extends ModelSerializer to render any CustomFields and their values associated with an object.
|
|
|
- """
|
|
|
-
|
|
|
- def to_representation(self, manager):
|
|
|
-
|
|
|
- # Initialize custom fields dictionary
|
|
|
- data = {f.name: None for f in self.parent._custom_fields}
|
|
|
+class CustomFieldsSerializer(serializers.BaseSerializer):
|
|
|
|
|
|
- # Assign CustomFieldValues from database
|
|
|
- for cfv in manager.all():
|
|
|
- if cfv.field.type == CF_TYPE_SELECT:
|
|
|
- data[cfv.field.name] = CustomFieldChoiceSerializer(cfv.value).data
|
|
|
- else:
|
|
|
- data[cfv.field.name] = cfv.value
|
|
|
-
|
|
|
- return data
|
|
|
+ def to_representation(self, obj):
|
|
|
+ return obj
|
|
|
|
|
|
|
|
|
class CustomFieldModelSerializer(serializers.ModelSerializer):
|
|
|
- custom_fields = CustomFieldSerializer(source='custom_field_values')
|
|
|
+ """
|
|
|
+ Extends ModelSerializer to render any CustomFields and their values associated with an object.
|
|
|
+ """
|
|
|
+ custom_fields = CustomFieldsSerializer()
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
super(CustomFieldModelSerializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
- # Cache the list of custom fields for this model
|
|
|
+ # Retrieve the set of CustomFields which apply to this type of object
|
|
|
content_type = ContentType.objects.get_for_model(self.Meta.model)
|
|
|
- self._custom_fields = CustomField.objects.filter(obj_type=content_type)
|
|
|
+ custom_fields = {f.name: None for f in CustomField.objects.filter(obj_type=content_type)}
|
|
|
+
|
|
|
+ # Assign CustomFieldValues from database
|
|
|
+ for cfv in self.instance.custom_field_values.all():
|
|
|
+ if cfv.field.type == CF_TYPE_SELECT:
|
|
|
+ custom_fields[cfv.field.name] = CustomFieldChoiceSerializer(cfv.value).data
|
|
|
+ else:
|
|
|
+ custom_fields[cfv.field.name] = cfv.value
|
|
|
+
|
|
|
+ self.instance.custom_fields = custom_fields
|
|
|
|
|
|
|
|
|
class CustomFieldChoiceSerializer(serializers.ModelSerializer):
|
|
|
+ """
|
|
|
+ Imitate utilities.api.ChoiceFieldSerializer
|
|
|
+ """
|
|
|
+ value = serializers.IntegerField(source='pk')
|
|
|
+ label = serializers.CharField(source='value')
|
|
|
|
|
|
class Meta:
|
|
|
model = CustomFieldChoice
|
|
|
- fields = ['id', 'value']
|
|
|
+ fields = ['value', 'label']
|