From 4b6a361dcf7da8acb7d995e7bc71ef5dc83f2302 Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Mon, 23 Jul 2018 13:03:39 +0200 Subject: [PATCH 1/8] fix SurveyAbstractFormField's clean_name attr, add django smart_str to label --- molo/surveys/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/molo/surveys/models.py b/molo/surveys/models.py index 1c7f6d4..dcec457 100644 --- a/molo/surveys/models.py +++ b/molo/surveys/models.py @@ -13,6 +13,7 @@ from django.http import Http404 from django.shortcuts import redirect, render from django.utils.functional import cached_property +from django.utils.encoding import smart_str from django.utils.translation import ugettext_lazy as _ from django.utils.text import slugify from django.utils.six import text_type @@ -79,7 +80,7 @@ class Meta: @property def clean_name(self): return str(slugify(text_type(unidecode( - '{} {}'.format(self.pk, self.label.lower()))))) + u'{} {}'.format(self.pk, smart_str(self.label)))))) class TermsAndConditionsIndexPage(TranslatablePageMixinNotRoutable, MoloPage): From c14f87ae87e7aae2d0b9499ce791f0425007562d Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Mon, 23 Jul 2018 13:12:16 +0200 Subject: [PATCH 2/8] remove useless django smart_str --- molo/surveys/models.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/molo/surveys/models.py b/molo/surveys/models.py index dcec457..1557a66 100644 --- a/molo/surveys/models.py +++ b/molo/surveys/models.py @@ -13,7 +13,6 @@ from django.http import Http404 from django.shortcuts import redirect, render from django.utils.functional import cached_property -from django.utils.encoding import smart_str from django.utils.translation import ugettext_lazy as _ from django.utils.text import slugify from django.utils.six import text_type @@ -80,7 +79,7 @@ class Meta: @property def clean_name(self): return str(slugify(text_type(unidecode( - u'{} {}'.format(self.pk, smart_str(self.label)))))) + u'{} {}'.format(self.pk, self.label))))) class TermsAndConditionsIndexPage(TranslatablePageMixinNotRoutable, MoloPage): From 51e9578cb60bc9b5f82c01d749cff82f4d009303 Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Mon, 23 Jul 2018 16:35:37 +0200 Subject: [PATCH 3/8] increase coverage for CI --- molo/surveys/tests/test_models.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/molo/surveys/tests/test_models.py b/molo/surveys/tests/test_models.py index b570b21..3c05a4f 100644 --- a/molo/surveys/tests/test_models.py +++ b/molo/surveys/tests/test_models.py @@ -2,9 +2,12 @@ from django.test import TestCase from molo.core.tests.base import MoloTestCaseMixin from molo.surveys.blocks import SkipLogicBlock, SkipState +from django.contrib.auth import get_user_model from molo.surveys.models import ( MoloSurveyFormField, MoloSurveyPage, + ArticlePage, + MoloSurveyPageView, MoloSurveySubmission, SurveysIndexPage, PersonalisableSurvey, @@ -472,6 +475,11 @@ def test_datetime_molo_form_fields_not_clean_with_invalid_default(self): self.assertEqual(e.exception.messages, ['Must be a valid date']) + def test_date_personalisabe_form_str_representation(self): + field = self.create_personalisable_survey_form_field('date') + self.assertEqual( + field.__str__(), 'Test Survey - When is your birthday') + def test_date_personalisabe_form_fields_clean_if_blank(self): field = self.create_personalisable_survey_form_field('date') field.default_value = "" @@ -520,3 +528,17 @@ def test_datetime_personalisable_fields_not_clean_with_invalid_default( field.clean() self.assertEqual(e.exception.messages, ['Must be a valid date']) + + +class TestMoloSurveyPageView(TestCase, MoloTestCaseMixin): + """ Test case """ + + def test_model(self): + self.mk_main() + user = get_user_model().objects.create_superuser( + username='superuser', + email='superuser@email.com', password='pass' + ) + survey = ArticlePage(title='Test Survey', slug='test-survey') + model = MoloSurveyPageView(user=user, page=survey) + self.assertEqual('superuser viewed Test Survey at' in model.__str__()) From d6bb8245bf54c2d74ec6c311a2040618517edf96 Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Mon, 23 Jul 2018 16:43:47 +0200 Subject: [PATCH 4/8] fix wrong assertion --- molo/surveys/tests/test_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molo/surveys/tests/test_models.py b/molo/surveys/tests/test_models.py index 3c05a4f..79c060c 100644 --- a/molo/surveys/tests/test_models.py +++ b/molo/surveys/tests/test_models.py @@ -541,4 +541,4 @@ def test_model(self): ) survey = ArticlePage(title='Test Survey', slug='test-survey') model = MoloSurveyPageView(user=user, page=survey) - self.assertEqual('superuser viewed Test Survey at' in model.__str__()) + self.assertTrue('superuser viewed Test Survey at' in model.__str__()) From d189071654f623eb7a02e672f4a1d451688ec54d Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Tue, 24 Jul 2018 10:43:43 +0200 Subject: [PATCH 5/8] wrap FormField's label with smart_unicode encoding in clean_name property --- molo/surveys/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/molo/surveys/models.py b/molo/surveys/models.py index 1557a66..6d5f47e 100644 --- a/molo/surveys/models.py +++ b/molo/surveys/models.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import json import datetime from unidecode import unidecode @@ -15,6 +17,7 @@ from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from django.utils.text import slugify +from django.utils.encoding import smart_unicode from django.utils.six import text_type from modelcluster.fields import ParentalKey from molo.core.blocks import MarkDownBlock @@ -79,7 +82,7 @@ class Meta: @property def clean_name(self): return str(slugify(text_type(unidecode( - u'{} {}'.format(self.pk, self.label))))) + u'{} {}'.format(self.pk, smart_unicode(self.label)))))) class TermsAndConditionsIndexPage(TranslatablePageMixinNotRoutable, MoloPage): From f2475af17fe7e160173d950b4c83b90d6b0b7bd3 Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Tue, 24 Jul 2018 10:52:41 +0200 Subject: [PATCH 6/8] extend tests --- molo/surveys/tests/test_models.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/molo/surveys/tests/test_models.py b/molo/surveys/tests/test_models.py index 79c060c..b857c32 100644 --- a/molo/surveys/tests/test_models.py +++ b/molo/surveys/tests/test_models.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from django.core.exceptions import ValidationError from django.test import TestCase from molo.core.tests.base import MoloTestCaseMixin @@ -411,7 +413,7 @@ def create_molo_survey_form_field(self, field_type): admin_label="birthday", ) - def create_personalisable_survey_form_field(self, field_type): + def create_personalisable_survey_form_field(self, field_type, label="When is your birthday"): survey = PersonalisableSurvey( title='Test Survey', introduction='Introduction to Test Survey ...', @@ -422,7 +424,7 @@ def create_personalisable_survey_form_field(self, field_type): return PersonalisableSurveyFormField.objects.create( page=survey, - label="When is your birthday", + label=label, field_type=field_type, admin_label="birthday", ) @@ -476,9 +478,10 @@ def test_datetime_molo_form_fields_not_clean_with_invalid_default(self): self.assertEqual(e.exception.messages, ['Must be a valid date']) def test_date_personalisabe_form_str_representation(self): - field = self.create_personalisable_survey_form_field('date') + field = self.create_personalisable_survey_form_field( + 'date', label="When is your birthdáy") self.assertEqual( - field.__str__(), 'Test Survey - When is your birthday') + field.__str__(), 'Test Survey - When is your birthd\xc3\xa1y') def test_date_personalisabe_form_fields_clean_if_blank(self): field = self.create_personalisable_survey_form_field('date') From a7e0118afe594b92f1c49a72ad544dcad7191dcc Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Tue, 24 Jul 2018 10:53:43 +0200 Subject: [PATCH 7/8] flake8 --- molo/surveys/tests/test_models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/molo/surveys/tests/test_models.py b/molo/surveys/tests/test_models.py index b857c32..abc115d 100644 --- a/molo/surveys/tests/test_models.py +++ b/molo/surveys/tests/test_models.py @@ -413,7 +413,8 @@ def create_molo_survey_form_field(self, field_type): admin_label="birthday", ) - def create_personalisable_survey_form_field(self, field_type, label="When is your birthday"): + def create_personalisable_survey_form_field( + self, field_type, label="When is your birthday"): survey = PersonalisableSurvey( title='Test Survey', introduction='Introduction to Test Survey ...', From ce49241b2daee309aff5fd323b9b5d69ac8f545f Mon Sep 17 00:00:00 2001 From: Aphiwe Moshesh Date: Tue, 24 Jul 2018 11:37:17 +0200 Subject: [PATCH 8/8] use py3 version for backward compatibility to py2 --- molo/surveys/models.py | 4 ++-- molo/surveys/tests/test_models.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/molo/surveys/models.py b/molo/surveys/models.py index 6d5f47e..d4b1ff1 100644 --- a/molo/surveys/models.py +++ b/molo/surveys/models.py @@ -17,7 +17,7 @@ from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from django.utils.text import slugify -from django.utils.encoding import smart_unicode +from django.utils.encoding import smart_str from django.utils.six import text_type from modelcluster.fields import ParentalKey from molo.core.blocks import MarkDownBlock @@ -82,7 +82,7 @@ class Meta: @property def clean_name(self): return str(slugify(text_type(unidecode( - u'{} {}'.format(self.pk, smart_unicode(self.label)))))) + u'{} {}'.format(self.pk, smart_str(self.label)))))) class TermsAndConditionsIndexPage(TranslatablePageMixinNotRoutable, MoloPage): diff --git a/molo/surveys/tests/test_models.py b/molo/surveys/tests/test_models.py index abc115d..060127d 100644 --- a/molo/surveys/tests/test_models.py +++ b/molo/surveys/tests/test_models.py @@ -481,8 +481,11 @@ def test_datetime_molo_form_fields_not_clean_with_invalid_default(self): def test_date_personalisabe_form_str_representation(self): field = self.create_personalisable_survey_form_field( 'date', label="When is your birthdáy") - self.assertEqual( - field.__str__(), 'Test Survey - When is your birthd\xc3\xa1y') + + self.assertTrue( + 'Test Survey - When is your birthd' in + field.__str__() + ) def test_date_personalisabe_form_fields_clean_if_blank(self): field = self.create_personalisable_survey_form_field('date')