Skip to content

Commit

Permalink
refactor if/elif/else construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanov-dv committed Dec 3, 2024
1 parent 0d47d7b commit cfe4e93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
19 changes: 9 additions & 10 deletions drf_excel/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def init_value(self, value):
def custom_mapping(self):
if type(self.mapping) is str:
return self.value.get(self.mapping)
elif callable(self.mapping):
if callable(self.mapping):
return self.mapping(self.value)
return self.value

Expand Down Expand Up @@ -89,9 +89,9 @@ def init_value(self, value):
with contextlib.suppress(Exception):
if isinstance(self.drf_field, IntegerField) and type(value) is not int:
return int(value)
elif isinstance(self.drf_field, FloatField) and type(value) is not float:
if isinstance(self.drf_field, FloatField) and type(value) is not float:
return float(value)
elif (
if (
isinstance(self.drf_field, DecimalField) and type(value) is not Decimal
):
return Decimal(value)
Expand Down Expand Up @@ -122,7 +122,7 @@ def _parse_date(self, value, setting_format, iso_parse_func):
parsed_datetime = datetime.datetime.strptime(value, parse_format)
if isinstance(self.drf_field, TimeField):
return parsed_datetime.time()
elif isinstance(self.drf_field, DateField):
if isinstance(self.drf_field, DateField):
return parsed_datetime.date()
return parsed_datetime

Expand All @@ -136,12 +136,12 @@ def init_value(self, value):
return self._parse_date(
value, "DATETIME_FORMAT", parse_datetime
).replace(tzinfo=None)
elif (
if (
isinstance(self.drf_field, DateField)
and type(value) is not datetime.date
):
return self._parse_date(value, "DATE_FORMAT", parse_date)
elif (
if (
isinstance(self.drf_field, TimeField)
and type(value) is not datetime.time
):
Expand Down Expand Up @@ -177,10 +177,9 @@ def prep_value(self) -> Any:
):
# array of array; write as json
return json.dumps(self.value, ensure_ascii=False)
else:
# Flatten the array into a comma separated string to fit
# in a single spreadsheet column
return self.list_sep.join(map(str, self.value))
# Flatten the array into a comma separated string to fit
# in a single spreadsheet column
return self.list_sep.join(map(str, self.value))


class XLSXBooleanField(XLSXField):
Expand Down
9 changes: 4 additions & 5 deletions drf_excel/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ def _get_label(parent_label, label_sep, obj):
if parent_label
else str(v.label)
)
else:
return False
return False

Check warning on line 273 in drf_excel/renderers.py

View check run for this annotation

Codecov / codecov/patch

drf_excel/renderers.py#L273

Added line #L273 was not covered by tests

_header_dict = {}
_fields = serializer.get_fields()
Expand Down Expand Up @@ -378,11 +377,11 @@ def _drf_to_xlsx_field(self, key, value) -> XLSXField:

if isinstance(field, BooleanField):
return XLSXBooleanField(boolean_display=self.boolean_display, **kwargs)
elif isinstance(field, (IntegerField, FloatField, DecimalField)):
if isinstance(field, (IntegerField, FloatField, DecimalField)):
return XLSXNumberField(**kwargs)
elif isinstance(field, (DateTimeField, DateField, TimeField)):
if isinstance(field, (DateTimeField, DateField, TimeField)):
return XLSXDateField(**kwargs)
elif (
if (
isinstance(field, ListField)
or isinstance(value, Iterable)
and not isinstance(value, str)
Expand Down

0 comments on commit cfe4e93

Please sign in to comment.