2015-10-03 01:07:05 +00:00
|
|
|
from django.db import models
|
2016-05-30 19:51:17 +00:00
|
|
|
from utils.models import UUIDModel, CreatedUpdatedModel
|
2017-02-08 22:34:24 +00:00
|
|
|
from program.models import EventType, EventLocation
|
2017-01-20 15:18:10 +00:00
|
|
|
from django.contrib.postgres.fields import DateTimeRangeField
|
2017-01-22 11:59:57 +00:00
|
|
|
from psycopg2.extras import DateTimeTZRange
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from datetime import timedelta
|
2017-01-29 09:51:31 +00:00
|
|
|
from django.utils import timezone
|
2017-02-19 20:20:19 +00:00
|
|
|
from django.urls import reverse
|
2017-03-23 17:32:13 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
|
|
|
|
2015-10-03 01:07:05 +00:00
|
|
|
|
|
|
|
class Camp(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
2016-12-25 14:52:55 +00:00
|
|
|
verbose_name = 'Camp'
|
|
|
|
verbose_name_plural = 'Camps'
|
2017-07-11 20:50:31 +00:00
|
|
|
ordering = ['-title']
|
2015-10-03 01:07:05 +00:00
|
|
|
|
2016-12-25 14:52:55 +00:00
|
|
|
title = models.CharField(
|
|
|
|
verbose_name='Title',
|
|
|
|
help_text='Title of the camp, ie. Bornhack 2016.',
|
2015-10-03 01:07:05 +00:00
|
|
|
max_length=255,
|
|
|
|
)
|
|
|
|
|
2016-12-25 14:52:55 +00:00
|
|
|
tagline = models.CharField(
|
|
|
|
verbose_name='Tagline',
|
|
|
|
help_text='Tagline of the camp, ie. "Initial Commit"',
|
|
|
|
max_length=255,
|
2015-10-03 01:07:05 +00:00
|
|
|
)
|
|
|
|
|
2016-12-25 14:52:55 +00:00
|
|
|
slug = models.SlugField(
|
|
|
|
verbose_name='Url Slug',
|
|
|
|
help_text='The url slug to use for this camp'
|
2015-10-03 01:07:05 +00:00
|
|
|
)
|
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
buildup = DateTimeRangeField(
|
|
|
|
verbose_name='Buildup Period',
|
|
|
|
help_text='The camp buildup period.',
|
2016-05-06 20:33:59 +00:00
|
|
|
)
|
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
camp = DateTimeRangeField(
|
|
|
|
verbose_name='Camp Period',
|
|
|
|
help_text='The camp period.',
|
2016-12-25 14:52:55 +00:00
|
|
|
)
|
2015-10-05 16:35:30 +00:00
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
teardown = DateTimeRangeField(
|
|
|
|
verbose_name='Teardown period',
|
|
|
|
help_text='The camp teardown period.',
|
2015-10-05 16:35:30 +00:00
|
|
|
)
|
|
|
|
|
2017-03-07 20:44:30 +00:00
|
|
|
read_only = models.BooleanField(
|
|
|
|
help_text='Whether the camp is read only (i.e. in the past)',
|
|
|
|
default=False
|
|
|
|
)
|
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('camp_detail', kwargs={'camp_slug': self.slug})
|
|
|
|
|
2017-01-22 11:59:57 +00:00
|
|
|
def clean(self):
|
|
|
|
''' Make sure the dates make sense - meaning no overlaps and buildup before camp before teardown '''
|
|
|
|
errors = []
|
|
|
|
# check for overlaps buildup vs. camp
|
|
|
|
if self.buildup.upper > self.camp.lower:
|
|
|
|
msg = "End of buildup must not be after camp start"
|
|
|
|
errors.append(ValidationError({'buildup', msg}))
|
|
|
|
errors.append(ValidationError({'camp', msg}))
|
|
|
|
|
|
|
|
# check for overlaps camp vs. teardown
|
|
|
|
if self.camp.upper > self.teardown.lower:
|
|
|
|
msg = "End of camp must not be after teardown start"
|
|
|
|
errors.append(ValidationError({'camp', msg}))
|
|
|
|
errors.append(ValidationError({'teardown', msg}))
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
2017-01-31 22:39:49 +00:00
|
|
|
def __str__(self):
|
2016-12-25 14:52:55 +00:00
|
|
|
return "%s - %s" % (self.title, self.tagline)
|
2016-07-13 19:44:09 +00:00
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
@property
|
|
|
|
def event_types(self):
|
|
|
|
# return all event types with at least one event in this camp
|
|
|
|
return EventType.objects.filter(event__instances__isnull=False, event__camp=self).distinct()
|
2015-10-03 01:07:05 +00:00
|
|
|
|
2017-02-08 22:34:24 +00:00
|
|
|
@property
|
|
|
|
def event_locations(self):
|
|
|
|
''' Return all event locations with at least one event in this camp'''
|
|
|
|
return EventLocation.objects.filter(eventinstances__isnull=False, camp=self).distinct()
|
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
@property
|
|
|
|
def logo_small(self):
|
2017-07-08 15:20:59 +00:00
|
|
|
return 'img/%(slug)s/logo/%(slug)s-logo-s.png' % {'slug': self.slug}
|
2015-10-03 01:07:05 +00:00
|
|
|
|
2017-04-15 13:47:54 +00:00
|
|
|
@property
|
|
|
|
def logo_small_svg(self):
|
|
|
|
return 'img/%(slug)s/logo/%(slug)s-logo-small.svg' % {'slug': self.slug}
|
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
@property
|
|
|
|
def logo_large(self):
|
2017-07-08 15:20:59 +00:00
|
|
|
return 'img/%(slug)s/logo/%(slug)s-logo-l.png' % {'slug': self.slug}
|
2016-12-25 14:52:55 +00:00
|
|
|
|
2017-06-04 14:08:41 +00:00
|
|
|
@property
|
|
|
|
def logo_large_svg(self):
|
|
|
|
return 'img/%(slug)s/logo/%(slug)s-logo-large.svg' % {'slug': self.slug}
|
|
|
|
|
2017-01-22 11:59:57 +00:00
|
|
|
def get_days(self, camppart):
|
|
|
|
'''
|
|
|
|
Returns a list of DateTimeTZRanges representing the days during the specified part of the camp.
|
|
|
|
'''
|
|
|
|
if not hasattr(self, camppart):
|
2017-03-23 17:32:13 +00:00
|
|
|
logger.error("nonexistant field/attribute")
|
2017-01-22 11:59:57 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
field = getattr(self, camppart)
|
|
|
|
|
|
|
|
if not hasattr(field, '__class__') or not hasattr(field.__class__, '__name__') or not field.__class__.__name__ == 'DateTimeTZRange':
|
2017-03-23 17:32:13 +00:00
|
|
|
logger.error("this attribute is not a datetimetzrange field: %s" % field)
|
2017-01-22 11:59:57 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
daycount = (field.upper - field.lower).days
|
|
|
|
days = []
|
|
|
|
for i in range(0, daycount):
|
|
|
|
if i == 0:
|
|
|
|
# on the first day use actual start time instead of midnight
|
|
|
|
days.append(
|
|
|
|
DateTimeTZRange(
|
2017-03-07 20:44:30 +00:00
|
|
|
field.lower,
|
2017-01-22 11:59:57 +00:00
|
|
|
(field.lower+timedelta(days=i+1)).replace(hour=0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif i == daycount-1:
|
|
|
|
# on the last day use actual end time instead of midnight
|
|
|
|
days.append(
|
|
|
|
DateTimeTZRange(
|
|
|
|
(field.lower+timedelta(days=i)).replace(hour=0),
|
|
|
|
field.lower+timedelta(days=i+1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# neither first nor last day, goes from midnight to midnight
|
|
|
|
days.append(
|
|
|
|
DateTimeTZRange(
|
|
|
|
(field.lower+timedelta(days=i)).replace(hour=0),
|
|
|
|
(field.lower+timedelta(days=i+1)).replace(hour=0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return days
|
|
|
|
|
|
|
|
@property
|
|
|
|
def buildup_days(self):
|
|
|
|
'''
|
|
|
|
Returns a list of DateTimeTZRanges representing the days during the buildup.
|
|
|
|
'''
|
|
|
|
return self.get_days('buildup')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def camp_days(self):
|
|
|
|
'''
|
|
|
|
Returns a list of DateTimeTZRanges representing the days during the camp.
|
|
|
|
'''
|
|
|
|
return self.get_days('camp')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def teardown_days(self):
|
|
|
|
'''
|
|
|
|
Returns a list of DateTimeTZRanges representing the days during the buildup.
|
|
|
|
'''
|
|
|
|
return self.get_days('teardown')
|
|
|
|
|
2017-01-29 09:52:20 +00:00
|
|
|
@property
|
|
|
|
def call_for_speakers_open(self):
|
|
|
|
if self.camp.upper < timezone.now():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2017-01-22 11:59:57 +00:00
|
|
|
|
2017-01-29 13:51:50 +00:00
|
|
|
@property
|
|
|
|
def call_for_sponsors_open(self):
|
|
|
|
""" Keep call for sponsors open 30 days after camp end """
|
|
|
|
if self.camp.upper + timedelta(days=30) < timezone.now():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|