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

Commit

Permalink
Override wagtailsurveys index view and remove templatetag to get surveys
Browse files Browse the repository at this point in the history
Using a templatetag and overriding the default template is an anti-pattern, as it means that we are fetching surveys twice when dispalying them.
It also meant that we were not fetching all of the surveys within a site - only those that were children of SurveyIndexPage.
  • Loading branch information
nathanbegbie committed Mar 19, 2018
1 parent 4a8dbee commit 8282efd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 50 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.

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 8282efd

Please sign in to comment.