Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Merge 8282efd into aa6048f
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanbegbie committed Mar 19, 2018
2 parents aa6048f + 8282efd commit 4486261
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 94 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ script:
- mkdir testapp/testapp/templates/core
- cp molo/surveys/test_templates/*.html testapp/testapp/templates/core/
- cp molo/surveys/test_templates/base.html testapp/testapp/templates/base.html
- mkdir testapp/testapp/templates/wagtailsurveys
- cp molo/surveys/test_templates/wagtailsurveys/*.html testapp/testapp/templates/wagtailsurveys/
- flake8 testapp
- pip install -e testapp
- py.test --cov=molo.surveys --cov-report=term
Expand Down
8 changes: 8 additions & 0 deletions molo/surveys/admin_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url

from molo.surveys.views import index

urlpatterns = [
# re-route to overwritten index view, originally in wagtailsurveys
url(r'^$', index, name='index'),
]
24 changes: 0 additions & 24 deletions molo/surveys/templates/wagtailsurveys/list_forms.html

This file was deleted.

24 changes: 0 additions & 24 deletions molo/surveys/test_templates/wagtailsurveys/list_forms.html

This file was deleted.

51 changes: 51 additions & 0 deletions molo/surveys/tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from molo.surveys.models import (
MoloSurveyFormField,
MoloSurveyPage,
SurveysIndexPage,
)
from .utils import skip_logic_data


def create_molo_survey_form_field(survey, sort_order, obj):
if obj['type'] == 'radio':
skip_logic = skip_logic_data(choices=obj['choices'])
else:
skip_logic = None

return MoloSurveyFormField.objects.create(
page=survey,
sort_order=sort_order,
label=obj["question"],
field_type=obj["type"],
required=obj["required"],
page_break=obj["page_break"],
admin_label=obj["question"].lower().replace(" ", "_"),
skip_logic=skip_logic
)


def create_molo_survey_page(
parent, title="Test Survey", slug='test-survey', **kwargs):
molo_survey_page = MoloSurveyPage(
title=title, slug=slug,
introduction='Introduction to Test Survey ...',
thank_you_text='Thank you for taking the Test Survey',
submit_text='survey submission text',
**kwargs
)

parent.add_child(instance=molo_survey_page)
molo_survey_page.save_revision().publish()

return molo_survey_page


def create_survey(fields={}, **kwargs):
survey = create_molo_survey_page(SurveysIndexPage.objects.first())

if not fields == {}:
num_questions = len(fields)
for index, field in enumerate(reversed(fields)):
sort_order = num_questions - (index + 1)
create_molo_survey_form_field(survey, sort_order, field)
return survey
20 changes: 20 additions & 0 deletions molo/surveys/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from wagtail_personalisation.models import Segment
from wagtail_personalisation.rules import UserIsLoggedInRule

from .base import create_molo_survey_page

User = get_user_model()


Expand Down Expand Up @@ -219,3 +221,21 @@ def test_export_submission_personalisable_survey(self):

self.assertContains(response, self.user.username)
self.assertContains(response, answer)

def test_survey_index_view_displays_all_surveys(self):
child_of_index_page = create_molo_survey_page(
self.surveys_index,
title="Child of SurveysIndexPage Survey",
slug="child-of-surveysindexpage-survey"
)

child_of_article_page = create_molo_survey_page(
self.article,
title="Child of Article Survey",
slug="child-of-article-survey"
)

self.client.force_login(self.super_user)
response = self.client.get('/admin/surveys/')
self.assertContains(response, child_of_index_page.title)
self.assertContains(response, child_of_article_page.title)
45 changes: 1 addition & 44 deletions molo/surveys/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

from .utils import skip_logic_block_data, skip_logic_data
from .base import create_survey


class TestSurveyModels(TestCase, MoloTestCaseMixin):
Expand Down Expand Up @@ -140,50 +141,6 @@ def test_question_passes_with_object(self):
self.assertEqual(cleaned_data['question'], 1)


def create_molo_survey_form_field(survey, sort_order, obj):
if obj['type'] == 'radio':
skip_logic = skip_logic_data(choices=obj['choices'])
else:
skip_logic = None

return MoloSurveyFormField.objects.create(
page=survey,
sort_order=sort_order,
label=obj["question"],
field_type=obj["type"],
required=obj["required"],
page_break=obj["page_break"],
admin_label=obj["question"].lower().replace(" ", "_"),
skip_logic=skip_logic
)


def create_molo_survey_page(parent, **kwargs):
molo_survey_page = MoloSurveyPage(
title='Test Survey', slug='test-survey',
introduction='Introduction to Test Survey ...',
thank_you_text='Thank you for taking the Test Survey',
submit_text='survey submission text',
**kwargs
)

parent.add_child(instance=molo_survey_page)
molo_survey_page.save_revision().publish()

return molo_survey_page


def create_survey(fields={}, **kwargs):
survey = create_molo_survey_page(SurveysIndexPage.objects.first())

if not fields == {}:
num_questions = len(fields)
for index, field in enumerate(reversed(fields)):
sort_order = num_questions - (index + 1)
create_molo_survey_form_field(survey, sort_order, field)
return survey


class TestPageBreakWithTwoQuestionsInOneStep(TestCase, MoloTestCaseMixin):
def setUp(self):
self.mk_main()
Expand Down
17 changes: 17 additions & 0 deletions molo/surveys/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,32 @@
from django.shortcuts import render
from django.http import JsonResponse
from django.utils.translation import ugettext as _
from wagtail.utils.pagination import paginate

from wagtail.wagtailadmin import messages
from wagtail.wagtailadmin.utils import permission_required
from wagtail_personalisation.forms import SegmentAdminForm
from wagtail_personalisation.models import Segment

from wagtailsurveys.models import get_surveys_for_user

from .forms import CSVGroupCreationForm


def index(request):
survey_pages = get_surveys_for_user(request.user)
survey_pages = (
survey_pages.descendant_of(request.site.root_page)
.filter(languages__language__is_main_language=True)
.specific()
)
paginator, survey_pages = paginate(request, survey_pages)

return render(request, 'wagtailsurveys/index.html', {
'survey_pages': survey_pages,
})


class SegmentCountForm(SegmentAdminForm):
class Meta:
model = Segment
Expand Down
11 changes: 11 additions & 0 deletions molo/surveys/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.conf import settings
from django.conf.urls import include, url
from django.utils.html import format_html_join
from django.contrib.auth.models import User

from wagtail.contrib.modeladmin.options import modeladmin_register
from wagtail.wagtailcore import hooks

from molo.surveys.models import MoloSurveyPage, SurveyTermsConditions
from molo.surveys import admin_urls
from molo.core.models import ArticlePage

from .admin import SegmentUserGroupAdmin
Expand Down Expand Up @@ -52,3 +54,12 @@ def create_new_page_relations(request, page, new_page):
.first()
relation.terms_and_conditions = new_article
relation.save()


# This overrwrites the wagtailsuveys admin urls in order to use custom
# survey index view
@hooks.register('register_admin_urls')
def register_admin_urls():
return [
url(r'^surveys/', include(admin_urls)),
]

0 comments on commit 4486261

Please sign in to comment.