From: Jan Szejko Date: Thu, 16 Nov 2017 13:33:20 +0000 (+0100) Subject: add option to disable contact form after given datetime X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/469775ecf6cef4426cd6a89a84889a9c007b9d01 add option to disable contact form after given datetime --- diff --git a/contact/views.py b/contact/views.py index 5a2f2ae..84578dc 100644 --- a/contact/views.py +++ b/contact/views.py @@ -1,28 +1,37 @@ # -*- coding: utf-8 -*- from urllib import unquote +from datetime import datetime from django.contrib.auth.decorators import permission_required from django.http import Http404 from django.shortcuts import get_object_or_404, redirect, render +from django.utils import timezone +from django.views.decorators.cache import never_cache from fnpdjango.utils.views import serve_file from honeypot.decorators import check_honeypot +from edumed.utils import localtime_to_utc from .forms import contact_forms from .models import Attachment @check_honeypot +@never_cache def form(request, form_tag, force_enabled=False): try: form_class = contact_forms[form_tag] except KeyError: raise Http404 - if (getattr(form_class, 'disabled', False) and - not (force_enabled and request.user.is_superuser)): - template = getattr(form_class, 'disabled_template', None) - if template: - return render(request, template, {'title': form_class.form_title}) - raise Http404 + if not (force_enabled and request.user.is_superuser): + disabled = getattr(form_class, 'disabled', False) + end_tuple = getattr(form_class, 'ends_on') + end_time = localtime_to_utc(datetime(*end_tuple)) if end_tuple else None + expired = end_time and end_time < timezone.now() + if disabled or expired: + template = getattr(form_class, 'disabled_template', None) + if template: + return render(request, template, {'title': form_class.form_title}) + raise Http404 if request.method == 'POST': form = form_class(request.POST, request.FILES) else: diff --git a/edumed/contact_forms.py b/edumed/contact_forms.py index 5ee5892..e5ceff2 100644 --- a/edumed/contact_forms.py +++ b/edumed/contact_forms.py @@ -51,7 +51,7 @@ class CommissionForm(forms.Form): class OlimpiadaForm(ContactForm): - disabled = False + ends_on = (2017, 11, 17, 0, 5) disabled_template = 'wtem/disabled_contact_form.html' form_tag = "olimpiada" form_title = u"Olimpiada Cyfrowa - Elektroniczny System Zgłoszeń" diff --git a/edumed/utils.py b/edumed/utils.py index 2dce875..0683476 100644 --- a/edumed/utils.py +++ b/edumed/utils.py @@ -3,6 +3,10 @@ import codecs import csv import cStringIO +import pytz +from django.conf import settings +from django.utils import timezone + from settings.apps import INSTALLED_APPS @@ -41,4 +45,11 @@ def process_app_deps(list_with_deps): return tuple( (x[0] if type(x) == tuple else x) for x in list_with_deps - if type(x) != tuple or x[1] in INSTALLED_APPS) \ No newline at end of file + if type(x) != tuple or x[1] in INSTALLED_APPS) + + +def localtime_to_utc(localtime): + tz = pytz.timezone(settings.TIME_ZONE) + return timezone.utc.normalize( + tz.localize(localtime) + )