@@ -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