def create_security_question_index_page(sender, instance, **kwargs): if not instance.get_children().filter(title='Security Questions').exists(): security_index = SecurityQuestionIndexPage( title='Security Questions', slug=('security-questions-%s' % (generate_slug(instance.title), ))) instance.add_child(instance=security_index) security_index.save_revision().publish()
def add_translation(request, page_id, locale): _page = get_object_or_404(Page, id=page_id) page = _page.specific if not hasattr(page, 'get_translation_for'): messages.add_message( request, messages.INFO, _('That page is not translatable.')) return redirect(reverse('wagtailadmin_home')) # redirect to edit page if translation already exists for this locale translated_page = page.get_translation_for(locale, is_live=None) if translated_page: return redirect( reverse('wagtailadmin_pages:edit', args=[translated_page.id])) # create translation and redirect to edit page language = get_object_or_404(SiteLanguage, locale=locale) new_title = str(language) + " translation of %s" % page.title new_slug = generate_slug(new_title) translation = page.__class__( title=new_title, slug=new_slug) page.get_parent().add_child(instance=translation) translation.save_revision() language_relation = translation.languages.first() language_relation.language = language language_relation.save() translation.save_revision() # make sure new translation is in draft mode translation.unpublish() PageTranslation.objects.get_or_create( page=page, translated_page=translation) return redirect( reverse('wagtailadmin_pages:edit', args=[translation.id]))
def test_two_sites_point_to_one_root_page(self): # assert that there is only one site rooted at main self.assertEqual(self.main.sites_rooted_here.count(), 1) client_1 = Client() # add a site that points to the same root page site_2 = Site.objects.create(hostname=generate_slug('site2'), port=80, root_page=self.main) # create a link buetween the current langauges and the new site Languages.objects.create(site_id=site_2.pk) SiteLanguageRelation.objects.create( language_setting=Languages.for_site(site_2), locale='en', is_active=True) client_2 = Client(HTTP_HOST=site_2.hostname) # assert that there are now two sites rooted at main self.assertEqual(self.main.sites_rooted_here.count(), 2) # assert that the correct hostname is returned for both sites response = client_1.get('/sections-main-1/your-mind/') self.assertEqual(response.status_code, 200) response = client_2.get(site_2.root_url + '/sections-main-1/your-mind/') self.assertEqual(response.status_code, 200)
def mk_articles(self, parent, count=2, **kwargs): articles = [] for i in range(count): data = {} data.update({ 'title': 'Test page %s' % (i, ), 'subtitle': 'Sample page description for %s' % (i, ), 'body': json.dumps([{ 'type': 'paragraph', 'value': 'Sample page content for %s' % (i, ) }]), }) data.update(kwargs) data.update({'slug': generate_slug(data['title'])}) article = ArticlePage(**data) if not article.first_published_at: article.first_published_at = timezone.now() parent.add_child(instance=article) article.save_revision().publish() articles.append(article) return articles
def test_two_sites_point_to_one_root_page(self): # assert that there is only one site rooted at main self.assertEquals(self.main.sites_rooted_here.count(), 1) client_1 = Client() # add a site that points to the same root page site_2 = Site.objects.create( hostname=generate_slug('site2'), port=80, root_page=self.main) # create a link buetween the current langauges and the new site Languages.objects.create( site_id=site_2.pk) SiteLanguageRelation.objects.create( language_setting=Languages.for_site(site_2), locale='en', is_active=True) client_2 = Client(HTTP_HOST=site_2.hostname) # assert that there are now two sites rooted at main self.assertEquals(self.main.sites_rooted_here.count(), 2) # create molo survey page molo_survey_page, molo_survey_form_field = \ self.create_molo_survey_page( parent=self.surveys_index, homepage_button_text='share your story yo') # assert that both sites render the survey response = client_1.get('/surveys-main-1/test-survey/') self.assertEquals(response.status_code, 200) response = client_2.get( site_2.root_url + '/surveys-main-1/test-survey/') self.assertEquals(response.status_code, 200)
def create_survey_index_pages(sender, instance, **kwargs): if not instance.get_children().filter(title='Surveys').exists(): survey_index = SurveysIndexPage(title='Surveys', slug=('surveys-{}'.format( generate_slug(instance.title), ))) instance.add_child(instance=survey_index) survey_index.save_revision().publish()
def test_two_sites_point_to_one_root_page(self): # assert that there is only one site rooted at main self.assertEqual(self.main.sites_rooted_here.count(), 1) client_1 = Client() # add a site that points to the same root page site_2 = Site.objects.create(hostname=generate_slug('site2'), port=80, root_page=self.main) # create a link buetween the current langauges and the new site Languages.objects.create(site_id=site_2.pk) SiteLanguageRelation.objects.create( language_setting=Languages.for_site(site_2), locale='en', is_active=True) client_2 = Client(HTTP_HOST=site_2.hostname) # assert that there are now two sites rooted at main self.assertEqual(self.main.sites_rooted_here.count(), 2) # create molo form page molo_form_page, molo_form_field = \ self.create_molo_form_page( parent=self.forms_index, homepage_button_text='share your story yo') # assert that both sites render the form response = client_1.get('/molo-forms-main-1/test-form/') self.assertEqual(response.status_code, 200) response = client_2.get(site_2.root_url + '/molo-forms-main-1/test-form/') self.assertEqual(response.status_code, 200)
def mk_reaction_question(self, parent, article, **kwargs): data = {} data.update({ 'title': 'Test Question', }) data.update(kwargs) data.update({ 'slug': generate_slug(data['title']) }) question = ReactionQuestion(**data) parent.add_child(instance=question) question.save_revision().publish() choice1 = ReactionQuestionChoice( title='yes', success_message='well done') question.add_child(instance=choice1) choice1.save_revision().publish() choice2 = ReactionQuestionChoice(title='maybe') question.add_child(instance=choice2) choice2.save_revision().publish() choice3 = ReactionQuestionChoice(title='no') question.add_child(instance=choice3) choice3.save_revision().publish() ArticlePageReactionQuestions.objects.create( reaction_question=question, page=article) return question
def create_form_index_pages(sender, instance, **kwargs): if not instance.get_children().filter(title='Forms').exists(): form_index = FormsIndexPage(title='Molo Forms', slug=('molo-forms-{}'.format( generate_slug(instance.title), ))) instance.add_child(instance=form_index) form_index.save_revision().publish()
def create_survey_index_pages(sender, instance, **kwargs): if not instance.get_children().filter( title='Surveys').exists(): survey_index = SurveysIndexPage( title='Surveys', slug=('surveys-{}'.format( generate_slug(instance.title), ))) instance.add_child(instance=survey_index) survey_index.save_revision().publish()
def test_slugify(self): self.mk_main() main = Main.objects.all().first() self.language_setting = Languages.objects.create( site_id=main.get_site().pk) self.english = SiteLanguageRelation.objects.create( language_setting=self.language_setting, locale='en', is_active=True) self.mk_section(self.main, title='Your mind') self.assertEqual(generate_slug('Your mind'), 'your-mind-1') self.mk_section(self.main, title='Your mind') self.assertEqual(generate_slug('Your mind'), 'your-mind-2') self.assertEqual(generate_slug(None), 'no-title')
def create_yourwords_competition_index_page(sender, instance, **kwargs): if not instance.get_children().filter( title='Your words competitions').exists(): yourwords_competition_index = YourWordsCompetitionIndexPage( title='Your words competitions', slug=('yourwords-%s' % (generate_slug(instance.title), ))) instance.add_child(instance=yourwords_competition_index) yourwords_competition_index.save_revision().publish()
def mk_tag(self, parent, slug=None, **kwargs): data = {} data.update({ 'title': 'Test Tag', }) data.update(kwargs) if slug: data.update({'slug': slug}) else: data.update({'slug': generate_slug(data['title'])}) tag = Tag(**data) parent.add_child(instance=tag) tag.save_revision().publish() return tag
def mk_banners(self, parent, count=2, **kwargs): banners = [] for i in range(count): data = {} data.update({ 'title': 'Test Banner {}'.format(i), }) data.update(kwargs) data.update({'slug': generate_slug(data['title'])}) banner = BannerPage(**data) parent.add_child(instance=banner) banner.save_revision().publish() banners.append(banner) return banners
def mk_tags(self, parent, count=2, **kwargs): tags = [] for i in range(count): data = {} data.update({ 'title': 'Test Tag {}'.format(i), }) data.update(kwargs) data.update({'slug': generate_slug(data['title'])}) tag = Tag(**data) parent.add_child(instance=tag) tag.save_revision().publish() tags.append(tag) return tags
def get_or_create(cls, parent, uuid, title): if cls.objects.filter(uuid=uuid).exists(): return cls.objects.get(uuid=uuid) instance = cls(uuid=uuid, title=title) try: parent.add_child(instance=instance) except ValidationError: # non-ascii titles need slugs to be manually generated instance.slug = generate_slug(title) parent.add_child(instance=instance) instance.save_revision().publish() return instance
def mk_sections(self, parent, count=2, **kwargs): sections = [] for i in range(count): data = {} data.update({ 'title': 'Test Section %s' % (i, ), }) data.update(kwargs) data.update({ 'slug': generate_slug(data['title']), }) section = SectionPage(**data) parent.add_child(instance=section) section.save_revision().publish() sections.append(section) return sections
def mk_banners(self, parent, count=2, **kwargs): banners = [] for i in range(count): data = {} data.update({ 'title': 'Test Banner {}'.format(i), }) data.update(kwargs) data.update({ 'slug': generate_slug(data['title']) }) banner = BannerPage(**data) parent.add_child(instance=banner) banner.save_revision().publish() banners.append(banner) return banners
def mk_tags(self, parent, count=2, **kwargs): tags = [] for i in range(count): data = {} data.update({ 'title': 'Test Tag {}'.format(i), }) data.update(kwargs) data.update({ 'slug': generate_slug(data['title']) }) tag = Tag(**data) parent.add_child(instance=tag) tag.save_revision().publish() tags.append(tag) return tags
def add_translation(request, page_id, locale): _page = get_object_or_404(Page, id=page_id) page = _page.specific if not issubclass(type(page), TranslatablePageMixinNotRoutable): messages.add_message( request, messages.INFO, _('That page is not translatable.')) return redirect(reverse('wagtailadmin_home')) # redirect to edit page if translation already exists for this locale translated_page = page.translated_pages.filter(language__locale=locale) if translated_page.exists(): return redirect( reverse('wagtailadmin_pages:edit', args=[ translated_page.first().id])) # create translation and redirect to edit page language = Languages.for_site(request.site).languages.filter( locale=locale).first() if not language: raise Http404 new_title = str(language) + " translation of %s" % page.title new_slug = generate_slug(new_title) translation = page.__class__( title=new_title, slug=new_slug, language=language) page.get_parent().add_child(instance=translation) translation.save_revision() # add the translation the new way page.specific.translated_pages.add(translation) page.save() translation.specific.translated_pages.add(page) translation.save() for translated_page in \ page.specific.translated_pages.all(): translations = page.specific.translated_pages.all().\ exclude(language__pk=translated_page.language.pk) for translation in translations: translated_page.translated_pages.add(translation) translated_page.save() # make sure new translation is in draft mode translation.unpublish() return redirect( reverse('wagtailadmin_pages:edit', args=[translation.id]))
def add_translation(request, page_id, locale): _page = get_object_or_404(Page, id=page_id) page = _page.specific if not issubclass(type(page), TranslatablePageMixinNotRoutable): messages.add_message(request, messages.INFO, _('That page is not translatable.')) return redirect(reverse('wagtailadmin_home')) # redirect to edit page if translation already exists for this locale translated_page = page.translated_pages.filter(language__locale=locale) if translated_page.exists(): return redirect( reverse('wagtailadmin_pages:edit', args=[translated_page.first().id])) # create translation and redirect to edit page language = Languages.for_site( request.site).languages.filter(locale=locale).first() if not language: raise Http404 new_title = str(language) + " translation of %s" % page.title new_slug = generate_slug(new_title) translation = page.__class__(title=new_title, slug=new_slug, language=language) page.get_parent().add_child(instance=translation) translation.save_revision() # add the translation the new way page.specific.translated_pages.add(translation) page.save() translation.specific.translated_pages.add(page) translation.save() for translated_page in \ page.specific.translated_pages.all(): translations = page.specific.translated_pages.all().\ exclude(language__pk=translated_page.language.pk) for translation in translations: translated_page.translated_pages.add(translation) translated_page.save() # make sure new translation is in draft mode translation.unpublish() return redirect(reverse('wagtailadmin_pages:edit', args=[translation.id]))
def mk_reaction_question(self, parent, article, **kwargs): data = {} data.update({ 'introduction': 'Test Question', }) data.update(kwargs) data.update({ 'slug': generate_slug(data['title']) }) form = MoloFormPage(**data) parent.add_child(instance=form) form.save_revision().publish() field = MoloFormField( choices='yes,maybe,no', success_message='well done') form.add_child(instance=field) field.save_revision().publish() ArticlePageForms.objects.create( reaction_question=form, page=article) return form
def mk_articles(self, parent, count=2, **kwargs): articles = [] for i in range(count): data = {} data.update({ 'title': 'Test page %s' % (i, ), 'subtitle': 'Sample page description for %s' % (i, ), 'body': json.dumps([{ 'type': 'paragraph', 'value': 'Sample page content for %s' % (i, )}]), }) data.update(kwargs) data.update({ 'slug': generate_slug(data['title']) }) article = ArticlePage(**data) if not article.first_published_at: article.first_published_at = timezone.now() parent.add_child(instance=article) article.save_revision().publish() articles.append(article) return articles
def mk_reaction_question(self, parent, article, **kwargs): data = {} data.update({ 'title': 'Test Question', }) data.update(kwargs) data.update({'slug': generate_slug(data['title'])}) question = ReactionQuestion(**data) parent.add_child(instance=question) question.save_revision().publish() choice1 = ReactionQuestionChoice(title='yes', success_message='well done') question.add_child(instance=choice1) choice1.save_revision().publish() choice2 = ReactionQuestionChoice(title='maybe') question.add_child(instance=choice2) choice2.save_revision().publish() choice3 = ReactionQuestionChoice(title='no') question.add_child(instance=choice3) choice3.save_revision().publish() ArticlePageReactionQuestions.objects.create(reaction_question=question, page=article) return question