From dd9ef2380a0ecd8ad7ff2272bee997c22de5b109 Mon Sep 17 00:00:00 2001 From: Vidir Valberg Gudmundsson Date: Tue, 22 Aug 2017 14:35:49 +0200 Subject: [PATCH] Camp redirect has to account for an ongoing camp. --- src/camps/views.py | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/camps/views.py b/src/camps/views.py index 3d9a9d70..1ba41d2a 100644 --- a/src/camps/views.py +++ b/src/camps/views.py @@ -12,32 +12,42 @@ logger = logging.getLogger("bornhack.%s" % __name__) class CampRedirectView(CampViewMixin, View): 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] + now = timezone.now() - # find the closest upcoming camp - nextcamp = Camp.objects.filter( - camp__startswith__gt=timezone.now() + try: + camp = Camp.objects.get( + camp__contains=now + ) + + logger.debug("Redirecting to camp '%s' for page '%s' because it is now!" % (camp.slug, kwargs['page'])) + except Camp.DoesNotExist: + # find the closest camp in the past + prevcamp = Camp.objects.filter( + camp__endswith__lt=now + ).order_by('-camp')[0] + + # find the closest upcoming camp + nextcamp = Camp.objects.filter( + camp__startswith__gt=now ).order_by('camp')[0] - # find the number of days between the two camps - daysbetween = (nextcamp.camp.lower - prevcamp.camp.upper).days + # find the number of days between the two camps + daysbetween = (nextcamp.camp.lower - prevcamp.camp.upper).days - # find the number of days since the last camp ended - dayssinceprevcamp = (timezone.now() - prevcamp.camp.lower).days + # find the number of days since the last camp ended + dayssinceprevcamp = (timezone.now() - prevcamp.camp.lower).days - # find the percentage of time passed - percentpassed = (dayssinceprevcamp / daysbetween) * 100 + # find the percentage of time passed + percentpassed = (dayssinceprevcamp / daysbetween) * 100 - # do the redirect - if percentpassed > settings.CAMP_REDIRECT_PERCENT: - camp = nextcamp - else: - camp = prevcamp + # do the redirect + if percentpassed > settings.CAMP_REDIRECT_PERCENT: + camp = nextcamp + else: + 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))) - 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)