def load_article_info(self, request): """get the tags in an article if the request is for an article page""" path = request.get_full_path() path_components = [ component for component in path.split('/') if component ] article_info = {} try: page, args, kwargs = request.site.root_page.specific.route( request, path_components) if issubclass(type(page.specific), ArticlePage): tags_str = "" main_lang = Languages.for_site(request.site).languages.filter( is_main_language=True).first() locale_code = get_locale_code( get_language_from_request(request)) if main_lang.locale == locale_code: article_info['cd5'] = page.specific.title else: article_info['cd5'] = page.specific.get_main_language_page( ).title qs = load_tags_for_article( { 'locale_code': 'en', 'request': request }, page) if qs: for q in qs: tags_str += "|" + q.title article_info.update({'cd6': tags_str[1:]}) return article_info except Http404: return article_info return article_info
def search(request, results_per_page=10): search_query = request.GET.get('q', None) page = request.GET.get('p', 1) locale = get_locale_code(get_language_from_request(request)) if search_query: results = ArticlePage.objects.filter( languages__language__locale=locale).live().search(search_query) Query.get(search_query).add_hit() else: results = ArticlePage.objects.none() paginator = Paginator(results, results_per_page) try: search_results = paginator.page(page) except PageNotAnInteger: search_results = paginator.page(1) except EmptyPage: search_results = paginator.page(paginator.num_pages) return render( request, 'search/search_results.html', { 'search_query': search_query, 'search_results': search_results, 'results': results, })
def poll_results(request, poll_id): question = get_object_or_404(Question, pk=poll_id) page = question.get_main_language_page() qs = Choice.objects.live().child_of(page).filter( languages__language__is_main_language=True) locale = get_locale_code(get_language_from_request(request)) choices = [(a.get_translation_for(locale) or a, a) for a in qs] total_votes = sum(c.votes for c in qs) choice_color = ['orange', 'purple', 'turq'] index = 0 for choice, main_choice in choices: vote_percentage = 0 if index >= len(choice_color): index = 0 if main_choice.votes > 0: vote_percentage = int(main_choice.votes * 100.0 / total_votes) choice.percentage = vote_percentage choice.color = choice_color[index] index += 1 context = { 'question': question, 'total': total_votes, 'choices': sorted( [c for c, m in choices], key=lambda x: x.percentage, reverse=True) } return render(request, 'polls/results.html', context,)
def serve(self, request): locale_code = get_locale_code(get_language_from_request(request)) translation = self.get_translation_for(locale_code) if translation: return redirect('%s?%s' % (translation.url, request.GET.urlencode())) return super(TranslatablePageMixin, self).serve(request)
def locale(request): locale_code = get_locale_code(get_language_from_request(request)) languages = Languages.for_site(request.site).languages.filter( is_active=True) return { 'locale_code': locale_code, 'languages': languages, 'selected_language': languages.filter(locale=locale_code).first()}
def locale(request): locale_code = get_locale_code(get_language_from_request(request)) languages = Languages.for_site( request.site).languages.filter(is_active=True) return { 'locale_code': locale_code, 'languages': languages, 'selected_language': languages.filter(locale=locale_code).first() }
def get_context_data(self, *args, **kwargs): persona_pages = PersonaPage.objects.live().filter( languages__language__is_main_language=True) context = super(PersonaView, self).get_context_data(*args, **kwargs) locale_code = get_locale_code(get_language_from_request(self.request)) context.update({ 'persona_pages': [ a.get_translation_for(locale_code, self.request.site) or a for a in persona_pages ], 'next': self.request.GET.get('next', '/')}) return context
def test_get_locale_code(self): self.assertEqual(utils.get_locale_code(), 'en') self.assertEqual(utils.get_locale_code('en-GB'), 'en-GB') self.assertEqual(utils.get_locale_code('en_GB'), 'en-GB') self.assertEqual(utils.get_locale_code('fr_FR'), 'fr-FR') self.assertEqual(utils.get_locale_code('zu-ZA'), 'zu-ZA') self.assertEqual(utils.get_locale_code('en'), 'en')
def get_context_data(self, *args, **kwargs): persona_pages = PersonaPage.objects.live().filter( languages__language__is_main_language=True) context = super(PersonaView, self).get_context_data(*args, **kwargs) locale_code = get_locale_code(get_language_from_request(self.request)) context.update({ 'persona_pages': [ a.get_translation_for(locale_code, self.request.site) or a for a in persona_pages ], 'next': self.request.GET.get('next', '/') }) return context
def search(request, results_per_page=10, load_more=False): search_query = request.GET.get('q', None) search_query = search_query.strip() if search_query else search_query page = request.GET.get('p', 1) locale = get_locale_code(get_language_from_request(request)) if search_query: main = request.site.root_page results = ArticlePage.objects.descendant_of(main).filter( language__locale=locale ).exact_type(ArticlePage).values_list('pk', flat=True) # Elasticsearch backend doesn't support filtering # on related fields, at the moment. # So we need to filter ArticlePage entries using DB, # then, we will be able to search results = ArticlePage.objects.filter(pk__in=results) results = results.live().search(search_query) # At the moment only ES backends have highlight API. if hasattr(results, 'highlight'): results = results.highlight( fields={ 'title': {}, 'subtitle': {}, 'body': {}, }, require_field_match=False ) Query.get(search_query).add_hit() else: results = ArticlePage.objects.none() if load_more: return results paginator = Paginator(results, results_per_page) try: search_results = paginator.page(page) except PageNotAnInteger: search_results = paginator.page(1) except EmptyPage: search_results = paginator.page(paginator.num_pages) return render(request, 'search/search_results.html', { 'search_query': search_query, 'search_results': search_results, 'results': results, })
def search(request, results_per_page=10, load_more=False): search_query = request.GET.get('q', None) search_query = search_query.strip() if search_query else search_query page = request.GET.get('p', 1) locale = get_locale_code(get_language_from_request(request)) if search_query: main = request.site.root_page results = ArticlePage.objects.descendant_of(main).filter( language__locale=locale).exact_type(ArticlePage).values_list( 'pk', flat=True) # Elasticsearch backend doesn't support filtering # on related fields, at the moment. # So we need to filter ArticlePage entries using DB, # then, we will be able to search results = ArticlePage.objects.filter(pk__in=results) results = results.live().search(search_query) # At the moment only ES backends have highlight API. if hasattr(results, 'highlight'): results = results.highlight(fields={ 'title': {}, 'subtitle': {}, 'body': {}, }, require_field_match=False) Query.get(search_query).add_hit() else: results = ArticlePage.objects.none() if load_more: return results paginator = Paginator(results, results_per_page) try: search_results = paginator.page(page) except PageNotAnInteger: search_results = paginator.page(1) except EmptyPage: search_results = paginator.page(paginator.num_pages) return render( request, 'search/search_results.html', { 'search_query': search_query, 'search_results': search_results, 'results': results, })