From: Radek Czajka Date: Fri, 26 Sep 2014 13:41:43 +0000 (+0200) Subject: Get current quiz by domain from request. X-Git-Url: https://git.mdrn.pl/koed-quiz.git/commitdiff_plain/d302791037e175b1fdb79ed1aa5de2984d117485?hp=4c14e51225e284c5b81d53e7f6a5cd0f46ec4d05 Get current quiz by domain from request. --- diff --git a/apps/quiz/middleware.py b/apps/quiz/middleware.py new file mode 100644 index 0000000..3cde07d --- /dev/null +++ b/apps/quiz/middleware.py @@ -0,0 +1,5 @@ +from .models import Quiz + +class CurrentQuizMiddleware(object): + def process_request(self, request): + request.current_quiz = Quiz.current(request) diff --git a/apps/quiz/models.py b/apps/quiz/models.py index 74142da..8916ce9 100644 --- a/apps/quiz/models.py +++ b/apps/quiz/models.py @@ -19,8 +19,8 @@ class Quiz(Site): verbose_name_plural = _('quizzes') @classmethod - def current(cls): - return cls.objects.get(id=settings.SITE_ID) + def current(cls, request): + return cls.objects.get(domain=request.get_host()) def start(self): return self.question_set.all()[0] diff --git a/apps/quiz/templates/quiz/home.html b/apps/quiz/templates/quiz/home.html index 34efdd7..ca67f39 100644 --- a/apps/quiz/templates/quiz/home.html +++ b/apps/quiz/templates/quiz/home.html @@ -2,16 +2,16 @@ {% load i18n %} -{% block "title" %}{{ quiz.name }}{% endblock %} -{% block "header" %}{{ quiz.name }}{% endblock %} +{% block "title" %}{{ request.current_quiz.name }}{% endblock %} +{% block "header" %}{{ request.current_quiz.name }}{% endblock %} {% block "body" %}
-{{ quiz.description|safe }} +{{ request.current_quiz.description|safe }}
-{% trans "Start the test!" %} +{% trans "Start the test!" %} {% endblock %} diff --git a/apps/quiz/templatetags/quiz_tags.py b/apps/quiz/templatetags/quiz_tags.py index 2790ae2..6f7af26 100755 --- a/apps/quiz/templatetags/quiz_tags.py +++ b/apps/quiz/templatetags/quiz_tags.py @@ -9,6 +9,6 @@ from quiz.models import Quiz register = template.Library() -@register.simple_tag -def quiz_footer(): - return mark_safe(Quiz.current().footer) +@register.simple_tag(takes_context=True) +def quiz_footer(context): + return mark_safe(context.get('request').current_quiz.footer) diff --git a/apps/quiz/views.py b/apps/quiz/views.py index 3bd3a84..75b918d 100644 --- a/apps/quiz/views.py +++ b/apps/quiz/views.py @@ -10,10 +10,10 @@ from quiz.models import Quiz def question(request, slug=None): if slug is None: - question = Quiz.current().start() + question = request.current_quiz.start() request.session['ticket'] = [request.path] else: - question = get_object_or_404(Quiz.current().question_set, slug=slug) + question = get_object_or_404(request.current_quiz.question_set, slug=slug) ticket = request.session.get('ticket', []) valid = request.path in ticket @@ -58,6 +58,6 @@ def result(request, slug=None): elif ticket: valid_url = ticket[-1] - result = get_object_or_404(Quiz.current().result_set, slug=slug) + result = get_object_or_404(request.current_quiz.result_set, slug=slug) return render(request, "quiz/result_detail.html", locals()) diff --git a/koedquiz/settings.py b/koedquiz/settings.py index 13ce2ef..6b17288 100644 --- a/koedquiz/settings.py +++ b/koedquiz/settings.py @@ -39,8 +39,6 @@ TIME_ZONE = None # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'pl' -SITE_ID = 1 - # If you set this to False, Django will make some optimizations so as not # to load the internationalization machinery. USE_I18N = True @@ -95,12 +93,24 @@ TEMPLATE_LOADERS = [ # 'django.template.loaders.eggs.Loader', ] +TEMPLATE_CONTEXT_PROCESSORS = ( + "django.contrib.auth.context_processors.auth", + "django.core.context_processors.debug", + "django.core.context_processors.i18n", + "django.core.context_processors.media", + "django.core.context_processors.static", + "django.core.context_processors.tz", + "django.contrib.messages.context_processors.messages", + "django.core.context_processors.request", +) + MIDDLEWARE_CLASSES = [ 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'quiz.middleware.CurrentQuizMiddleware', ] ROOT_URLCONF = 'koedquiz.urls' diff --git a/koedquiz/urls.py b/koedquiz/urls.py index 7ff451a..72de887 100644 --- a/koedquiz/urls.py +++ b/koedquiz/urls.py @@ -3,12 +3,12 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf.urls import patterns, include, url - +from django.views.generic import TemplateView from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', - url(r'^$', 'koedquiz.views.home', name='main_page'), + url(r'^$', TemplateView.as_view(template_name="quiz/home.html"), name='main_page'), url(r'^quiz/', include('quiz.urls')), diff --git a/koedquiz/views.py b/koedquiz/views.py deleted file mode 100644 index b89fdda..0000000 --- a/koedquiz/views.py +++ /dev/null @@ -1,13 +0,0 @@ -# -*- coding: utf-8 -*- -# This file is part of KOED-Quiz, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from django.shortcuts import render - -from django.conf import settings -from quiz.models import Quiz - - -def home(request): - quiz = Quiz.current() - return render(request, "quiz/home.html", locals())