def test_attach_translated_content(self): # create 2 languages self.english = SiteLanguageRelation.objects.create( language_setting=self.importer.language_setting, locale='en', is_active=True) self.french = SiteLanguageRelation.objects.create( language_setting=self.importer.language_setting, locale='fr', is_active=True) content = constants.ARTICLE_PAGE_RESPONSE_MAIN_LANG content_for_translated = constants.ARTICLE_PAGE_RESPONSE_FRENCH content_copy = dict(content) content_for_translated_copy = dict(content_for_translated) index = self.section_index parent = self.mk_section(index) self.assertEqual(ArticlePage.objects.count(), 0) article = self.importer.attach_page(parent, content_copy) self.assertEqual(ArticlePage.objects.count(), 1) translated_article = self.importer.attach_translated_content( article, content_for_translated_copy, "fr") self.assertEqual(ArticlePage.objects.count(), 2) site = Site.objects.get(pk=self.importer.site_pk) self.assertEqual(get_translation_for( [article], "fr", site)[0], translated_article)
def test_create_section_page_translated(self): # create 2 languages self.english = SiteLanguageRelation.objects.create( language_setting=self.importer.language_setting, locale='en', is_active=True) self.french = SiteLanguageRelation.objects.create( language_setting=self.importer.language_setting, locale='fr', is_active=True) content = constants.SECTION_PAGE_RESPONSE content_for_translated = constants.SECTION_PAGE_RESPONSE_FRENCH content_copy = dict(content) content_for_translated_copy = dict(content_for_translated) parent = self.section_index self.assertEqual(SectionPage.objects.count(), 0) section = self.importer.attach_page(parent, content_copy) self.assertEqual(SectionPage.objects.count(), 1) translated_section = self.importer.attach_translated_content( section, content_for_translated_copy, "fr") self.assertEqual(SectionPage.objects.count(), 2) site = Site.objects.get(pk=self.importer.site_pk) self.assertEqual(get_translation_for( [section], "fr", site)[0], translated_section)
def get_pages(context, queryset, locale): from molo.core.models import get_translation_for if queryset.count() == 0: return [] request = context['request'] if not hasattr(request, 'site'): return list[queryset] language = get_language(request.site, locale) if language and language.is_main_language: return list(queryset.live()) pages = get_translation_for(queryset, locale, request.site) return pages or []
def get_next_tag(context, tag): request = context['request'] locale_code = context.get('locale_code') current_tag = tag.get_main_language_page() qs = Tag.objects.descendant_of( request.site.root_page).filter(language__is_main_language=True).live() if qs.exists(): tags = list(qs) if not len(tags) == tags.index(current_tag) + 1: next_tag = tags[tags.index(current_tag) + 1] else: next_tag = tags[0] next_tag_translated = get_translation_for([next_tag], locale_code, context['request'].site) if next_tag_translated: return next_tag_translated[0] else: return next_tag
def get_next_tag(context, tag): request = context['request'] locale_code = context.get('locale_code') current_tag = tag.get_main_language_page() qs = Tag.objects.descendant_of(request.site.root_page).filter( language__is_main_language=True).live() if qs.exists(): tags = list(qs) if not (len(tags) == tags.index(current_tag) + 1): next_tag = tags[tags.index(current_tag) + 1] else: next_tag = tags[0] next_tag_translated = get_translation_for( [next_tag], locale_code, context['request'].site) if next_tag_translated: return next_tag_translated[0] else: return next_tag
def serve_questions(self, request): """ Implements a simple multi-step form. Stores each step in the session. When the last step is submitted correctly, the whole form is saved in the DB. """ context = self.get_context(request) # this will only return a page if there is a translation page = get_translation_for([context['page']], locale=request.LANGUAGE_CODE, site=request.site) if page: page = page[0] if not page.language.is_main_language: # if there is a translation, redirect to the translated page return redirect(page.url) form_data = self.load_data(request) paginator = SkipLogicPaginator( self.get_form_fields(), request.POST, form_data, ) is_last_step = False step_number = request.GET.get('p', 1) try: step = paginator.page(step_number) except PageNotAnInteger: step = paginator.page(1) except EmptyPage: step = paginator.page(paginator.num_pages) is_last_step = True if request.method == 'POST': # The first step will be submitted with step_number == 2, # so we need to get a from from previous step # Edge case - submission of the last step prev_step = step if is_last_step else paginator.page( int(step_number) - 1) # Create a form only for submitted step prev_form_class = self.get_form_class_for_step(prev_step) prev_form = prev_form_class( paginator.new_answers, page=self, user=request.user, ) if prev_form.is_valid(): # If data for step is valid, update the session form_data.update(prev_form.cleaned_data) self.save_data(request, form_data) if prev_step.has_next(): # Create a new form for a following step, if the following # step is present form_class = self.get_form_class_for_step(step) form = form_class(page=self, user=request.user) else: # If there is no more steps, create form for all fields data = self.load_data(request) form = self.get_form(data, page=self, user=request.user, prevent_required=True) if form.is_valid(): # Perform validation again for whole form. # After successful validation, save data into DB, # and remove from the session. self.set_form_as_submitted_for_session(request) # We fill in the missing fields which were skipped with # a default value for question in self.get_form_fields(): if question.clean_name not in data: form.cleaned_data[question.clean_name] = SKIP self.process_form_submission(form) del request.session[self.session_key_data] return prev_step.success(self.slug) else: # If data for step is invalid # we will need to display form again with errors, # so restore previous state. step = prev_step form = prev_form else: # Create empty form for non-POST requests form_class = self.get_form_class_for_step(step) form = form_class(page=self, user=request.user) context['form'] = form context['fields_step'] = step context['is_intermediate_step'] = step.possibly_has_next() return render(request, self.template, context)
def serve_questions(self, request): """ Implements a simple multi-step form. Stores each step in the session. When the last step is submitted correctly, the whole form is saved in the DB. """ context = self.get_context(request) # this will only return a page if there is a translation page = get_translation_for( [context['page']], locale=request.LANGUAGE_CODE, site=request.site) if page: page = page[0] if not page.language.is_main_language: # if there is a translation, redirect to the translated page return redirect(page.url) survey_data = self.load_data(request) paginator = SkipLogicPaginator( self.get_form_fields(), request.POST, survey_data, ) is_last_step = False step_number = request.GET.get('p', 1) try: step = paginator.page(step_number) except PageNotAnInteger: step = paginator.page(1) except EmptyPage: step = paginator.page(paginator.num_pages) is_last_step = True if request.method == 'POST': # The first step will be submitted with step_number == 2, # so we need to get a from from previous step # Edge case - submission of the last step prev_step = step if is_last_step else paginator.page( int(step_number) - 1) # Create a form only for submitted step prev_form_class = self.get_form_class_for_step(prev_step) prev_form = prev_form_class( paginator.new_answers, page=self, user=request.user, ) if prev_form.is_valid(): # If data for step is valid, update the session survey_data.update(prev_form.cleaned_data) self.save_data(request, survey_data) if prev_step.has_next(): # Create a new form for a following step, if the following # step is present form_class = self.get_form_class_for_step(step) form = form_class(page=self, user=request.user) else: # If there is no more steps, create form for all fields data = self.load_data(request) form = self.get_form( data, page=self, user=request.user, prevent_required=True ) if form.is_valid(): # Perform validation again for whole form. # After successful validation, save data into DB, # and remove from the session. self.set_survey_as_submitted_for_session(request) # We fill in the missing fields which were skipped with # a default value for question in self.get_form_fields(): if question.clean_name not in data: form.cleaned_data[question.clean_name] = SKIP self.process_form_submission(form) del request.session[self.session_key_data] return prev_step.success(self.slug) else: # If data for step is invalid # we will need to display form again with errors, # so restore previous state. step = prev_step form = prev_form else: # Create empty form for non-POST requests form_class = self.get_form_class_for_step(step) form = form_class(page=self, user=request.user) context['form'] = form context['fields_step'] = step context['is_intermediate_step'] = step.possibly_has_next() return render( request, self.template, context )