some fields validation and model cleaning
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+16
-8
@@ -1,4 +1,8 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
from accounts.models import DepotUser
|
||||
from common.fields import ContainerNumberField, UpperCaseCharField
|
||||
from common.models import ContainerTypeModel, LinesModel, OperationModel
|
||||
|
||||
# Create your models here.
|
||||
@@ -10,15 +14,15 @@ class Booking(models.Model):
|
||||
('canceled', 'Canceled'),
|
||||
]
|
||||
|
||||
number = models.CharField(max_length=50, unique=True)
|
||||
vehicles = models.CharField(blank=True, null=True)
|
||||
number = UpperCaseCharField(max_length=50)
|
||||
vehicles = UpperCaseCharField(blank=True, null=True)
|
||||
container_type = models.ForeignKey(
|
||||
ContainerTypeModel,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='booking_container_types',
|
||||
)
|
||||
container_count = models.IntegerField()
|
||||
container_expedited_count = models.IntegerField(default=0)
|
||||
container_count = models.PositiveIntegerField()
|
||||
container_expedited_count = models.PositiveIntegerField(default=0)
|
||||
carrier = models.CharField(max_length=100, blank=True, null=True)
|
||||
line = models.ForeignKey(
|
||||
LinesModel,
|
||||
@@ -27,12 +31,12 @@ class Booking(models.Model):
|
||||
)
|
||||
visible = models.BooleanField(default=True)
|
||||
is_new = models.BooleanField(default=True)
|
||||
container_number = models.CharField(max_length=11, blank=True, null=True)
|
||||
container_number = ContainerNumberField(max_length=11, blank=True, null=True)
|
||||
vehicles_left = models.CharField(blank=True, null=True)
|
||||
created_on = models.DateTimeField(auto_now_add=True)
|
||||
created_by = models.IntegerField()
|
||||
created_by = models.ForeignKey(DepotUser, related_name='booking_user', on_delete=models.CASCADE)
|
||||
updated_on = models.DateTimeField(auto_now=True)
|
||||
updated_by = models.IntegerField()
|
||||
updated_by = models.ForeignKey(DepotUser, related_name='booking_user', on_delete=models.CASCADE)
|
||||
status = models.CharField(
|
||||
max_length=10,
|
||||
choices=STATUS_CHOICES,
|
||||
@@ -44,4 +48,8 @@ class Booking(models.Model):
|
||||
return self.container_count - (self.container_expedited_count or 0)
|
||||
|
||||
class Meta:
|
||||
app_label = 'booking'
|
||||
app_label = 'booking'
|
||||
|
||||
def clean(self):
|
||||
if Booking.objects.filter(number=self.number, status='active').exclude(id=self.id).exists():
|
||||
raise ValidationError(f'Booking with number {self.number} already exists and is active.')
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+16
-15
@@ -45,8 +45,8 @@ class BookingViewsTestCase(TestCase):
|
||||
line=self.line,
|
||||
created_by=self.user_employee_all_rights.id,
|
||||
updated_by=self.user_employee_all_rights.id,
|
||||
vehicles='',
|
||||
vehicles_left='',
|
||||
vehicles='K1,J2,K3',
|
||||
vehicles_left='K1,J2,K3',
|
||||
|
||||
)
|
||||
|
||||
@@ -72,8 +72,8 @@ class BookingViewsTestCase(TestCase):
|
||||
self.assertTemplateUsed(response, 'employee/booking-list.html')
|
||||
self.assertContains(response, self.booking.number)
|
||||
|
||||
def test_booking_create_view(self):
|
||||
self.assertTrue( self.user_client_all_rights.has_company_perm('can_view_booking'))
|
||||
def test_client_create_booking__expect_redirect_to_booking_list_and_booking_in_db(self):
|
||||
self.assertTrue( self.user_client_all_rights.has_company_perm('can_manage_booking'))
|
||||
self.client.login(username=self.user_client_all_rights.username, password=self.user_password)
|
||||
|
||||
response = self.client.post(reverse('client_booking_create'), {
|
||||
@@ -84,16 +84,17 @@ class BookingViewsTestCase(TestCase):
|
||||
'vehicles': 'Truck1,Truck2',
|
||||
'vehicles_left': 'Truck1,Truck2',
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertTrue(Booking.objects.filter(number='BOOK456').exists())
|
||||
|
||||
# def test_booking_update_view(self):
|
||||
# response = self.client.post(reverse('booking-update', args=[self.booking.id]), {
|
||||
# 'number': 'BOOK123-updated',
|
||||
# 'container_type': self.container_type.id,
|
||||
# 'container_count': 15,
|
||||
# 'line': self.line.id,
|
||||
# })
|
||||
# self.assertEqual(response.status_code, 302)
|
||||
# self.booking.refresh_from_db()
|
||||
# self.assertEqual(self.booking.number, 'BOOK123-updated')
|
||||
def test_booking_update_view(self):
|
||||
self.client.login(username=self.user_client_all_rights.username, password=self.user_password)
|
||||
response = self.client.post(reverse('client_booking_update', args=[self.booking.id]), {
|
||||
'number': 'BOOK123-updated',
|
||||
'container_type': self.container_type.id,
|
||||
'container_count': 15,
|
||||
'line': self.line.id,
|
||||
})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.booking.refresh_from_db()
|
||||
self.assertEqual(self.booking.number, 'BOOK123-updated')
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -59,4 +59,4 @@ class ClientBookingUpdateView(LoginRequiredMixin, UserPassesTestMixin, LineFilte
|
||||
return super().form_valid(form)
|
||||
|
||||
def test_func(self):
|
||||
return self.request.user.has_company_perm('can_edit_preinfo') or self.request.user.user_type == 'CA'
|
||||
return self.request.user.has_company_perm('can_manage_booking') or self.request.user.user_type == 'CA'
|
||||
|
||||
Reference in New Issue
Block a user