Skip to content

Commit

Permalink
Merge pull request #67 from thesaintraphael/main
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
FlipperPA authored Feb 3, 2023
2 parents 87ddcb4 + 092f7b6 commit 2a6a429
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
21 changes: 10 additions & 11 deletions drf_excel/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import datetime
import json
from decimal import Decimal
Expand Down Expand Up @@ -84,15 +85,15 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)

def init_value(self, value):
try:

with contextlib.suppress(Exception):
if isinstance(self.drf_field, IntegerField) and type(value) != int:
return int(value)
elif isinstance(self.drf_field, FloatField) and type(value) != float:
return float(value)
elif isinstance(self.drf_field, DecimalField) and type(value) != Decimal:
return Decimal(value)
except:
pass

return value

def prep_cell(self, cell: Cell):
Expand All @@ -114,16 +115,14 @@ def _parse_date(self, value, setting_format, iso_parse_func):
drf_format = getattr(self.drf_field, "format", None)
# Otherwise, use DRF output format: DATETIME_FORMAT, DATE_FORMAT or TIME_FORMAT
parse_format = drf_format or getattr(drf_settings, setting_format)
# Use the provided iso parse function for the special case ISO_8601
if parse_format.lower() == ISO_8601:
return iso_parse_func(value)
else:
parsed_datetime = datetime.datetime.strptime(value, parse_format)
if isinstance(self.drf_field, TimeField):
return parsed_datetime.time()
elif isinstance(self.drf_field, DateField):
return parsed_datetime.date()
return parsed_datetime
parsed_datetime = datetime.datetime.strptime(value, parse_format)
if isinstance(self.drf_field, TimeField):
return parsed_datetime.time()
elif isinstance(self.drf_field, DateField):
return parsed_datetime.date()
return parsed_datetime

def init_value(self, value):
# Set tzinfo to None on datetime and time types since timezones are not supported in Excel
Expand Down
26 changes: 6 additions & 20 deletions drf_excel/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):

# Set the header row
if use_header:
last_col_letter = "G"
if column_count:
last_col_letter = get_column_letter(column_count)
last_col_letter = get_column_letter(column_count) if column_count else "G"
self.ws.merge_cells(f"A1:{last_col_letter}1")

cell = self.ws.cell(row=1, column=1, value=header_title)
Expand Down Expand Up @@ -217,9 +215,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):

def _check_validation_data(self, data):
detail_key = "detail"
if detail_key in data:
return False
return True
return detail_key not in data

def _serializer_fields(self, serializer, parent_key="", key_sep="."):
_fields_dict = {}
Expand Down Expand Up @@ -247,10 +243,7 @@ def _flatten_serializer_keys(

def _get_label(parent_label, label_sep, obj):
if getattr(v, "label", None):
if parent_label:
return f"{parent_label}{label_sep}{v.label}"
else:
return str(v.label)
return f"{parent_label}{label_sep}{v.label}" if parent_label else str(v.label)
else:
return False

Expand Down Expand Up @@ -348,18 +341,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)
or isinstance(field, FloatField)
or isinstance(field, DecimalField)
):
elif isinstance(field, (IntegerField, FloatField, DecimalField)):
return XLSXNumberField(**kwargs)
elif (
isinstance(field, DateTimeField)
or isinstance(field, DateField)
or isinstance(field, TimeField)
):
elif isinstance(field, (DateTimeField, DateField, TimeField)):
return XLSXDateField(**kwargs)

elif (
isinstance(field, ListField)
or isinstance(value, Iterable)
Expand Down
4 changes: 2 additions & 2 deletions drf_excel/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_attribute(get_from, prop_name, default=None):
"""
prop = getattr(get_from, prop_name, None)
if not prop:
prop_func = getattr(get_from, "get_{}".format(prop_name), None)
prop_func = getattr(get_from, f"get_{prop_name}", None)
if prop_func:
prop = prop_func()
if prop is None:
Expand All @@ -72,7 +72,7 @@ def get_attribute(get_from, prop_name, default=None):


def get_setting(key, default=None):
return getattr(django_settings, "DRF_EXCEL_" + key, default)
return getattr(django_settings, f"DRF_EXCEL_{key}", default)


def sanitize_value(value):
Expand Down

0 comments on commit 2a6a429

Please sign in to comment.