Skip to content

Commit

Permalink
Fix various ruff lint checks
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyNotHugo committed Oct 3, 2024
1 parent c73828c commit 7de6e29
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
27 changes: 12 additions & 15 deletions django_afip/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from typing import ClassVar
from typing import Generic
from typing import TypeVar
from typing import Dict
from typing import Optional as TypeOptional
from uuid import uuid4

from django import VERSION as DJANGO_VERSION
Expand All @@ -33,7 +31,6 @@
from django.db.models import F
from django.db.models import Q
from django.db.models import Sum
from django.db.models.fields.files import FieldFile
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from lxml import etree
Expand All @@ -54,6 +51,7 @@

if TYPE_CHECKING:
from django.core.files.storage import Storage
from django.db.models.fields.files import FieldFile

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -129,11 +127,11 @@ def first_currency() -> int | None:
def _get_storage_from_settings(setting_name: str) -> Storage:
path = getattr(settings, setting_name, None)
if not path:
if DJANGO_VERSION >= (4,2):
if DJANGO_VERSION >= (4, 2):
from django.core.files.storage import default_storage

return default_storage
else:
return import_string(settings.DEFAULT_FILE_STORAGE)()
return import_string(settings.DEFAULT_FILE_STORAGE)()
return import_string(path)


Expand Down Expand Up @@ -428,7 +426,7 @@ class TaxPayer(models.Model):
help_text=_("A logo to use when generating printable receipts."),
)

_old_files: Dict[str, TypeOptional[FieldFile]] = {}
_old_files: dict[str, FieldFile | None]

@property
def logo_as_data_uri(self) -> str:
Expand Down Expand Up @@ -574,21 +572,20 @@ def fetch_points_of_sales(
results = []

existing_pos_dict = {
pos.number: pos
for pos in PointOfSales.objects.filter(owner=self)
pos.number: pos for pos in PointOfSales.objects.filter(owner=self)
}

for pos_data in response.ResultGet.PtoVenta:
pos_number = pos_data.Nro
issuance_type = pos_data.EmisionTipo
is_blocked = pos_data.Bloqueado == "S"
drop_date = parsers.parse_date_maybe(pos_data.FchBaja)
drop_date = parsers.parse_date_maybe(pos_data.FchBaja)

if pos_number in existing_pos_dict:
pos = existing_pos_dict[pos_number]

is_modified = False

if pos.issuance_type != issuance_type:
pos.issuance_type = issuance_type
is_modified = True
Expand Down Expand Up @@ -1381,8 +1378,8 @@ def approximate_date(self: Receipt) -> bool:
If a receipt should have been validated in a past date, adjust its date as close
as possible:
- Receipts can only be validated with dates as far as 14 days ago for
services and 5 days ago for products. If the receipt date is older
- Receipts can only be validated with dates as far as 14 days ago for
services and 5 days ago for products. If the receipt date is older
than that, set it to 14 or 5 days ago.
- If other receipts have been validated on a more recent date, the receipt
cannot be older than the most recent one.
Expand Down
19 changes: 12 additions & 7 deletions django_afip/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from typing import TYPE_CHECKING

from django.db.models.signals import pre_save, post_save
from django.db.models.signals import post_save
from django.db.models.signals import pre_save
from django.dispatch import receiver

from django_afip import models
Expand All @@ -21,7 +22,8 @@ def update_certificate_expiration(
instance.certificate_expiration = instance.get_certificate_expiration()


FILE_FIELDS = ['certificate', 'logo']
FILE_FIELDS = ["certificate", "logo"]


# Store old files before saving.
@receiver(pre_save, sender=models.TaxPayer)
Expand All @@ -34,10 +36,13 @@ def store_old_files(
try:
old_instance = sender.objects.get(pk=instance.pk)
# Save the reference of the old files in the model.
instance._old_files = {field: getattr(old_instance, field) for field in FILE_FIELDS}
instance._old_files = {
field: getattr(old_instance, field) for field in FILE_FIELDS
}
except sender.DoesNotExist:
instance._old_files = {}


# Delete old files after saving.
@receiver(post_save, sender=models.TaxPayer)
def delete_file_taxpayer(
Expand All @@ -46,14 +51,14 @@ def delete_file_taxpayer(
**kwargs,
) -> None:
if not instance.pk:
return # The instance is new, there are no old files to delete.
old_files = getattr(instance, '_old_files', {})
return # The instance is new, there are no old files to delete.

old_files = getattr(instance, "_old_files", {})

for field_name in FILE_FIELDS:
old_file = old_files.get(field_name)
new_file = getattr(instance, field_name)

if old_file and old_file != new_file:
# Delete the old file from storage.
old_file.delete(save=False)
old_file.delete(save=False)

0 comments on commit 7de6e29

Please sign in to comment.