Skip to content

Commit

Permalink
feat: Refactor field value handling in Issue class
Browse files Browse the repository at this point in the history
This change refactors the field value handling logic in the `Issue` class:

1. Removes the `_cast_and_set` static method, as the field value setting is now handled directly using `setattr`.
2. Simplifies the `editable_fields` method by directly setting the field values using `setattr` instead of calling `_cast_and_set`.
3. Removes the unnecessary try-except block for handling value casting, as the field types are now properly defined in the `attrs.py` module.
4. Updates the `_update_options_for_each_conditional` method to only add new fields to the `editable_fields` dictionary, avoiding unnecessary updates.

This refactoring improves the readability and maintainability of the `Issue` class by simplifying the field value handling logic.
  • Loading branch information
ronaldokun committed Aug 6, 2024
1 parent 8968422 commit 1807f1a
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions fiscaliza/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,24 +335,26 @@ def _append_irregularity_options(
editable_fields["irregularidade"].options = options
return editable_fields

@staticmethod
def _cast_and_set(field, value):
try:
match field.dtype:
case "string":
value = str(value)
case "int":
value = int(value)
case "float":
value = float(value)
case "list":
value = listify(value)
case _:
print(f"Unknown dtype {field.dtype}, casting skipped...")
except ValueError as e:
print(f"Error casting value {value} to dtype {field.dtype}: {e}")

setattr(field, "value", value)
# @staticmethod
# def _cast_and_set(field, value):
# try:
# match field.dtype:
# case "string":
# value = str(value)
# case "int":
# value = int(value)
# case "float":
# value = float(value)
# case "list":
# value = listify(value)
# case _:
# print(f"Unknown dtype {field.dtype}, casting skipped...")
# except ValueError as e:
# print(
# f"Error casting {field.name} value {value} to dtype {field.dtype}: {e}"
# )
# setattr(field, "value", value)
# return field

@cached_property
def editable_fields(self) -> dict:
Expand All @@ -367,7 +369,7 @@ def editable_fields(self) -> dict:
self.attrs[key] = [str(k) for k in self.attrs[key]]
else:
self.attrs[key] = str(self.attrs[key])
self._cast_and_set(field, self.attrs[key])
setattr(field, "value", self.attrs[key])
if key == "fiscais":
setattr(field, "options", self.attrs["membros"])
elif key == "fiscal_responsavel":
Expand Down Expand Up @@ -451,7 +453,9 @@ def _update_fields(dados: dict, editable_fields: dict) -> dict:
Check if the data to be submitted to the Fiscaliza server is complete and valid.
"""
insert, delete = Issue._update_options_for_each_conditional(dados)
editable_fields.update(insert)
for key, value in insert.items():
if key not in editable_fields:
editable_fields[key] = value
for key in delete:
if key in editable_fields:
del editable_fields[key]
Expand Down

0 comments on commit 1807f1a

Please sign in to comment.