1 # -*- coding: utf-8 -*-
2 # This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
5 from __future__ import unicode_literals
7 from django.http import HttpResponse
8 from django.shortcuts import render
9 from django.utils import translation
10 from ssify import ssi_included, ssi_expect, SsiVariable as V
13 @ssi_included(use_lang=False, get_ssi_vars=lambda number: [
14 ('test_tags.number_of_quotes',),
15 ('test_tags.quote_len_odd', [ssi_expect(number, int)])
17 def quote(request, number):
19 return render(request, 'tests_basic/quote.html', {
21 'quote': QUOTES[number]
25 @ssi_included(use_lang=False)
26 def quote_undeclared(request, number):
28 return render(request, 'tests_basic/quote.html', {
30 'quote': QUOTES[number]
34 @ssi_included(use_lang=False, get_ssi_vars=lambda number: [
35 ('test_tags.number_of_quotes',),
36 ('test_tags.quote_len_odd', [ssi_expect(number, int)]),
37 ('test_tags.quote_len_odd', [V('nonexistent')]),
39 def quote_overdeclared(request, number):
41 return render(request, 'tests_basic/quote.html', {
43 'quote': QUOTES[number]
48 @ssi_included(use_lang=False, get_ssi_vars=lambda: (
49 lambda number: [number] + quote.get_ssi_vars(number))(
50 number=V('test_tags.random_number', (), {'limit': V('test_tags.number_of_quotes')})
52 def random_quote(request):
54 This view is purposely overcomplicated to test interdependencies
55 between SSI variables and SSI includes.
57 It finds number of quotes and sets that in an SSI variable,
58 then uses that to set a random quote number in a second variable,
59 then sets a third saying if the length of the selected quote is odd.
62 return render(request, 'tests_basic/random_quote.html')
65 def language(request):
66 assert request.LANGUAGE_CODE == translation.get_language()
67 return HttpResponse(request.LANGUAGE_CODE)
69 language_without_lang = ssi_included(use_lang=False)(language)
70 language_with_lang = ssi_included(language)
75 @ssi_included(use_lang=False, get_ssi_vars=lambda limit: (
76 ('test_tags.random_number', [], {'limit': ssi_expect(limit, int)}),
77 ('test_tags.random_number', [V(
78 'test_tags.random_number', [], {'limit': ssi_expect(limit, int)}
80 ('test_tags.random_number', [], {'limit': V(
81 'test_tags.random_number', [V(
82 'test_tags.random_number', [], {'limit': ssi_expect(limit, int)}
86 def args(request, limit):
87 return render(request, 'tests_args/args.html', {'limit': int(limit)})
90 def csrf_check(request):
91 return HttpResponse(request.POST['test'])
94 # Nothing interesting here.
97 stdout_backup = sys.stdout
100 from cStringIO import StringIO
101 sys.stdout = StringIO()
104 from io import BytesIO, TextIOWrapper
105 sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding)
108 this_string = sys.stdout.read()
110 sys.stdout = stdout_backup
111 return this_string.split('\n')