add camp redirect views

This commit is contained in:
Thomas Steen Rasmussen 2017-02-19 21:20:19 +01:00
parent 0acf3e0fee
commit 98bb51941e
4 changed files with 66 additions and 5 deletions

View file

@ -35,6 +35,10 @@ TIME_ZONE='{{ django_timezone }}'
MEDIA_ROOT='{{ django_media_root }}'
PDF_ARCHIVE_PATH='{{ pdf_archive_path }}'
# start redirecting to the next camp instead of the previous camp after
# this much of the time between the camps has passed
CAMP_REDIRECT_PERCENT=40
# PSP settings
EPAY_MERCHANT_NUMBER='{{ epay_merchant_number }}'
EPAY_MD5_SECRET='{{ epay_md5_secret }}'

View file

@ -29,11 +29,6 @@ urlpatterns = [
r'^news/',
include('news.urls', namespace='news')
),
url(
r'^$',
TemplateView.as_view(template_name='frontpage.html'),
name='frontpage'
),
url(
r'^contact/',
TemplateView.as_view(template_name='contact.html'),
@ -73,6 +68,40 @@ urlpatterns = [
name='camp_list'
),
# camp redirect views here
url(
r'^$',
CampRedirectView.as_view(),
kwargs={'page': 'camp_detail'}
),
url(
r'^program/$',
CampRedirectView.as_view(),
kwargs={'page': 'schedule_index'}
),
url(
r'^info/$',
CampRedirectView.as_view(),
kwargs={'page': 'info'}
),
url(
r'^sponsors/$',
CampRedirectView.as_view(),
kwargs={'page': 'sponsors'}
),
url(
r'^villages/$',
CampRedirectView.as_view(),
kwargs={'page': 'village_list'}
),
# camp specific urls below here
url(

View file

@ -7,6 +7,7 @@ from psycopg2.extras import DateTimeTZRange
from django.core.exceptions import ValidationError
from datetime import timedelta
from django.utils import timezone
from django.urls import reverse
class Camp(CreatedUpdatedModel, UUIDModel):
@ -46,6 +47,9 @@ class Camp(CreatedUpdatedModel, UUIDModel):
help_text='The camp teardown period.',
)
def get_absolute_url(self):
return reverse('camp_detail', kwargs={'camp_slug': self.slug})
def clean(self):
''' Make sure the dates make sense - meaning no overlaps and buildup before camp before teardown '''
errors = []

View file

@ -1,6 +1,30 @@
from django.views.generic import ListView, DetailView
from django.utils import timezone
from .models import *
from django.shortcuts import redirect
from .mixins import CampViewMixin
from django.views import View
from django.conf import settings
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]
# find the closest upcoming camp
nextcamp = Camp.objects.filter(camp__startswith__gt=timezone.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 since the last camp ended
dayssinceprevcamp = (timezone.now() - prevcamp.camp.lower).days
# find the percentage of time passed
percentpassed = (dayssinceprevcamp / daysbetween) * 100
print(daysbetween, dayssinceprevcamp, percentpassed)
# do the redirect
if percentpassed > settings.CAMP_REDIRECT_PERCENT:
return redirect(kwargs['page'], camp_slug=nextcamp.slug)
else:
return redirect(kwargs['page'], camp_slug=prevcamp.slug)
class CampDetailView(DetailView):