# Path: common/mixins.py
# class SlugifyMixin:
#     """
#     Slugify specific field and pass as value to slug field in the model.
# 
#     This mixin helps in solving the problem of having case insensitive duplicates
#     by creating a slug and ensuring uniqueness.
#     Model field to be slugified should be passed as a string into a variable
#     called slugify_field.
#     Slug field in the model should be named slug.
#     """
# 
#     def raise_validation_error(self, value):
#         raise ValidationError(_("Entry with {0} - {1} already exists.".format(
#                                 self.slugify_field, value)))
# 
#     def clean(self):
#         if hasattr(self, 'slugify_field') and hasattr(self, 'slug'):
#             slugify_field_value = getattr(self, self.slugify_field)
#             self.slug = slugify(slugify_field_value)
# 
#             if self.__class__.objects.filter(slug=self.slug).exists():
#                 # If pk exists, object exists in db and is being edited by user.
#                 if self.pk:
#                     try:
#                         self.validate_unique()
#                     except ValidationError:
#                         self.raise_validation_error(slugify_field_value)
# 
#                 else:
#                     self.raise_validation_error(slugify_field_value)
# 
#     def save(self, *args, **kwargs):
#         self.clean()
#         return super().save(*args, **kwargs)
# 
# class TimestampMixin(models.Model):
#     """Mixin for date and timestamp. Inherits django's models.Model."""
# 
#     date_created = models.DateTimeField(auto_now_add=True)
#     date_modified = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         abstract = True
# 
# Path: common/utils.py
# def timestamp_seconds():
#     global LAST_TIMESTAMP
# 
#     timestamp = int(time())
#     LAST_TIMESTAMP = (LAST_TIMESTAMP + 1) if timestamp <= LAST_TIMESTAMP else timestamp
# 
#     return LAST_TIMESTAMP


from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.validators import (MinValueValidator,
                                    validate_comma_separated_integer_list,)
from django.db import models, transaction
from django.db.utils import IntegrityError
from django.utils.translation import ugettext_lazy as _
from hashid_field import HashidField
from common.mixins import SlugifyMixin, TimestampMixin
from common.utils import timestamp_seconds

    NO_MEAL = 'no-meal'

    ACTION_CHOICES = (
        (NO_MEAL, 'no meal will be served'),
    )

    name = models.CharField(max_length=150)
    timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE)
    action = models.CharField(max_length=255, choices=ACTION_CHOICES)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

    def clean(self):
        if self.start_date and self.end_date:
            if self.start_date >= self.end_date:
                raise ValidationError(_('Start date must be less than end date.'))
        super().clean()

    def save(self, *args, **kwargs):
        self.clean()
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.name

    class Meta:
        unique_together = ('name', 'start_date', 'end_date')



# Path: common/mixins.py
# class SlugifyMixin:
#     """
#     Slugify specific field and pass as value to slug field in the model.
# 
#     This mixin helps in solving the problem of having case insensitive duplicates
#     by creating a slug and ensuring uniqueness.
#     Model field to be slugified should be passed as a string into a variable
#     called slugify_field.
#     Slug field in the model should be named slug.
#     """
# 
#     def raise_validation_error(self, value):
#         raise ValidationError(_("Entry with {0} - {1} already exists.".format(
#                                 self.slugify_field, value)))
# 
#     def clean(self):
#         if hasattr(self, 'slugify_field') and hasattr(self, 'slug'):
#             slugify_field_value = getattr(self, self.slugify_field)
#             self.slug = slugify(slugify_field_value)
# 
#             if self.__class__.objects.filter(slug=self.slug).exists():
#                 # If pk exists, object exists in db and is being edited by user.
#                 if self.pk:
#                     try:
#                         self.validate_unique()
#                     except ValidationError:
#                         self.raise_validation_error(slugify_field_value)
# 
#                 else:
#                     self.raise_validation_error(slugify_field_value)
# 
#     def save(self, *args, **kwargs):
#         self.clean()
#         return super().save(*args, **kwargs)
# 
# class TimestampMixin(models.Model):
#     """Mixin for date and timestamp. Inherits django's models.Model."""
# 
#     date_created = models.DateTimeField(auto_now_add=True)
#     date_modified = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         abstract = True
# 
# Path: common/utils.py
# def timestamp_seconds():
#     global LAST_TIMESTAMP
# 
#     timestamp = int(time())
#     LAST_TIMESTAMP = (LAST_TIMESTAMP + 1) if timestamp <= LAST_TIMESTAMP else timestamp
# 
#     return LAST_TIMESTAMP


# Path: app/timetables/models.py
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.validators import (MinValueValidator,
                                    validate_comma_separated_integer_list,)
from django.db import models, transaction
from django.db.utils import IntegrityError
from django.utils.translation import ugettext_lazy as _
from hashid_field import HashidField
from common.mixins import SlugifyMixin, TimestampMixin
from common.utils import timestamp_seconds


    NO_MEAL = 'no-meal'

    ACTION_CHOICES = (
        (NO_MEAL, 'no meal will be served'),
    )

    name = models.CharField(max_length=150)
    timetable = models.ForeignKey(Timetable, on_delete=models.CASCADE)
    action = models.CharField(max_length=255, choices=ACTION_CHOICES)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

    def clean(self):
        if self.start_date and self.end_date:
            if self.start_date >= self.end_date:
                raise ValidationError(_('Start date must be less than end date.'))
        super().clean()

    def save(self, *args, **kwargs):
        self.clean()
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.name

    class Meta:
        unique_together = ('name', 'start_date', 'end_date')



class Serving(TimestampMixin):