44 lines
2.0 KiB
Python
44 lines
2.0 KiB
Python
# type: ignore
|
|
from django.db import models
|
|
from django_core.models.base import AbstractBaseModel
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.contrib.auth import get_user_model
|
|
from core.apps.api.choices.ad_type import AdType, AdCategoryType
|
|
|
|
|
|
class AdModel(AbstractBaseModel):
|
|
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, verbose_name=_("User"), related_name="ads")
|
|
name = models.CharField(_("Name"), max_length=255)
|
|
ad_type = models.CharField(_("Type"), max_length=255, choices=AdType)
|
|
category = models.ForeignKey("api.CategoryModel", on_delete=models.CASCADE, verbose_name=_("Category"))
|
|
ad_category_type = models.CharField(_("Category Type"), max_length=255, choices=AdCategoryType)
|
|
_price = models.DecimalField(_("Price"), max_digits=10, decimal_places=2, null=True, blank=True, db_column="price")
|
|
discount = models.DecimalField(_("Discount"), max_digits=10, decimal_places=2, default=-1)
|
|
is_available = models.BooleanField(_("Is available"), default=True)
|
|
physical_product = models.BooleanField(_("Physical product"), default=False)
|
|
plan = models.ForeignKey("api.AdTopPlanModel", on_delete=models.CASCADE, verbose_name=_("Plan"))
|
|
tags = models.ManyToManyField("api.TagsModel", verbose_name=_("Tags"), blank=True)
|
|
image = models.ImageField(_("Image"), upload_to="ads/")
|
|
description = models.TextField(_("Description"))
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
@property
|
|
def price(self):
|
|
"""Get actual price - either from variant or direct price"""
|
|
if self.ad_category_type == AdCategoryType.PRODUCT.value:
|
|
variant = self.variants.order_by("price").first()
|
|
return variant.price if variant else 0
|
|
return self._price
|
|
|
|
class Meta:
|
|
db_table = "ad"
|
|
verbose_name = _("Ad")
|
|
verbose_name_plural = _("Ads")
|
|
ordering = ["-created_at"]
|
|
indexes = [
|
|
models.Index(fields=["-created_at"]),
|
|
models.Index(fields=["category", "is_available"]),
|
|
]
|