-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rebuild aggregation serializer
- Loading branch information
Simon Legtenborg
committed
May 14, 2024
1 parent
4e0341a
commit e2442eb
Showing
4 changed files
with
60 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
-r requirements.txt | ||
coverage==7.3.2 | ||
psycopg2==2.9.9 | ||
mssql-django==1.5 | ||
ruff==0.3.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
import decimal | ||
|
||
from rest_framework import serializers | ||
from rest_framework.serializers import BaseSerializer | ||
|
||
|
||
class AggregationSerializer(BaseSerializer): | ||
def to_representation(self, instance): | ||
ret = {} | ||
for field in instance.keys(): | ||
if isinstance(instance[field], int): | ||
ret[field] = serializers.IntegerField(read_only=True).to_representation(instance[field]) | ||
elif isinstance(instance[field], float) or isinstance(instance[field], decimal.Decimal): | ||
ret[field] = serializers.FloatField(read_only=True).to_representation(float(instance[field])) | ||
else: | ||
ret[field] = serializers.CharField(read_only=True).to_representation(instance[field]) | ||
return ret | ||
|
||
|
||
class PassTroughField(serializers.Field): | ||
|
||
def to_representation(self, value): | ||
return value | ||
|
||
def to_internal_value(self, data): | ||
return data | ||
|
||
|
||
class AggregationSerializer(serializers.Serializer): | ||
""" | ||
Serializer for aggregation requests. | ||
Authored by Tim Streicher | ||
""" | ||
|
||
value = serializers.SerializerMethodField(method_name='serialize_value') | ||
|
||
def serialize_value(self, obj): | ||
return obj['value'] | ||
|
||
def __init__(self, *args, **kwargs): | ||
# Instantiate the superclass normally | ||
super(AggregationSerializer, self).__init__(*args, **kwargs) | ||
|
||
fields = self.context['request'].query_params.get('group_by') | ||
allowed = {self.context['name']} | ||
if fields: | ||
fields = fields.split(',') | ||
allowed.update(fields) | ||
else: | ||
allowed.add('group') | ||
|
||
# Drop any fields that are not specified in the `fields` argument except value. | ||
existing = set(self.fields.keys()) | ||
for field_name in existing - allowed: | ||
self.fields.pop(field_name) | ||
|
||
for field_name in allowed: | ||
field = self.fields.get(field_name) | ||
# add field for unknown field_names | ||
if field is None: | ||
self.fields[field_name] = PassTroughField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters