Beispiel #1
0
def index(request):
    """
    Função que gera a lista de posts do blog.
    Gerar a paginação de acordo com o número definido na variável "number_of_posts".

    :return:        Retorna os posts do blog
    """
    next_lectures = Blog.objects.filter(
        translations__language_code__in=get_active_language_choices(),
        translations__date_time__gt=datetime.datetime.now()
    ).order_by('translations__date_time')

    next_lectures_ids = next_lectures.values_list('pk', flat=True)

    blog_list = Blog.objects.active_translations().exclude(pk__in=next_lectures_ids)
    number_of_posts = 4
    paginator = Paginator(blog_list, number_of_posts)
    page = request.GET.get('page')

    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # Se a página não é um inteiro, mostrar a primeira página.
        posts = paginator.page(1)
    except EmptyPage:
        # Se a página está fora do intervalo (por exemplo 9999), mostrar a última página de resultados.
        posts = paginator.page(paginator.num_pages)

    context = {'posts': posts, 'next_lectures': next_lectures}

    return render(request, 'blog/blog.html', context)
Beispiel #2
0
    def active_translations(self, language_code=None):
        """
        Only return objects which are translated, or have a fallback that should be displayed.

        Typically that's the currently active language and fallback language.
        When ``hide_untranslated = True``, only the currently active language will be returned.
        """
        # Default:     (language, fallback) when hide_translated == False
        # Alternative: (language,)          when hide_untranslated == True
        language_codes = get_active_language_choices(language_code)
        return self.translated(*language_codes)
Beispiel #3
0
    def active_translations(self, language_code=None):
        """
        Only return objects which are translated, or have a fallback that should be displayed.

        Typically that's the currently active language and fallback language.
        When ``hide_untranslated = True``, only the currently active language will be returned.
        """
        # Default:     (language, fallback) when hide_translated == False
        # Alternative: (language,)          when hide_untranslated == True
        language_codes = get_active_language_choices(language_code)
        return self.translated(*language_codes)
Beispiel #4
0
 def get_queryset(self):
     try:
         langs = get_active_language_choices()
         lang1 = langs[0]
         lang2 = langs[1] if len(langs) > 1 else "nonexistent"
     except ValueError:
         lang1 = settings.LANGUAGE_CODE
         try:
             lang2 = settings.LANGUAGES[1][0]
         except IndexError:
             lang2 = "nonexistent"
     return (super().get_queryset().annotate(translation1=FilteredRelation(
         "translations", condition=Q(translations__language_code=lang1)
     )).annotate(translation2=FilteredRelation(
         "translations", condition=Q(
             translations__language_code=lang2))).annotate(
                 descr=Coalesce("translation1__descr",
                                "translation2__descr")).order_by("descr"))
Beispiel #5
0
 def get_queryset(self):
     langs = get_active_language_choices()
     lang1 = langs[0]
     lang2 = langs[1] if len(langs) > 1 else "nonexistent"
     return (
         super()
         .get_queryset()
         .annotate(
             translation1=FilteredRelation(
                 "translations", condition=Q(translations__language_code=lang1)
             )
         )
         .annotate(
             translation2=FilteredRelation(
                 "translations", condition=Q(translations__language_code=lang2)
             )
         )
         .annotate(descr=Coalesce("translation1__descr", "translation2__descr"))
         .order_by("descr")
     )
Beispiel #6
0
 def get_language_choices(self):
     """
     Define the language choices for the view, defaults to the defined
     settings.
     """
     return get_active_language_choices(self.current_language)
Beispiel #7
0
 def get_language_choices(self):
     """
     Define the language choices for the view, defaults to the defined settings.
     """
     return get_active_language_choices(self.get_language())
 def test_language_codes(self):
     codes = ['en', 'es']
     active_codes = get_active_language_choices()
     for code in codes:
         self.assertTrue(code in active_codes)