2023-12-05 03:16:34 +00:00
|
|
|
from django.db.models import Count
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-12-18 09:56:16 +00:00
|
|
|
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
|
2023-12-05 03:16:34 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2023-12-18 09:56:16 +00:00
|
|
|
from .const import label_resource_types
|
2023-12-05 03:16:34 +00:00
|
|
|
from .models import Label, LabeledResource
|
|
|
|
|
|
|
|
__all__ = ['LabelSerializer', 'LabeledResourceSerializer', 'ContentTypeResourceSerializer']
|
|
|
|
|
|
|
|
|
|
|
|
class LabelSerializer(BulkOrgResourceModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Label
|
2023-12-19 10:46:02 +00:00
|
|
|
fields = [
|
2024-06-04 11:08:16 +00:00
|
|
|
'id', 'name', 'value', 'color', 'res_count',
|
|
|
|
'comment', 'date_created', 'date_updated'
|
2023-12-19 10:46:02 +00:00
|
|
|
]
|
2023-12-05 03:16:34 +00:00
|
|
|
read_only_fields = ('date_created', 'date_updated', 'res_count')
|
|
|
|
extra_kwargs = {
|
2024-02-22 09:46:12 +00:00
|
|
|
'res_count': {'label': _('Resource count')},
|
2023-12-05 03:16:34 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 10:46:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def validate_name(value):
|
|
|
|
if ':' in value or ',' in value:
|
|
|
|
raise serializers.ValidationError(_('Cannot contain ":,"'))
|
|
|
|
return value
|
|
|
|
|
|
|
|
def validate_value(self, value):
|
|
|
|
return self.validate_name(value)
|
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2024-01-18 11:43:55 +00:00
|
|
|
queryset = queryset.annotate(res_count=Count('labeled_resources', distinct=True))
|
2023-12-05 03:16:34 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class LabeledResourceSerializer(serializers.ModelSerializer):
|
2023-12-18 09:56:16 +00:00
|
|
|
res_type = LabeledChoiceField(
|
|
|
|
choices=[], label=_("Resource type"), source='res_type_id', required=False
|
2023-12-05 03:16:34 +00:00
|
|
|
)
|
2023-12-19 10:42:44 +00:00
|
|
|
label = ObjectRelatedField(queryset=Label.objects, attrs=('id', 'display_name'), label=_("Label"))
|
2023-12-05 03:16:34 +00:00
|
|
|
resource = serializers.CharField(label=_("Resource"))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = LabeledResource
|
|
|
|
fields = ('id', 'label', 'res_type', 'res_id', 'date_created', 'resource', 'date_updated')
|
|
|
|
read_only_fields = ('date_created', 'date_updated', 'resource')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
queryset = queryset.select_related('label', 'res_type')
|
|
|
|
return queryset
|
|
|
|
|
2023-12-18 09:56:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_res_type_choices()
|
|
|
|
|
|
|
|
def set_res_type_choices(self):
|
|
|
|
res_type_field = self.fields.get('res_type')
|
|
|
|
if not res_type_field:
|
|
|
|
return
|
|
|
|
|
|
|
|
res_type_field.choices = [(ct.id, ct.name) for ct in label_resource_types]
|
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
|
|
|
|
class ContentTypeResourceSerializer(serializers.Serializer):
|
|
|
|
id = serializers.CharField()
|
|
|
|
name = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_name(obj):
|
|
|
|
return str(obj)
|