from django import template
from copy import copy
from molo.yourwords.models import (YourWordsCompetition, ThankYou,
                                   YourWordsCompetitionIndexPage)
from molo.core.templatetags.core_tags import get_pages

register = template.Library()


@register.inclusion_tag(
    'yourwords/your_words_competition_tag.html',
    takes_context=True
)
def your_words_competition(context):
    context = copy(context)
    locale_code = context.get('locale_code')
    page = YourWordsCompetitionIndexPage.objects.live().all().first()
    if page:
        competitions = (
            YourWordsCompetition.objects.child_of(page).filter(
                languages__language__is_main_language=True).specific())
    else:
        competitions = []

    context.update({
        'competitions': get_pages(context, competitions, locale_code)
    })
    return context


@register.assignment_tag(takes_context=True)
def load_thank_you_page_for_competition(context, competition):

    page = competition.get_main_language_page()
    locale = context.get('locale_code')

    qs = ThankYou.objects.child_of(page).filter(
        languages__language__is_main_language=True)

    if not locale:
        return qs

    if qs:
        return get_pages(context, qs, locale)
    else:
        return []

from django import template
from copy import copy
from molo.yourwords.models import (YourWordsCompetition, ThankYou,
                                   YourWordsCompetitionIndexPage)
from molo.core.templatetags.core_tags import get_pages

register = template.Library()


@register.inclusion_tag(
    'yourwords/your_words_competition_tag.html',
    takes_context=True
)
def your_words_competition(context):
    context = copy(context)
    locale_code = context.get('locale_code')
    page = YourWordsCompetitionIndexPage.objects.live().all().first()
    if page:
        competitions = (
            YourWordsCompetition.objects.child_of(page).filter(
                languages__language__is_main_language=True).specific())
    else:
        competitions = YourWordsCompetition.objects.none()

    context.update({
        'competitions': get_pages(context, competitions, locale_code)
    })
    return context


@register.assignment_tag(takes_context=True)
def load_thank_you_page_for_competition(context, competition):

    page = competition.get_main_language_page()
    locale = context.get('locale_code')

    qs = ThankYou.objects.child_of(page).filter(
        languages__language__is_main_language=True)

    if not locale:
        return qs

    if qs:
        return get_pages(context, qs, locale)
    else:
        return []

Return None if there is no competition
Return None if there is no competition

  from django import template
  from copy import copy
  from molo.yourwords.models import (YourWordsCompetition, ThankYou,
                                     YourWordsCompetitionIndexPage)
  from molo.core.templatetags.core_tags import get_pages
  
  register = template.Library()
  
  
  @register.inclusion_tag(
      'yourwords/your_words_competition_tag.html',
      takes_context=True
  )
  def your_words_competition(context):
      context = copy(context)
      locale_code = context.get('locale_code')
      page = YourWordsCompetitionIndexPage.objects.live().all().first()
      if page:
          competitions = (
              YourWordsCompetition.objects.child_of(page).filter(
                  languages__language__is_main_language=True).specific())
      else:
-         competitions = []
+         competitions = YourWordsCompetition.objects.none()
  
      context.update({
          'competitions': get_pages(context, competitions, locale_code)
      })
      return context
  
  
  @register.assignment_tag(takes_context=True)
  def load_thank_you_page_for_competition(context, competition):
  
      page = competition.get_main_language_page()
      locale = context.get('locale_code')
  
      qs = ThankYou.objects.child_of(page).filter(
          languages__language__is_main_language=True)
  
      if not locale:
          return qs
  
      if qs:
          return get_pages(context, qs, locale)
      else:
          return []
  
Return None if there is no competition
## Code Before:
from django import template
from copy import copy
from molo.yourwords.models import (YourWordsCompetition, ThankYou,
                                   YourWordsCompetitionIndexPage)
from molo.core.templatetags.core_tags import get_pages

register = template.Library()


@register.inclusion_tag(
    'yourwords/your_words_competition_tag.html',
    takes_context=True
)
def your_words_competition(context):
    context = copy(context)
    locale_code = context.get('locale_code')
    page = YourWordsCompetitionIndexPage.objects.live().all().first()
    if page:
        competitions = (
            YourWordsCompetition.objects.child_of(page).filter(
                languages__language__is_main_language=True).specific())
    else:
        competitions = []

    context.update({
        'competitions': get_pages(context, competitions, locale_code)
    })
    return context


@register.assignment_tag(takes_context=True)
def load_thank_you_page_for_competition(context, competition):

    page = competition.get_main_language_page()
    locale = context.get('locale_code')

    qs = ThankYou.objects.child_of(page).filter(
        languages__language__is_main_language=True)

    if not locale:
        return qs

    if qs:
        return get_pages(context, qs, locale)
    else:
        return []

## Instruction:
Return None if there is no competition
## Code After:
from django import template
from copy import copy
from molo.yourwords.models import (YourWordsCompetition, ThankYou,
                                   YourWordsCompetitionIndexPage)
from molo.core.templatetags.core_tags import get_pages

register = template.Library()


@register.inclusion_tag(
    'yourwords/your_words_competition_tag.html',
    takes_context=True
)
def your_words_competition(context):
    context = copy(context)
    locale_code = context.get('locale_code')
    page = YourWordsCompetitionIndexPage.objects.live().all().first()
    if page:
        competitions = (
            YourWordsCompetition.objects.child_of(page).filter(
                languages__language__is_main_language=True).specific())
    else:
        competitions = YourWordsCompetition.objects.none()

    context.update({
        'competitions': get_pages(context, competitions, locale_code)
    })
    return context


@register.assignment_tag(takes_context=True)
def load_thank_you_page_for_competition(context, competition):

    page = competition.get_main_language_page()
    locale = context.get('locale_code')

    qs = ThankYou.objects.child_of(page).filter(
        languages__language__is_main_language=True)

    if not locale:
        return qs

    if qs:
        return get_pages(context, qs, locale)
    else:
        return []
