Skip to content

Commit

Permalink
chore: simplify enum naming
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Oct 23, 2024
1 parent 58e5aee commit c353506
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
24 changes: 12 additions & 12 deletions weblate_web/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ def url_fetcher(url: str) -> dict[str, str | bytes]:
return result


class QuantityUnitChoices(models.IntegerChoices):
class QuantityUnit(models.IntegerChoices):
BLANK = 0, ""
HOURS = 1, "hours"


class CurrencyChoices(models.IntegerChoices):
class Currency(models.IntegerChoices):
EUR = 0, "EUR"
CZK = 1, "CZK"


class InvoiceKindChoices(models.IntegerChoices):
class InvoiceKind(models.IntegerChoices):
DRAFT = 0, "Draft"
INVOICE = 10, "Invoice"
PROFORMA = 50, "Proforma"
Expand Down Expand Up @@ -108,14 +108,14 @@ class Invoice(models.Model):
)
issue_date = models.DateField(default=datetime.date.today)
due_date = models.DateField(blank=True)
kind = models.IntegerField(choices=InvoiceKindChoices)
kind = models.IntegerField(choices=InvoiceKind)
customer = models.ForeignKey("payments.Customer", on_delete=models.deletion.PROTECT)
customer_reference = models.CharField(max_length=100, blank=True)
discount = models.ForeignKey(
Discount, on_delete=models.deletion.PROTECT, blank=True, null=True
)
vat_rate = models.IntegerField(default=0)
currency = models.IntegerField(choices=CurrencyChoices, default=CurrencyChoices.EUR)
currency = models.IntegerField(choices=Currency, default=Currency.EUR)

# Invoice chaining Proforma -> Invoice, or Draft -> Invoice
parent = models.ForeignKey(
Expand Down Expand Up @@ -172,7 +172,7 @@ def save( # type: ignore[override]
)

def render_amount(self, amount: int | Decimal) -> str:
if self.currency == CurrencyChoices.EUR:
if self.currency == Currency.EUR:
return f"€{amount}"
return f"{amount} {self.get_currency_display()}"

Expand Down Expand Up @@ -331,7 +331,7 @@ def add_amounts(root, in_czk: bool = False):
add_element(output, "Proplatit", self.total_amount_czk)
add_element(output, "Vyuctovano", "0")
add_amounts(output, in_czk=True)
if self.currency != CurrencyChoices.CZK:
if self.currency != Currency.CZK:
valuty = add_element(output, "Valuty")
mena = add_element(valuty, "Mena")
add_element(mena, "Kod", self.get_currency_display())
Expand All @@ -340,7 +340,7 @@ def add_amounts(root, in_czk: bool = False):
add_amounts(valuty)

add_element(output, "PriUhrZbyv", "0")
if self.currency != CurrencyChoices.CZK:
if self.currency != Currency.CZK:
add_element(output, "ValutyProp", self.total_amount)
add_element(output, "SumZaloha", "0")
add_element(output, "SumZalohaC", "0")
Expand Down Expand Up @@ -371,7 +371,7 @@ def add_amounts(root, in_czk: bool = False):
polozka = add_element(seznam, "Polozka")
add_element(polozka, "Popis", item.description)
add_element(polozka, "PocetMJ", item.quantity)
if self.currency == CurrencyChoices.CZK:
if self.currency == Currency.CZK:
add_element(polozka, "Cena", item.total_price)
else:
add_element(polozka, "Valuty", item.total_price)
Expand Down Expand Up @@ -423,7 +423,7 @@ def generate_pdf(self) -> None:
def finalize(
self,
*,
kind: InvoiceKindChoices = InvoiceKindChoices.INVOICE,
kind: InvoiceKind = InvoiceKind.INVOICE,
prepaid: bool = True,
) -> Invoice:
"""Create a final invoice from draft/proforma upon payment."""
Expand Down Expand Up @@ -454,7 +454,7 @@ class InvoiceItem(models.Model):
default=1, validators=[MinValueValidator(1), MaxValueValidator(50)]
)
quantity_unit = models.IntegerField(
choices=QuantityUnitChoices, default=QuantityUnitChoices.BLANK
choices=QuantityUnit, default=QuantityUnit.BLANK
)
unit_price = models.DecimalField(decimal_places=2, max_digits=7)

Expand All @@ -475,7 +475,7 @@ def display_total_price(self) -> str:

def get_quantity_unit_display(self) -> str: # type: ignore[no-redef]
# Correcly handle singulars
if self.quantity_unit == QuantityUnitChoices.HOURS and self.quantity == 1:
if self.quantity_unit == QuantityUnit.HOURS and self.quantity == 1:
return "hour"
# This is what original get_quantity_unit_display() would have done
return self._get_FIELD_display( # type: ignore[attr-defined]
Expand Down
8 changes: 4 additions & 4 deletions weblate_web/invoices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from weblate_web.payments.models import Customer

from .models import Discount, Invoice, InvoiceKindChoices, QuantityUnitChoices
from .models import Discount, Invoice, InvoiceKind, QuantityUnit

S3_SCHEMA_PATH = (
Path(__file__).parent.parent.parent / "schemas" / "money-s3" / "_Document.xsd"
Expand Down Expand Up @@ -40,7 +40,7 @@ def create_invoice(
customer=self.create_customer(vat=vat),
discount=discount,
vat_rate=vat_rate,
kind=InvoiceKindChoices.INVOICE,
kind=InvoiceKind.INVOICE,
customer_reference=customer_reference,
)
invoice.invoiceitem_set.create(
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_total_items_hours(self):
description="Other item",
unit_price=1000,
quantity=4,
quantity_unit=QuantityUnitChoices.HOURS,
quantity_unit=QuantityUnit.HOURS,
)
self.assertEqual(invoice.total_amount, 4100)
self.validate_invoice(invoice)
Expand All @@ -96,7 +96,7 @@ def test_total_items_hour(self):
description="Other item",
unit_price=1000,
quantity=1,
quantity_unit=QuantityUnitChoices.HOURS,
quantity_unit=QuantityUnit.HOURS,
)
self.assertEqual(invoice.total_amount, 1100)
self.validate_invoice(invoice)
Expand Down
10 changes: 4 additions & 6 deletions weblate_web/payments/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,14 @@ def complete(self, request: HttpRequest | None) -> bool:
@transaction.atomic
def generate_invoice(self, *, proforma: bool = False) -> None:
from weblate_web.invoices.models import ( # noqa: PLC0415
CurrencyChoices,
Currency,
Invoice,
InvoiceKindChoices,
InvoiceKind,
)

if self.payment.paid_invoice:
raise ValueError("Invoice already exists!")
invoice_kind = (
InvoiceKindChoices.PROFORMA if proforma else InvoiceKindChoices.INVOICE
)
invoice_kind = InvoiceKind.PROFORMA if proforma else InvoiceKind.INVOICE
if self.payment.draft_invoice:
# Is there already draft proforma?
if proforma and self.payment.draft_invoice.kind == invoice_kind:
Expand All @@ -189,7 +187,7 @@ def generate_invoice(self, *, proforma: bool = False) -> None:
kind=invoice_kind,
customer=self.payment.customer,
vat_rate=self.payment.customer.vat_rate,
currency=CurrencyChoices.EUR,
currency=Currency.EUR,
prepaid=not proforma,
)
invoice.invoiceitem_set.create(
Expand Down

0 comments on commit c353506

Please sign in to comment.