combo/combo/apps/calendar/models.py

68 lines
2.7 KiB
Python

# combo - content management system
# Copyright (C) 2017 Entr'ouvert
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from django.db import models
from django.utils.translation import ugettext_lazy as _
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from .utils import (is_chrono_enabled, is_wcs_enabled,
get_chrono_events, get_calendar_context_vars)
@register_cell_class
class BookingCalendar(CellBase):
title = models.CharField(_('Title'), max_length=128, blank=True, null=True)
agenda_reference = models.CharField(_('Agenda'), max_length=128)
formdef_reference = models.CharField(_('Form'), max_length=128)
slot_duration = models.DurationField(
_('Slot duration'), default=datetime.timedelta(minutes=30),
help_text=_('Format is hours:minutes:seconds'))
minimal_booking_duration = models.DurationField(
_('Minimal booking duration'), default=datetime.timedelta(hours=1),
help_text=_('Format is hours:minutes:seconds'))
days_displayed = models.PositiveSmallIntegerField(_('Number of days to display'), default=7)
template_name = 'calendar/booking_calendar_cell.html'
class Meta:
verbose_name = _('Booking Calendar')
def get_default_form_class(self):
from .forms import BookingCalendarForm
return BookingCalendarForm
@classmethod
def is_enabled(cls):
return is_chrono_enabled() and is_wcs_enabled()
def is_visible(self, user=None):
return self.agenda_reference and self.formdef_reference \
and super(BookingCalendar, self).is_visible(user=user)
def get_cell_extra_context(self, context):
if context.get('placeholder_search_mode'):
return {}
extra_context = super(BookingCalendar, self).get_cell_extra_context(context)
events_data = get_chrono_events(self.agenda_reference, not(context.get('synchronous')))
extra_context.update(get_calendar_context_vars(
context['request'], extra_context['cell'], events_data))
return extra_context