Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
14 changes: 14 additions & 0 deletions padam_django/apps/BusShift/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.contrib import admin

from padam_django.apps.BusShift.models import BusShift


@admin.register(BusShift)
class BusShiftAdmin(admin.ModelAdmin):
list_display = ('bus', 'driver',
'shift_start', 'shift_end', 'duration')
filter_horizontal = ('places',)
search_fields = ('BusShiftname', 'bus__name', 'driver__name')

def duration(self, obj):
return obj.duration
5 changes: 5 additions & 0 deletions padam_django/apps/BusShift/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BusShiftConfid(AppConfig):
name = "padam_django.apps.BusShift"
30 changes: 30 additions & 0 deletions padam_django/apps/BusShift/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import factory
from faker import Faker

from . import models

from padam_django.apps.fleet.factories import BusFactory, DriverFactory

fake = Faker(['fr'])


class BusShiftFactory(factory.django.DjangoModelFactory):

BusShiftname = factory.LazyFunction(fake.name)
driver = factory.SubFactory(DriverFactory)
bus = factory.SubFactory(BusFactory)

shift_start = factory.LazyFunction(fake.date_time_this_year)
shift_end = factory.LazyAttribute(
lambda o: o.shift_start + fake.time_delta())

@factory.post_generation
def places(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for place in extracted:
self.places.add(place)

class Meta:
model = models.BusShift
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions padam_django/apps/BusShift/management/commands/create_busshift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from padam_django.apps.common.management.base import CreateDataBaseCommand

from padam_django.apps.BusShift.factories import BusShiftFactory


class Command(CreateDataBaseCommand):

help = 'Create few shift'

def handle(self, *args, **options):
super().handle(*args, **options)
self.stdout.write(f'Creating {self.number} shift ...')
BusShiftFactory.create_batch(size=self.number)
Empty file.
74 changes: 74 additions & 0 deletions padam_django/apps/BusShift/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.db import models
from django.core.exceptions import ValidationError

from padam_django.apps.fleet.models import Bus, Driver
from padam_django.apps.geography.models import Place

from datetime import datetime, timedelta
from django.utils import timezone


class BusShift(models.Model):
BusShiftname = models.CharField("Name of the Bus Shift",
max_length=100, default="Shift")

bus = models.ForeignKey(Bus, on_delete=models.CASCADE)
driver = models.ForeignKey(Driver, on_delete=models.CASCADE)

shift_start = models.DateTimeField(default=timezone.now())
shift_end = models.DateTimeField(
default=timezone.now() + timedelta(hours=8))

places = models.ManyToManyField(Place, related_name='bus_shifts')

@property
def duration(self):
return self.shift_end - self.shift_start

class Meta:
verbose_name_plural = "Bus Shifts"

def __str__(self):
return f"Bus Shift: {self.BusShiftname}"

def clean(self):
# Validate that the bus is not already assigned to another shift during the same period
overlapping_shifts = BusShift.objects.filter(
bus=self.bus,
shift_start__lt=self.shift_end,
shift_end__gt=self.shift_start
).exclude(id=self.id)
if overlapping_shifts.exists():
raise ValidationError(
'This bus is already assigned to another shift during the selected time period.')

# Validate that the driver is not already assigned to another shift during the same period
overlapping_shifts = BusShift.objects.filter(
driver=self.driver,
shift_start__lt=self.shift_end,
shift_end__gt=self.shift_start
).exclude(id=self.id)
if overlapping_shifts.exists():
raise ValidationError(
'This driver is already assigned to another shift during the selected time period.')

def save(self, *args, **kwargs):
self.clean() # Ensure clean is called to validate before saving
super().save(*args, **kwargs)


# class BusStops(models.Model):

# bus_line = models.CharField(max_length=100, default="line_test")

# place = models.ForeignKey(Place, on_delete=models.CASCADE)

# scheduled_time = models.DateTimeField(default=datetime.now())
# order = models.PositiveIntegerField(default=1)

# class Meta:
# ordering = ['order']
# unique_together = ('bus_line', 'order')

# def __str__(self):
# return f"{self.place} at {self.scheduled_time}"
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def handle(self, *args, **options):
management.call_command('create_drivers', number=5)
management.call_command('create_buses', number=10)
management.call_command('create_places', number=30)
management.call_command('create_busshift', number=2)
1 change: 1 addition & 0 deletions padam_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'padam_django.apps.fleet',
'padam_django.apps.geography',
'padam_django.apps.users',
'padam_django.apps.BusShift',
]

MIDDLEWARE = [
Expand Down