* fix: created dashboard, widgets and dashboard widget model * fix: new user home dashboard * chore: recent projects list * chore: recent collaborators * chore: priority order change * chore: payload changes * chore: collaborator's active issue count * chore: all dashboard widgets added with services and typs * chore: centered metric for pie chart * chore: widget filters * chore: created issue filter * fix: created and assigned issues payload change * chore: created issue payload change * fix: date filter change * chore: implement filters * fix: added expansion fields * fix: changed issue structure with relation * chore: new issues response * fix: project member fix * chore: updated issue_relation structure * chore: code cleanup * chore: update issues response and added empty states * fix: button text wrap * chore: update empty state messages * fix: filters * chore: update dark mode empty states * build-error: Type check in the issue relation service * fix: issues redirection * fix: project empty state * chore: project member active check * chore: project member check in state and priority * chore: remove console logs and replace harcoded values with constants * fix: code refactoring * fix: key name changed * refactor: mapping through similar components using an array * fix: build errors --------- Co-authored-by: Aaryan Khandelwal <aaryankhandu123@gmail.com> Co-authored-by: gurusainath <gurusainath007@gmail.com>
144 lines
6.2 KiB
Python
144 lines
6.2 KiB
Python
from rest_framework import serializers
|
|
|
|
|
|
class BaseSerializer(serializers.ModelSerializer):
|
|
id = serializers.PrimaryKeyRelatedField(read_only=True)
|
|
|
|
|
|
class DynamicBaseSerializer(BaseSerializer):
|
|
def __init__(self, *args, **kwargs):
|
|
# If 'fields' is provided in the arguments, remove it and store it separately.
|
|
# This is done so as not to pass this custom argument up to the superclass.
|
|
fields = kwargs.pop("fields", [])
|
|
self.expand = kwargs.pop("expand", []) or []
|
|
fields = self.expand
|
|
|
|
# Call the initialization of the superclass.
|
|
super().__init__(*args, **kwargs)
|
|
# If 'fields' was provided, filter the fields of the serializer accordingly.
|
|
if fields is not None:
|
|
self.fields = self._filter_fields(fields)
|
|
|
|
def _filter_fields(self, fields):
|
|
"""
|
|
Adjust the serializer's fields based on the provided 'fields' list.
|
|
|
|
:param fields: List or dictionary specifying which fields to include in the serializer.
|
|
:return: The updated fields for the serializer.
|
|
"""
|
|
# Check each field_name in the provided fields.
|
|
for field_name in fields:
|
|
# If the field is a dictionary (indicating nested fields),
|
|
# loop through its keys and values.
|
|
if isinstance(field_name, dict):
|
|
for key, value in field_name.items():
|
|
# If the value of this nested field is a list,
|
|
# perform a recursive filter on it.
|
|
if isinstance(value, list):
|
|
self._filter_fields(self.fields[key], value)
|
|
|
|
# Create a list to store allowed fields.
|
|
allowed = []
|
|
for item in fields:
|
|
# If the item is a string, it directly represents a field's name.
|
|
if isinstance(item, str):
|
|
allowed.append(item)
|
|
# If the item is a dictionary, it represents a nested field.
|
|
# Add the key of this dictionary to the allowed list.
|
|
elif isinstance(item, dict):
|
|
allowed.append(list(item.keys())[0])
|
|
|
|
for field in allowed:
|
|
if field not in self.fields:
|
|
from . import (
|
|
WorkspaceLiteSerializer,
|
|
ProjectLiteSerializer,
|
|
UserLiteSerializer,
|
|
StateLiteSerializer,
|
|
IssueSerializer,
|
|
LabelSerializer,
|
|
CycleIssueSerializer,
|
|
IssueFlatSerializer,
|
|
IssueRelationSerializer,
|
|
)
|
|
|
|
# Expansion mapper
|
|
expansion = {
|
|
"user": UserLiteSerializer,
|
|
"workspace": WorkspaceLiteSerializer,
|
|
"project": ProjectLiteSerializer,
|
|
"default_assignee": UserLiteSerializer,
|
|
"project_lead": UserLiteSerializer,
|
|
"state": StateLiteSerializer,
|
|
"created_by": UserLiteSerializer,
|
|
"issue": IssueSerializer,
|
|
"actor": UserLiteSerializer,
|
|
"owned_by": UserLiteSerializer,
|
|
"members": UserLiteSerializer,
|
|
"assignees": UserLiteSerializer,
|
|
"labels": LabelSerializer,
|
|
"issue_cycle": CycleIssueSerializer,
|
|
"parent": IssueSerializer,
|
|
"issue_relation": IssueRelationSerializer,
|
|
}
|
|
|
|
self.fields[field] = expansion[field](many=True if field in ["members", "assignees", "labels", "issue_cycle", "issue_relation"] else False)
|
|
|
|
return self.fields
|
|
|
|
def to_representation(self, instance):
|
|
response = super().to_representation(instance)
|
|
|
|
# Ensure 'expand' is iterable before processing
|
|
if self.expand:
|
|
for expand in self.expand:
|
|
if expand in self.fields:
|
|
# Import all the expandable serializers
|
|
from . import (
|
|
WorkspaceLiteSerializer,
|
|
ProjectLiteSerializer,
|
|
UserLiteSerializer,
|
|
StateLiteSerializer,
|
|
IssueSerializer,
|
|
LabelSerializer,
|
|
CycleIssueSerializer,
|
|
IssueRelationSerializer,
|
|
)
|
|
|
|
# Expansion mapper
|
|
expansion = {
|
|
"user": UserLiteSerializer,
|
|
"workspace": WorkspaceLiteSerializer,
|
|
"project": ProjectLiteSerializer,
|
|
"default_assignee": UserLiteSerializer,
|
|
"project_lead": UserLiteSerializer,
|
|
"state": StateLiteSerializer,
|
|
"created_by": UserLiteSerializer,
|
|
"issue": IssueSerializer,
|
|
"actor": UserLiteSerializer,
|
|
"owned_by": UserLiteSerializer,
|
|
"members": UserLiteSerializer,
|
|
"assignees": UserLiteSerializer,
|
|
"labels": LabelSerializer,
|
|
"issue_cycle": CycleIssueSerializer,
|
|
"parent": IssueSerializer,
|
|
"issue_relation": IssueRelationSerializer
|
|
}
|
|
# Check if field in expansion then expand the field
|
|
if expand in expansion:
|
|
if isinstance(response.get(expand), list):
|
|
exp_serializer = expansion[expand](
|
|
getattr(instance, expand), many=True
|
|
)
|
|
else:
|
|
exp_serializer = expansion[expand](
|
|
getattr(instance, expand)
|
|
)
|
|
response[expand] = exp_serializer.data
|
|
else:
|
|
# You might need to handle this case differently
|
|
response[expand] = getattr(
|
|
instance, f"{expand}_id", None
|
|
)
|
|
|
|
return response
|