feat: ruff'ed

This commit is contained in:
2025-05-02 08:38:57 +02:00
parent 32b36b81b3
commit 48b53d851a
62 changed files with 2019 additions and 3655 deletions
+33 -52
View File
@@ -12,17 +12,7 @@ from django.utils import timezone
from django.utils.text import gettext_lazy as _
from shop.exceptions import RedsysPaymentException, RedsysValidationException
from shop.models import (
Cart,
CartItem,
Order,
OrderLine,
Payment,
Product,
ProductBatch,
ProductPrice,
ShippingMethod,
)
from shop.models import Cart, CartItem, Order, OrderLine, Payment, Product, ProductBatch, ProductPrice, ShippingMethod
from shop.settings import ERROR_CODES
from shop.signals import clear_cart
@@ -32,7 +22,7 @@ User = get_user_model()
def create_order_line_for_product(product: Product, quantity: Decimal, order: Order):
price = product.prices.last()
base_total = round(price.price * quantity, 2)
tax_value = price.tax.value / Decimal("100")
tax_value = price.tax.value / Decimal('100')
taxes = round(base_total * tax_value, 2)
return OrderLine.objects.create(
@@ -54,11 +44,11 @@ def create_order(
billing_state: str,
billing_country: str,
billing_zip: str,
shipping_address: str = "",
shipping_city: str = "",
shipping_state: str = "",
shipping_country: str = "",
shipping_zip: str = "",
shipping_address: str = '',
shipping_city: str = '',
shipping_state: str = '',
shipping_country: str = '',
shipping_zip: str = '',
) -> Order:
order = Order.objects.create(
user=customer,
@@ -87,29 +77,29 @@ def delete_product_batch(batch: ProductBatch):
def create_order_from_cart(
cart: Cart,
shipping_method: ShippingMethod,
billing_address_full_name="",
billing_address_address="",
billing_address_town="",
billing_address_state="",
billing_address_country="",
billing_address_zip="",
shipping_address_full_name="",
shipping_address_address="",
shipping_address_town="",
shipping_address_state="",
shipping_address_country="",
shipping_address_zip="",
shipping_address_phone="",
email="",
billing_address_full_name='',
billing_address_address='',
billing_address_town='',
billing_address_state='',
billing_address_country='',
billing_address_zip='',
shipping_address_full_name='',
shipping_address_address='',
shipping_address_town='',
shipping_address_state='',
shipping_address_country='',
shipping_address_zip='',
shipping_address_phone='',
email='',
):
order = Order.objects.create(
billing_address=f"{billing_address_full_name} {billing_address_address}",
billing_address=f'{billing_address_full_name} {billing_address_address}',
billing_city=billing_address_town,
billing_state=billing_address_state,
billing_country=billing_address_country,
billing_zip=billing_address_zip,
contact_phone=shipping_address_phone,
shipping_address=f"{shipping_address_address} {shipping_address_full_name}",
shipping_address=f'{shipping_address_address} {shipping_address_full_name}',
shipping_city=shipping_address_town,
shipping_state=shipping_address_state,
shipping_country=shipping_address_country,
@@ -174,9 +164,7 @@ def compute_signature(salt, payload, key):
"""
b64_key = base64.b64decode(key)
des3 = pyDes.triple_des(
b64_key, mode=pyDes.CBC, IV="\0" * 8, pad="\0", padmode=pyDes.PAD_NORMAL
)
des3 = pyDes.triple_des(b64_key, mode=pyDes.CBC, IV='\0' * 8, pad='\0', padmode=pyDes.PAD_NORMAL)
pepper = des3.encrypt(str(salt))
payload_hash = hmac.new(pepper, payload.encode(), hashlib.sha256).digest()
@@ -211,27 +199,25 @@ def validate_payment_for_order(request, order: Order) -> Decimal:
"""
data = request.POST
merchant_parameters = data.get("Ds_MerchantParameters")
merchant_parameters = data.get('Ds_MerchantParameters')
if not merchant_parameters:
raise RedsysValidationException(
_("No se ha recibido ningún valor para Ds_MerchantParameters")
)
raise RedsysValidationException(_('No se ha recibido ningún valor para Ds_MerchantParameters'))
merchant_params = decode_b64_string(merchant_parameters)
result = json.loads(merchant_params)
order_code = result.get("Ds_Order")
order_code = result.get('Ds_Order')
assert order_code == order.code
status_code = result.get("Ds_Response")
status_code = result.get('Ds_Response')
if int(status_code) > 100:
reason = ERROR_CODES.get(status_code, _("Error no tipificado"))
reason = ERROR_CODES.get(status_code, _('Error no tipificado'))
raise RedsysPaymentException(_(f"No se ha realizado el pago. Motivo: {reason}"))
raise RedsysPaymentException(_(f'No se ha realizado el pago. Motivo: {reason}'))
amount = Decimal(result.get("Ds_Amount")) / 100
amount = Decimal(result.get('Ds_Amount')) / 100
return amount
@@ -268,9 +254,7 @@ def validate_expiry_date(expiry_date: str):
def update_order_payment_status(order: Order):
if order.amount_paid >= order.total:
order.status = Order.Statuses.STATUS_PAID
elif order.status == Order.Statuses.STATUS_PAID and order.amount_paid == Decimal(
"0.00"
):
elif order.status == Order.Statuses.STATUS_PAID and order.amount_paid == Decimal('0.00'):
order.status = Order.Statuses.STATUS_RETURNED
else:
order.status = Order.Statuses.STATUS_PENDING
@@ -286,10 +270,7 @@ def delete_cart_items_from_order(order):
def add_payment_to_order(order: Order, amount):
with transaction.atomic():
payment = Payment.objects.create(
amount=amount,
order=order,
user=order.user,
method=Payment.MethodChoices.REDSYS,
amount=amount, order=order, user=order.user, method=Payment.MethodChoices.REDSYS
)
order.amount_paid += payment.amount