Get current quiz by domain from request.
authorRadek Czajka <radekczajka@nowoczesnapolska.org.pl>
Fri, 26 Sep 2014 13:41:43 +0000 (15:41 +0200)
committerRadek Czajka <radekczajka@nowoczesnapolska.org.pl>
Fri, 26 Sep 2014 13:41:43 +0000 (15:41 +0200)
apps/quiz/middleware.py [new file with mode: 0644]
apps/quiz/models.py
apps/quiz/templates/quiz/home.html
apps/quiz/templatetags/quiz_tags.py
apps/quiz/views.py
koedquiz/settings.py
koedquiz/urls.py
koedquiz/views.py [deleted file]

diff --git a/apps/quiz/middleware.py b/apps/quiz/middleware.py
new file mode 100644 (file)
index 0000000..3cde07d
--- /dev/null
@@ -0,0 +1,5 @@
+from .models import Quiz
+
+class CurrentQuizMiddleware(object):
+    def process_request(self, request):
+        request.current_quiz = Quiz.current(request)
index 74142da..8916ce9 100644 (file)
@@ -19,8 +19,8 @@ class Quiz(Site):
         verbose_name_plural = _('quizzes')
 
     @classmethod
         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]
 
     def start(self):
         return self.question_set.all()[0]
index 34efdd7..ca67f39 100644 (file)
@@ -2,16 +2,16 @@
 {% load i18n %}
 
 
 {% 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" %}
 
 <div>
 
 
 {% block "body" %}
 
 <div>
-{{ quiz.description|safe }}
+{{ request.current_quiz.description|safe }}
 </div>
 
 </div>
 
-<a class='big-button' href="{{ quiz.get_absolute_url }}">{% trans "Start the test!" %}</a>
+<a class='big-button' href="{{ request.current_quiz.get_absolute_url }}">{% trans "Start the test!" %}</a>
 
 {% endblock %}
 
 {% endblock %}
index 2790ae2..6f7af26 100755 (executable)
@@ -9,6 +9,6 @@ from quiz.models import Quiz
 register = template.Library()
 
 
 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)
index 3bd3a84..75b918d 100644 (file)
@@ -10,10 +10,10 @@ from quiz.models import Quiz
 
 def question(request, slug=None):
     if slug is None:
 
 def question(request, slug=None):
     if slug is None:
-        question = Quiz.current().start()
+        question = request.current_quiz.start()
         request.session['ticket'] = [request.path]
     else:
         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
 
     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]
 
     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())
 
     return render(request, "quiz/result_detail.html", locals())
 
index 13ce2ef..6b17288 100644 (file)
@@ -39,8 +39,6 @@ TIME_ZONE = None
 # http://www.i18nguy.com/unicode/language-identifiers.html
 LANGUAGE_CODE = 'pl'
 
 # 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
 # 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',
 ]
 
 #     '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',
 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'
 ]
 
 ROOT_URLCONF = 'koedquiz.urls'
index 7ff451a..72de887 100644 (file)
@@ -3,12 +3,12 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf.urls import patterns, include, url
 # 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('',
 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')),
 
 
     url(r'^quiz/', include('quiz.urls')),
 
diff --git a/koedquiz/views.py b/koedquiz/views.py
deleted file mode 100644 (file)
index b89fdda..0000000
+++ /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())