Ticket #34589: method.py

File method.py, 2.4 KB (added by ftamy9, 18 months ago)
Line 
1from common.db.basic import Logged, Ordered
2from django.db import models
3from django.utils.translation import gettext_lazy as _
4
5from tehran_plus_common.models.base import StoreTypeChoices
6
7
8class ShippingChoice(models.TextChoices):
9 MIARE = 'Miare', _('Miare')
10 ALOPEYK = 'Alopeyk', _('Alopeyk')
11 ORGANIZATION = 'Organization', _('Organization') # like motorbikes for grocery or MRF vehicles
12
13
14class ShippingMethodQuerySet(models.QuerySet):
15 def actives(self, *args, **kwargs):
16 return super(ShippingMethodQuerySet, self).filter(*args, **kwargs).filter(is_active=True)
17
18
19class ShippingMethodManager(models.Manager):
20 def get_queryset(self):
21 return ShippingMethodQuerySet(self.model, using=self._db)
22
23 def actives(self, *args, **kwargs):
24 return self.get_queryset().actives(*args, **kwargs)
25
26
27class ShippingMethod(Logged, Ordered):
28 title = models.CharField(verbose_name=_("title"), max_length=200, choices=ShippingChoice.choices, unique=True)
29 description = models.TextField(verbose_name=_("description"), blank=True, null=True)
30 is_active = models.BooleanField(verbose_name=_('is active'), default=True)
31 max_course_in_trip = models.PositiveIntegerField(verbose_name=_('max number of courses in one trip'))
32
33 class Meta:
34 verbose_name = _('Shipping Method')
35 verbose_name_plural = _("Shipping Methods")
36
37 def __str__(self):
38 return self.title
39
40 @classmethod
41 def is_method(cls, title):
42 return cls.objects.filter(title=title).exists()
43
44 @property
45 def handler(self):
46 from ..miare_controller import MiareShipmentHandler
47 from ..organization_controller import OrganizationShipmentHandler
48
49 if self.title == ShippingChoice.MIARE.value:
50 return MiareShipmentHandler()
51 elif self.title == ShippingChoice.ORGANIZATION.value:
52 return OrganizationShipmentHandler()
53 return None # should never be returned!
54
55 objects = ShippingMethodManager()
56
57
58class ShippingMethodStoreType(models.Model):
59 store_type = models.PositiveSmallIntegerField(verbose_name=_("store type"), choices=StoreTypeChoices.choices)
60 shipping_method = models.ForeignKey(ShippingMethod, on_delete=models.CASCADE, verbose_name=_('shipping method'))
61 is_available = models.BooleanField(default=True, verbose_name=_('is_available'))
62
63 class Meta:
64 verbose_name = _('Shipping Method Store Type')
65 verbose_name_plural = _("Shipping Methods Stores Types")
Back to Top