class CSVUploadSerializerBase(serializers.ModelSerializer): rows = serializers.SerializerMethodField('get_rows') provider = serializers.CharField(read_only=True, source='provider.name') created_by = serializers.CharField(read_only=True, source='created_by.user.username') body = JSONField() def get_rows(self, obj): return len(obj.body) def validate_month(self, attrs, source): value = attrs[source] validate_first_of_month(value) return attrs def validate_body(self, attrs, source): value = attrs[source] if len(value) == 0: raise serializers.ValidationError('No rows found.') ProviderCSVValidator(value).validate() return attrs class Meta: model = CSVUpload fields = ()
class ExtendedEligibilityCheckSerializer(EligibilityCheckSerializer): calculations = JSONField(read_only=True) class Meta(EligibilityCheckSerializer.Meta): fields = list(EligibilityCheckSerializer.Meta.fields) + [ 'calculations' ]
class EligibilityCheckSerializerBase(ClaModelSerializer): property_set = PropertySerializerBase(allow_add_remove=True, many=True, required=False) you = PersonSerializerBase(required=False) partner = PersonSerializerBase(required=False) category = serializers.SlugRelatedField(slug_field='code', required=False) your_problem_notes = serializers.CharField(max_length=500, required=False) notes = serializers.CharField(max_length=5000, required=False) specific_benefits = JSONField(required=False) class Meta: model = EligibilityCheck fields = () def validate_property_set(self, attrs, source): """ Checks that only one main property is selected """ if source in attrs: main_props = [prop for prop in attrs[source] if prop.main] if len(main_props) > 1: raise serializers.ValidationError( "Only one main property allowed") return attrs def validate_specific_benefits(self, attrs, source): if source in attrs: data_benefits = attrs[source] if data_benefits: extra_fields = set(data_benefits.keys()) - set( SPECIFIC_BENEFITS.CHOICES_DICT.keys()) if extra_fields: raise serializers.ValidationError( "Fields %s not recognised" % ', '.join(list(extra_fields))) # translate into safer bool values data_benefits = {k: bool(v) for k, v in data_benefits.items()} attrs[source] = data_benefits return attrs def save(self, **kwargs): # TODO double-check this, not sure... if not self.object.on_passported_benefits: self.object.specific_benefits = None obj = super(EligibilityCheckSerializerBase, self).save(**kwargs) obj.update_state() diff = obj.diff if 'category' in diff: # if the category has been updated then reset mattertype on # corresponding case obj.reset_matter_types() return obj
class CSVUploadSerializer(CSVUploadSerializerBase): body = JSONField(write_only=True) class Meta(CSVUploadSerializerBase.Meta): fields = [ "id", "provider", "created_by", "comment", "rows", "body", "month", "created", "modified" ]
class LogSerializerBase(ClaModelSerializer): code = serializers.CharField(read_only=True) created_by = serializers.CharField(read_only=True, source="created_by.username") created = serializers.DateTimeField(read_only=True) level = serializers.CharField(read_only=True) type = serializers.CharField(read_only=True) timer_id = serializers.IntegerField(read_only=True) notes = serializers.CharField(read_only=True) patch = JSONField(read_only=True) class Meta(object): model = Log fields = ()
class CSVUploadSerializer(CSVUploadSerializerBase): body = JSONField(write_only=True) class Meta(CSVUploadSerializerBase.Meta): fields = [ 'id', 'provider', 'created_by', 'comment', 'rows', 'body', 'month', 'created', ]