2020-02-12 12:10:41 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.conf import settings
|
2017-02-19 20:20:19 +00:00
|
|
|
from django.shortcuts import redirect
|
2020-02-12 12:10:41 +00:00
|
|
|
from django.utils import timezone
|
2017-02-19 20:20:19 +00:00
|
|
|
from django.views import View
|
2020-02-12 12:10:41 +00:00
|
|
|
from django.views.generic import DetailView, ListView
|
|
|
|
|
|
|
|
from .models import Camp
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2017-03-23 17:32:13 +00:00
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
2017-02-19 20:20:19 +00:00
|
|
|
|
|
|
|
|
2020-02-22 13:50:09 +00:00
|
|
|
class CampRedirectView(View):
|
2017-02-19 20:20:19 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2017-08-22 12:35:49 +00:00
|
|
|
now = timezone.now()
|
|
|
|
|
|
|
|
try:
|
2019-06-16 12:32:24 +00:00
|
|
|
camp = Camp.objects.get(camp__contains=now)
|
|
|
|
logger.debug(
|
|
|
|
"Redirecting to camp '%s' for page '%s' because it is now!"
|
|
|
|
% (camp.slug, kwargs["page"])
|
2017-08-22 12:35:49 +00:00
|
|
|
)
|
2019-06-16 12:32:24 +00:00
|
|
|
return redirect(kwargs["page"], camp_slug=camp.slug)
|
2017-08-22 12:35:49 +00:00
|
|
|
except Camp.DoesNotExist:
|
2018-09-13 11:42:31 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
# no ongoing camp, find the closest camp in the past
|
|
|
|
try:
|
2019-06-16 12:32:24 +00:00
|
|
|
prevcamp = (
|
|
|
|
Camp.objects.filter(camp__endswith__lt=now).order_by("-camp").first()
|
|
|
|
)
|
2018-09-13 11:42:31 +00:00
|
|
|
except Camp.DoesNotExist:
|
|
|
|
prevcamp = None
|
2017-08-22 12:35:49 +00:00
|
|
|
|
2018-09-13 11:42:31 +00:00
|
|
|
# find the closest upcoming camp
|
|
|
|
try:
|
2019-06-16 12:32:24 +00:00
|
|
|
nextcamp = (
|
|
|
|
Camp.objects.filter(camp__startswith__gt=now).order_by("camp").first()
|
|
|
|
)
|
2018-09-13 11:42:31 +00:00
|
|
|
except Camp.DoesNotExist:
|
|
|
|
nextcamp = None
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2018-09-13 11:42:31 +00:00
|
|
|
percentpassed = False
|
|
|
|
if prevcamp and nextcamp:
|
2017-08-22 12:35:49 +00:00
|
|
|
# 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
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2017-08-22 12:35:49 +00:00
|
|
|
# find the percentage of time passed
|
|
|
|
percentpassed = (dayssinceprevcamp / daysbetween) * 100
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2018-09-13 11:42:31 +00:00
|
|
|
# figure out where to redirect
|
|
|
|
if percentpassed > settings.CAMP_REDIRECT_PERCENT or not prevcamp:
|
|
|
|
# either we have no previous camp, or we have both and more than settings.CAMP_REDIRECT_PERCENT has passed, so redirect to the next camp
|
|
|
|
camp = nextcamp
|
|
|
|
else:
|
|
|
|
# either we have no next camp, or we have both and less than settings.CAMP_REDIRECT_PERCENT has passed, so redirect to the previous camp
|
|
|
|
camp = prevcamp
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2018-09-13 11:42:31 +00:00
|
|
|
# do the redirect
|
2019-06-16 12:32:24 +00:00
|
|
|
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
|
2019-06-16 12:32:24 +00:00
|
|
|
slug_url_kwarg = "camp_slug"
|
2016-12-25 14:52:55 +00:00
|
|
|
|
2017-01-20 15:18:10 +00:00
|
|
|
def get_template_names(self):
|
2019-06-16 12:32:24 +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
|
2019-06-16 12:32:24 +00:00
|
|
|
template_name = "camp_list.html"
|
|
|
|
queryset = Camp.objects.all().order_by("camp")
|