Added check for double serializer class registering.
Removed AssignmentShortPollSerializer.
This commit is contained in:
parent
52193e9fb2
commit
e6daf32924
@ -80,25 +80,6 @@ class AssignmentOptionSerializer(ModelSerializer):
|
|||||||
return obj.poll.assignment.is_elected(obj.candidate)
|
return obj.poll.assignment.is_elected(obj.candidate)
|
||||||
|
|
||||||
|
|
||||||
class FilterPollListSerializer(ListSerializer):
|
|
||||||
"""
|
|
||||||
Customized serializer to filter polls (exclude unpublished).
|
|
||||||
"""
|
|
||||||
|
|
||||||
def to_representation(self, data):
|
|
||||||
"""
|
|
||||||
List of object instances -> List of dicts of primitive datatypes.
|
|
||||||
|
|
||||||
This method is adapted to filter the data and exclude unpublished polls.
|
|
||||||
"""
|
|
||||||
# Dealing with nested relationships, data can be a Manager,
|
|
||||||
# so, first get a queryset from the Manager if needed
|
|
||||||
iterable = (
|
|
||||||
data.filter(published=True) if isinstance(data, models.Manager) else data
|
|
||||||
)
|
|
||||||
return [self.child.to_representation(item) for item in iterable]
|
|
||||||
|
|
||||||
|
|
||||||
class AssignmentAllPollSerializer(ModelSerializer):
|
class AssignmentAllPollSerializer(ModelSerializer):
|
||||||
"""
|
"""
|
||||||
Serializer for assignment.models.AssignmentPoll objects.
|
Serializer for assignment.models.AssignmentPoll objects.
|
||||||
@ -198,31 +179,6 @@ class AssignmentAllPollSerializer(ModelSerializer):
|
|||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class AssignmentShortPollSerializer(AssignmentAllPollSerializer):
|
|
||||||
"""
|
|
||||||
Serializer for assignment.models.AssignmentPoll objects.
|
|
||||||
|
|
||||||
Serializes only short polls (excluded unpublished polls).
|
|
||||||
"""
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
list_serializer_class = FilterPollListSerializer
|
|
||||||
model = AssignmentPoll
|
|
||||||
fields = (
|
|
||||||
"id",
|
|
||||||
"pollmethod",
|
|
||||||
"description",
|
|
||||||
"published",
|
|
||||||
"options",
|
|
||||||
"votesabstain",
|
|
||||||
"votesno",
|
|
||||||
"votesvalid",
|
|
||||||
"votesinvalid",
|
|
||||||
"votescast",
|
|
||||||
"has_votes",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class AssignmentFullSerializer(ModelSerializer):
|
class AssignmentFullSerializer(ModelSerializer):
|
||||||
"""
|
"""
|
||||||
Serializer for assignment.models.Assignment objects. With all polls.
|
Serializer for assignment.models.Assignment objects. With all polls.
|
||||||
|
@ -204,6 +204,13 @@ class ModelSerializerRegisterer(SerializerMetaclass):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
if model_serializer_classes.get(model) is not None:
|
||||||
|
error = (
|
||||||
|
f"Model {model} is already used for the serializer class "
|
||||||
|
f"{model_serializer_classes[model]} and cannot be registered "
|
||||||
|
f"for serializer class {serializer_class}."
|
||||||
|
)
|
||||||
|
raise RuntimeError(error)
|
||||||
model_serializer_classes[model] = serializer_class
|
model_serializer_classes[model] = serializer_class
|
||||||
return serializer_class
|
return serializer_class
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user