1 | from common.db.basic import Logged
|
---|
2 | from django.core.exceptions import ValidationError
|
---|
3 | from django.core.validators import MaxValueValidator
|
---|
4 | from django.db import models
|
---|
5 | from django.utils.translation import gettext_lazy as _
|
---|
6 |
|
---|
7 | from shipment.models.method import ShippingMethod, ShippingChoice
|
---|
8 |
|
---|
9 |
|
---|
10 | class Trip(Logged):
|
---|
11 | trip_id = models.CharField(verbose_name=_('trip id'), max_length=400, null=True, blank=True)
|
---|
12 | method = models.ForeignKey(ShippingMethod, on_delete=models.PROTECT, verbose_name=_('method'))
|
---|
13 | source = models.JSONField(verbose_name=_('source address'))
|
---|
14 | trip_cost = models.PositiveIntegerField(verbose_name=_('trip cost'), null=True, blank=True, default=0)
|
---|
15 | driver_info = models.JSONField(verbose_name=_('driver info'), null=True, blank=True, )
|
---|
16 | driver = models.PositiveBigIntegerField(
|
---|
17 | verbose_name=_("driver national code"), validators=[MaxValueValidator(9999999999)], null=True, blank=True
|
---|
18 | )
|
---|
19 |
|
---|
20 | class Meta:
|
---|
21 | verbose_name = _('Trip')
|
---|
22 | verbose_name_plural = _("Trips")
|
---|
23 | unique_together = ('trip_id', 'method')
|
---|
24 | index_together = ('trip_id', 'method')
|
---|
25 |
|
---|
26 | @property
|
---|
27 | def handler(self):
|
---|
28 | return self.method.handler
|
---|
29 |
|
---|
30 | def __str__(self):
|
---|
31 | return str(self.trip_id)
|
---|
32 |
|
---|
33 | def cancel(self):
|
---|
34 | for shipment in self.shipments.all():
|
---|
35 | shipment.set_as_cancelled()
|
---|
36 |
|
---|
37 | def clean(self):
|
---|
38 | if self.method.title == ShippingChoice.ORGANIZATION:
|
---|
39 | if self.driver is None:
|
---|
40 | raise ValidationError({'driver': _('driver is empty')})
|
---|
41 | else:
|
---|
42 | if self.trip_id is None:
|
---|
43 | raise ValidationError({'trip_id': _('trip_id is empty')})
|
---|
44 | super(Trip, self).clean()
|
---|