Skip to content
models.py 2.27 KiB
Newer Older
Bengfort's avatar
Bengfort committed
# (c) 2020 MPIB <https://www.mpib-berlin.mpg.de/>,
#
# This file is part of castellum-scheduler.
#
# castellum-scheduler is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Castellum is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with Castellum. If not, see
# <http://www.gnu.org/licenses/>.

import datetime
Hayat's avatar
Hayat committed
import uuid
Bengfort's avatar
Bengfort committed

from django.db import models
from django.utils import formats
from django.utils.translation import gettext_lazy as _


class Schedule(models.Model):
Hayat's avatar
Hayat committed
    uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
Bengfort's avatar
Bengfort committed
    title = models.CharField(_('Title'), max_length=254)

    def __str__(self):
        return self.title


class Timeslot(models.Model):
    schedule = models.ForeignKey(Schedule, verbose_name=_('Schedule'), on_delete=models.CASCADE)
    # date and time fields are always naive -- which is what we want
    # https://docs.djangoproject.com/en/3.0/topics/i18n/timezones/#naive-and-aware-datetime-objects
    date = models.DateField(_('Date'))
    time = models.TimeField(_('Time'))

    class Meta:
        unique_together = ['schedule', 'date', 'time']
        ordering = ['date', 'time']

    def __str__(self):
        return '%s %s' % (formats.date_format(self.date), formats.time_format(self.time))

    @property
    def datetime(self):
        return datetime.datetime.combine(self.date, self.time)


class Invitation(models.Model):
    token = models.CharField(_('Token'), max_length=254)
    schedule = models.ForeignKey(Schedule, verbose_name=_('Schedule'), on_delete=models.CASCADE)
    timeslot = models.OneToOneField(
        Timeslot,
        verbose_name=_('Timeslot'),
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
    )

    class Meta:
        unique_together = ['schedule', 'token']

    def __str__(self):
        return '%s:%s' % (self.schedule, self.token)