Skip to content

Commit 9553aca

Browse files
committed
bugfix: ClassFeature serializer table_data/gained_at no longer mutually exclusive
1 parent e320823 commit 9553aca

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

api_v2/models/characterclass.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ class ClassFeature(HasName, HasDescription, FromDocument):
5050

5151
parent = models.ForeignKey('CharacterClass', on_delete=models.CASCADE, related_name="features")
5252

53-
def gained_at(self):
54-
return self.feature_items.filter(column_value__isnull=True)
55-
56-
def table_data(self):
57-
"""Returns an array of tabular data relating to the feature. Each
58-
array element is a table-row of data. Not needed for most features."""
59-
60-
return self.feature_items.filter(column_value__isnull=False)
61-
6253
# Infer the type of this feature based on the `key`
6354
@property
6455
def feature_type(self):

api_v2/serializers/characterclass.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,35 @@ class ClassFeatureSerializer(GameContentSerializer):
2828
feature_items = ClassFeaturePrefetchSerializer(many=True, read_only=True)
2929

3030
def to_representation(self, instance):
31+
"""
32+
'feature_items' field contains tabulated and non-tabulated data. These
33+
have different uses and must be split into the 'gained_at' and
34+
'table_data' fields before being returned by the serializer
35+
"""
3136
# run 'to_representation' on super-class (GameContentSerializer)
3237
representation = super().to_representation(instance)
33-
34-
# Filters non-table data from FeatureItems
35-
gained_at = [
36-
ClassFeatureItemSerializer(item).data
37-
for item in instance.feature_items.all()
38-
if item.column_value is None
39-
]
40-
41-
# Filters table data from FeatureItems
42-
table_data = [
43-
ClassFeatureColumnItemSerializer(item).data
44-
for item in instance.feature_items.all()
45-
if item.column_value is not None
46-
]
47-
48-
# replace 'feature_items' with 'gained_at' and 'column_data' in representation
49-
representation['gained_at'] = gained_at
50-
representation['table_data'] = table_data
38+
39+
# Split FeatureItems into tabulated and non-tabulated data arrays
40+
table_data = []
41+
non_table_data = []
42+
for item in instance.feature_items.all():
43+
if item.column_value is None:
44+
non_table_data.append(item)
45+
else:
46+
table_data.append(item)
47+
48+
# If a feature has tabulated data AND a description, take its lowest
49+
# level FeatureItem and add it to the non-tabulated data.
50+
if len(table_data) > 0 and instance.desc != '[Column data]':
51+
first_level_gained = min(table_data, key=lambda x: x.level)
52+
non_table_data.append(first_level_gained)
53+
54+
# serialize data and add it to representation
55+
representation['gained_at'] = [ClassFeatureItemSerializer(item).data for item in non_table_data]
56+
representation['table_data'] = [ClassFeatureColumnItemSerializer(item).data for item in table_data]
57+
58+
# remove feature_items field to avoid data duplication
5159
del representation['feature_items']
52-
5360
return representation
5461

5562
class Meta:

0 commit comments

Comments
 (0)