2016-12-25 14:52:55 +00:00
|
|
|
from django.views.generic import ListView, DetailView
|
|
|
|
from django.utils import timezone
|
2017-06-04 11:08:59 +00:00
|
|
|
from .models import Camp
|
2017-02-19 20:20:19 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
from .mixins import CampViewMixin
|
|
|
|
from django.views import View
|
|
|
|
from django.conf import settings
|
2017-03-23 17:32:13 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
2017-02-19 20:20:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CampRedirectView(CampViewMixin, View):
|
2017-06-04 11:08:59 +00:00
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
# find the closest camp in the past
|
|
|
|
prevcamp = Camp.objects.filter(camp__endswith__lt=timezone.now()).order_by('-camp')[0]
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
# find the closest upcoming camp
|
|
|
|
nextcamp = Camp.objects.filter(camp__startswith__gt=timezone.now()).order_by('camp')[0]
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
# find the number of days between the two camps
|
|
|
|
daysbetween = (nextcamp.camp.lower - prevcamp.camp.upper).days
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
# find the number of days since the last camp ended
|
|
|
|
dayssinceprevcamp = (timezone.now() - prevcamp.camp.lower).days
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
# find the percentage of time passed
|
|
|
|
percentpassed = (dayssinceprevcamp / daysbetween) * 100
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2017-02-19 20:20:19 +00:00
|
|
|
# do the redirect
|
|
|
|
if percentpassed > settings.CAMP_REDIRECT_PERCENT:
|
2017-03-23 17:32:13 +00:00
|
|
|
camp = nextcamp
|
2017-02-19 20:20:19 +00:00
|
|
|
else:
|
2017-03-23 17:32:13 +00:00
|
|
|
camp = prevcamp
|
|
|
|
|
|
|
|
logger.debug("Redirecting to camp '%s' for page '%s' because %s%% of the time between the camps passed" % (camp.slug, kwargs['page'], int(percentpassed)))
|
|
|
|
return redirect(kwargs['page'], camp_slug=camp.slug)
|
2015-10-05 16:35:30 +00:00
|
|
|
|
2015-10-03 01:07:05 +00:00
|
|
|
|
2016-12-25 14:52:55 +00:00
|
|
|
class CampDetailView(DetailView):
|
|
|
|
model = Camp
|
|
|
|
slug_url_kwarg = 'camp_slug'
|
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
def get_template_names(self):
|
2017-01-28 18:30:52 +00:00
|
|
|
return '%s_camp_detail.html' % self.get_object().slug
|
2017-01-20 15:18:10 +00:00
|
|
|
|
2017-01-25 00:49:13 +00:00
|
|
|
|
|
|
|
class CampListView(ListView):
|
|
|
|
model = Camp
|
|
|
|
template_name = 'camp_list.html'
|
|
|
|
|