-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0442430
Showing
50 changed files
with
1,943 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# https://editorconfig.org/ | ||
|
||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
end_of_line = lf | ||
charset = utf-8 | ||
|
||
# Docstrings and comments use max_line_length = 79 | ||
[*.py] | ||
max_line_length = 119 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# If you need to exclude files such as those generated by an IDE, use | ||
# $GIT_DIR/info/exclude or the core.excludesFile configuration variable as | ||
# described in https://git-scm.com/docs/gitignore | ||
|
||
*.egg-info | ||
*.pot | ||
*.py[co] | ||
.coverage | ||
.tox/ | ||
__pycache__ | ||
MANIFEST | ||
dist/ | ||
|
||
# local settings overrides | ||
example_app/settings_local.py | ||
tests/settings_local.py | ||
# media | ||
media/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
language: python | ||
sudo: false | ||
cache: pip | ||
python: | ||
- "3.7" | ||
- "3.6" | ||
- "3.5" | ||
- "2.7" | ||
install: pip install tox-travis | ||
script: tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 anfema GmbH | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
include LICENSE | ||
include README.md | ||
recursive-include questionnaire_core/static * | ||
recursive-include questionnaire_core/templates * | ||
prune example_app | ||
prune tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# django-questionnaire-core | ||
|
||
A django application which can be used as a base / starting point for questionnaire functionality in your project. | ||
It heavily relies on django form fields & widgets and uses PostgreSQL JSON fields to store the results. | ||
|
||
## Requirements | ||
|
||
- [Django](https://www.djangoproject.com) version 1.11, 2.0 or 2.1 | ||
- [django-ordered-model](https://github.com/bfirsh/django-ordered-model) (see | ||
[Compatibility matrix](https://github.com/bfirsh/django-ordered-model#compatibility-with-django-and-python)) | ||
- A [PostgreSQL](https://www.postgresql.org/) Database | ||
|
||
|
||
## Quick start | ||
|
||
1. Add "questionnaire_core" and its dependency to your INSTALLED_APPS setting like this: | ||
|
||
``` | ||
INSTALLED_APPS = [ | ||
... | ||
'ordered_model', | ||
'questionnaire_core', | ||
] | ||
``` | ||
2. Create a view based on `questionnaire_core.views.generic.QuestionnaireFormView`; a simple version might look like this: | ||
```python | ||
class BasicQuestionnaireView(QuestionnaireFormView): | ||
template_name = 'basic_questionnaire.html' | ||
def get_questionnaire(self): | ||
return Questionnaire.objects.get(pk=self.kwargs.get('pk')) | ||
def get_questionnaire_result_set(self): | ||
if self.request.GET.get('result_set'): | ||
return QuestionnaireResult.objects.get(pk=self.request.GET.get('result_set')) | ||
return QuestionnaireResult(questionnaire=self.get_questionnaire()) | ||
def get_success_url(self): | ||
return reverse('basic_questionnaire', args=(self.kwargs.get('pk'),)) | ||
def form_valid(self, form): | ||
# Add current result set to the url (allows editing of the result) | ||
redirect = super(BasicQuestionnaireView, self).form_valid(form) | ||
url_params = urlencode({'result_set': form.current_result_set.pk}) | ||
return HttpResponseRedirect('{url}?{params}'.format(url=redirect.url, params=url_params)) | ||
``` | ||
3. Add the new view to your URLconf: | ||
``` | ||
url(r'^questionnaire/(?P<pk>[0-9]+)/$', BasicQuestionnaireView.as_view(), name='basic_questionnaire'), | ||
``` | ||
4. Run `python manage.py migrate` to create the questionnaire_core models. | ||
5. Start the development server and visit http://127.0.0.1:8000/admin/ | ||
to create a questionnaire (you'll need the Admin app enabled). | ||
6. Visit http://127.0.0.1:8000/questionnaire/1/ to test your first questionnaire. | ||
## Development setup | ||
1. Upgrade packaging tools: | ||
```bash | ||
pip install --upgrade pip setuptools wheel | ||
``` | ||
2. Install Django (the `example_app` expects django 1.11): | ||
```bash | ||
pip install Django~=1.11.0 django-ordered-model~=2.1.0 psycopg2 | ||
``` | ||
3. Install tox, isort & flake8 | ||
```bash | ||
pip install tox isort flake8 | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Minimal settings file to develop questionnaire_core. | ||
Use `settings_local.py` to override any settings. | ||
""" | ||
import os | ||
|
||
|
||
APP_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
|
||
DEBUG = True | ||
|
||
SECRET_KEY = "yRMdC-vQ4c8c*Biil(&&aEjG&cDBff=DIWp(wYRLWovM$vVC/=@CZoRO6EGX`#s" | ||
|
||
INSTALLED_APPS = ( | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.admin', | ||
'django.contrib.messages', | ||
'django.contrib.sessions', | ||
'django.contrib.staticfiles', | ||
'ordered_model', | ||
'questionnaire_core', | ||
) | ||
|
||
ROOT_URLCONF = 'example_app.urls' | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'NAME': 'questionnaire_core', | ||
}, | ||
} | ||
|
||
MIDDLEWARE = ( | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
) | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [ | ||
os.path.join(APP_DIR, 'example_app/templates') | ||
], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
STATIC_URL = '/static/' | ||
|
||
MEDIA_ROOT = os.path.join(APP_DIR, 'media') | ||
MEDIA_URL = '/media/' | ||
|
||
try: | ||
from .settings_local import * # NOQA | ||
except ImportError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{% load i18n static %} | ||
<!DOCTYPE html> | ||
<html lang="en-us"> | ||
<head> | ||
<title>{% block title %}{% endblock %}</title> | ||
<style type="text/css"> | ||
ul { list-style: none; padding: 0; } | ||
li { list-style-type: none; margin-bottom: 1em; } | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id="content"> | ||
{% block content %} | ||
{{ content }} | ||
<form action="" method="post" enctype="multipart/form-data"> | ||
<ul> | ||
{{ form.as_ul }} | ||
</ul> | ||
<input type="submit"> | ||
<input type="reset"> | ||
{% if request.GET.result_set %} | ||
<a href="{% url 'basic_questionnaire_form' form.current_questionnaire.pk %}">new result set</a> | ||
{% endif %} | ||
</form> | ||
{% endblock %} | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.conf.urls import include, url | ||
from django.contrib import admin | ||
from django.conf import settings | ||
from django.conf.urls.static import static | ||
|
||
from .views import BasicQuestionnaireView | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^questionnaire/(?P<pk>[0-9]+)/$', BasicQuestionnaireView.as_view(), name='basic_questionnaire_form'), | ||
url(r'^admin/', include(admin.site.urls)), | ||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from django.http import HttpResponseRedirect | ||
from django.urls import reverse | ||
from django.utils.http import urlencode | ||
|
||
from questionnaire_core.models import Questionnaire, QuestionnaireResult | ||
from questionnaire_core.views.generic import QuestionnaireFormView | ||
|
||
|
||
class BasicQuestionnaireView(QuestionnaireFormView): | ||
template_name = 'questionnaire_form.html' | ||
|
||
def get_questionnaire(self): | ||
return Questionnaire.objects.get(pk=self.kwargs.get('pk')) | ||
|
||
def get_questionnaire_result_set(self): | ||
if self.request.GET.get('result_set'): | ||
return QuestionnaireResult.objects.get(pk=self.request.GET.get('result_set')) | ||
return QuestionnaireResult(questionnaire=self.get_questionnaire()) | ||
|
||
def get_success_url(self): | ||
return reverse('basic_questionnaire_form', args=(self.kwargs.get('pk'),)) | ||
|
||
def form_valid(self, form): | ||
# Add current result set to the url (allows editing of the result) | ||
redirect = super(BasicQuestionnaireView, self).form_valid(form) | ||
url_params = urlencode({'result_set': form.current_result_set.pk}) | ||
return HttpResponseRedirect('{url}?{params}'.format(url=redirect.url, params=url_params)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1 and sys.argv[1] == 'test': | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings") | ||
else: | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_app.settings") | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
name = 'questionnaire_core' | ||
default_app_config = 'questionnaire_core.apps.QuestionnaireCoreConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
import json | ||
|
||
from django.contrib import admin | ||
from django.contrib.postgres import fields as postgres_fields | ||
from django.forms import widgets | ||
from ordered_model.admin import OrderedTabularInline | ||
|
||
from .models import Question, QuestionAnswer, Questionnaire, QuestionnaireResult | ||
|
||
|
||
try: | ||
from ordered_model.admin import OrderedInlineModelAdminMixin # v3+ | ||
except ImportError: | ||
class OrderedInlineModelAdminMixin(object): | ||
def get_urls(self): | ||
urls = super(OrderedInlineModelAdminMixin, self).get_urls() | ||
for inline in self.inlines: | ||
if hasattr(inline, 'get_urls'): | ||
urls = inline.get_urls(self) + urls | ||
return urls | ||
|
||
|
||
class PrettyJSONWidget(widgets.Textarea): | ||
def format_value(self, value): | ||
try: | ||
return json.dumps(json.loads(value), indent=2) # reformat json | ||
except TypeError: | ||
return value | ||
|
||
|
||
class QuestionnaireQuestionListModelInline(OrderedTabularInline): | ||
model = Question | ||
fields = ('question_type', 'question_text', 'question_options', 'required', 'order', 'move_up_down_links',) | ||
readonly_fields = ('order', 'move_up_down_links',) | ||
extra = 1 | ||
ordering = ('order',) | ||
formfield_overrides = { | ||
postgres_fields.JSONField: { | ||
'widget': PrettyJSONWidget | ||
}, | ||
} | ||
|
||
|
||
class QuestionnaireAnswerListModelInline(OrderedTabularInline): | ||
model = QuestionAnswer | ||
fields = ('question', 'answer_data', 'order', 'move_up_down_links',) | ||
readonly_fields = ('order', 'move_up_down_links',) | ||
extra = 0 | ||
ordering = ('order',) | ||
|
||
|
||
@admin.register(Questionnaire) | ||
class QuestionnaireAdmin(OrderedInlineModelAdminMixin, admin.ModelAdmin): | ||
list_display = ('title', ) | ||
inlines = (QuestionnaireQuestionListModelInline,) | ||
|
||
|
||
@admin.register(Question) | ||
class QuestionAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
|
||
@admin.register(QuestionnaireResult) | ||
class QuestionnaireResultAdmin(OrderedInlineModelAdminMixin, admin.ModelAdmin): | ||
readonly_fields = ('created_at', 'updated_at') | ||
inlines = (QuestionnaireAnswerListModelInline,) | ||
|
||
|
||
@admin.register(QuestionAnswer) | ||
class QuestionAnswerAdmin(admin.ModelAdmin): | ||
pass |
Oops, something went wrong.