Skip to content

Commit 59e22a1

Browse files
committed
Add: Additional tests
1 parent 61dd95d commit 59e22a1

File tree

9 files changed

+101
-12
lines changed

9 files changed

+101
-12
lines changed

.travis.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
language: python
44

55
python:
6-
- "3.5"
6+
- "3.7"
77

88
env:
9-
- TOX_ENV=py35-django-19
10-
- TOX_ENV=py34-django-19
11-
- TOX_ENV=py35-django-110
12-
- TOX_ENV=py34-django-110
13-
- TOX_ENV=py35-django-111
14-
- TOX_ENV=py34-django-111
9+
- TOX_ENV=py36-django-111
10+
- TOX_ENV=py37-django-111
11+
- TOX_ENV=py36-django-20
12+
- TOX_ENV=py37-django-20
13+
- TOX_ENV=py36-django-21
14+
- TOX_ENV=py37-django-21
1515

1616
matrix:
1717
fast_finish: true

django_typeform/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from django.views import View
55
from django.views.generic.edit import FormMixin
66
from django.views.generic.base import TemplateResponseMixin
7-
from .forms import TypeformMixin
87

98

109
class BaseTypeformView(FormMixin, View):

docs/typeformmixin.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
TypeformMixin
33
=============
44

5-
The ``TypeformMixin`` is a mixin for Django's form class. To turn
6-
Typeform features on for a Django form just use the Mixin:
5+
To process a typeform in Django just the way you would process a Django form,
6+
use the ``TypeformMixin``. It requires the typeform to be mirrored by a corresponding
7+
Django form, question by question, but can also contain processing logic.
8+
To turn Typeform features on for a Django form just use the Mixin and specify
9+
the ``typeform_url``:
710

811
.. code-block:: python
912
@@ -14,7 +17,7 @@ Typeform features on for a Django form just use the Mixin:
1417
class MyTypeForm(TypeformMixin, MyForm)
1518
typeform_url = 'https://xxxx.typeform.com/to/xxxxxx'
1619
17-
``MyTypeForm`` will be a fully fledge Django form and at the same
20+
``MyTypeForm`` will be a fully fledged Django form and at the same
1821
time be linked to the Typeform with given url.
1922

2023
For this link to work, the ``Question reference`` for each Typeform

docs/typeformview.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
============
2+
TypeformView
3+
============
4+
5+
The ``TypeformView`` replaces the ``FormView`` class and is the default view
6+
for typeforms.
7+
8+
Upon get requests it displays the typeform using the ``template_name`` property
9+
just like a ``FormView``. Upon completion of the typeform the rendered
10+
typeform posts the typeform's unique id to the view.
11+
12+
``TypeformView`` intercepts this post request, uses the Typeform Results
13+
API and retrieves the answers the user has just entered. These answers are
14+
encoded as a ``QueryDict`` and forwarded for form processing.
15+
16+
This way the use of ``TypeformView`` becomes transparent for Django.
17+
18+
See this example, taken from ``urls.py``:
19+
20+
.. code-block:: python
21+
22+
from django.urls import path
23+
from django_typeform.views import TypeformView
24+
25+
from .forms import MyTypeForm
26+
27+
urlpatterns = [
28+
...,
29+
path('my-typeform/', TypeformView.as_view(
30+
form_class=MyTypeForm,
31+
template_name='mytypeform.html',
32+
)),
33+
...,
34+
]
35+
36+
where a simple template might look like this (``mytypeform.html``):
37+
38+
.. code-block:: html
39+
40+
{% extends 'base.html' %}
41+
{% block content %}
42+
<form method="post">
43+
{% include form with height='100vh' %}
44+
</form>
45+
{% endblock %}

tests/settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# -*- coding: utf-8
22
from __future__ import unicode_literals, absolute_import
33

4+
import os
5+
46
import django
57

8+
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
9+
610
DEBUG = True
711
USE_TZ = True
812

@@ -23,8 +27,18 @@
2327
"django.contrib.contenttypes",
2428
"django.contrib.sites",
2529
"django_typeform",
30+
"sekizai",
2631
]
2732

33+
TEMPLATES = [
34+
{
35+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
36+
'DIRS': [os.path.join(BASE_DIR, "tests/templates")],
37+
'APP_DIRS': True,
38+
},
39+
]
40+
41+
2842
SITE_ID = 1
2943

3044
if django.VERSION >= (1, 10):

tests/templates/typeform_test.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<form method="post">
2+
{% include form with height='100vh' %}
3+
</form>

tests/test_forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@
88
class MyForm(TypeformMixin, Form):
99
pass
1010

11+
1112
class TypeformMixinTest(SimpleTestCase):
1213
def test_mixin(self):
1314
form = MyForm(typeform_url='https://whatever.typeform.com/to/xxxxxx')
1415
self.assertEqual(form.typeform_id, 'xxxxxx')
1516

17+
def test_mixin_rendering(self):
18+
class Request(object):
19+
session = {}
20+
21+
request = Request()
22+
form = MyForm(typeform_url='https://whatever.typeform.com/to/xxxxxx')
23+
24+
form.get_uid(Request())
25+
self.assertFalse(not request.session, 'typeformuid not stored in session')
26+
form.render(Context({'request': Request()}))

tests/test_views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.test import SimpleTestCase
2+
from django.test import Client
3+
4+
class TypeformViewTest(SimpleTestCase):
5+
def test_view(self):
6+
c = Client()
7+
response = c.get('/testform/')
8+
self.assertEqual(response.status_code, 200)

tests/urls.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals, absolute_import
33

4-
from django.conf.urls import url, include
4+
from django.conf.urls import include, url
5+
from django_typeform.views import TypeformView
6+
from tests.test_forms import MyForm
57

68

79
urlpatterns = [
10+
url(r'^testform/', TypeformView.as_view(
11+
template_name='typeform_test.html',
12+
form_class=MyForm,
13+
))
814
]

0 commit comments

Comments
 (0)