Skip to content

Commit

Permalink
refactor: remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Nov 7, 2023
1 parent 06efd55 commit d400a54
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions openedx_tagging/core/tagging/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,33 +186,15 @@ class TaxonomyImportBodySerializer(serializers.Serializer): # pylint: disable=a
taxonomy_description = serializers.CharField(default="")
file = serializers.FileField(required=True)

def validate_file(self, file):
def validate(self, data):
"""
Validates the file extension
Validates the file extension and add parser_format to the data
"""
filename = file.name
filename = data["file"].name
ext = filename.split('.')[-1]
parser_format = getattr(ParserFormat, ext.upper(), None)
if not parser_format:
raise serializers.ValidationError(f'File type not supported: {ext.lower()}')
raise serializers.ValidationError({"file": f'File type not supported: {ext.lower()}'}, 'file')

return file

def get_parser_format(self, obj) -> ParserFormat:
"""
Returns the ParserFormat based on the file extension
"""
filename = obj["file"].name
ext = filename.split('.')[-1]
parser_format = getattr(ParserFormat, ext.upper(), None)
assert parser_format, f'File type not supported: ${ext.lower()}'

return parser_format

def to_internal_value(self, data):
"""
Adds the parser_format to the validated data
"""
validated_data = super().to_internal_value(data)
validated_data['parser_format'] = self.get_parser_format(data)
return validated_data
data['parser_format'] = parser_format
return data

0 comments on commit d400a54

Please sign in to comment.