from django.template import Library
from django.utils.translation import get_language

from simpleseo import settings
from simpleseo.models import SeoMetadata

register = Library()


@register.filter
def single_quotes(description):
    return description.replace('\"', '\'')


@register.inclusion_tag('simpleseo/metadata.html', takes_context=True)
def get_seo(context):
    lang_code = get_language()[:2]
    path = context['request'].path
    try:
        metadata = SeoMetadata.objects.get(path=path, lang_code=lang_code)
    except SeoMetadata.DoesNotExist:
        metadata = None
    if metadata is None:
        return {'title': settings.FALLBACK_TITLE,
                'description': settings.FALLBACK_DESCRIPTION}
    return {'title': metadata.title, 'description': metadata.description}


@register.simple_tag(takes_context=True)
def get_seo_title(context):
    return get_seo(context)['title']


@register.simple_tag(takes_context=True)
def get_seo_description(context):
    return get_seo(context)['description']

from django.forms.models import model_to_dict
from django.template import Library
from django.utils.translation import get_language

from simpleseo import settings
from simpleseo.models import SeoMetadata

register = Library()


@register.filter
def single_quotes(description):
    return description.replace('\"', '\'')


@register.inclusion_tag('simpleseo/metadata.html', takes_context=True)
def get_seo(context, **kwargs):
    path = context['request'].path
    lang_code = get_language()[:2]
    try:
        metadata = model_to_dict(SeoMetadata.objects.get(path=path,
                                                         lang_code=lang_code))
    except SeoMetadata.DoesNotExist:
        metadata = {}
    result = {}
    for item in ['title', 'description']:
        result[item] = (metadata.get(item) or
                        kwargs.get(item) or
                        getattr(settings, 'FALLBACK_{0}'.format(item.upper())))
    return result


@register.simple_tag(takes_context=True)
def get_seo_title(context, default=''):
    return get_seo(context, title=default).get('title')


@register.simple_tag(takes_context=True)
def get_seo_description(context, default=''):
    return get_seo(context, description=default).get('description')

Allow to set default value in template
Allow to set default value in template

+ from django.forms.models import model_to_dict
  from django.template import Library
  from django.utils.translation import get_language
  
  from simpleseo import settings
  from simpleseo.models import SeoMetadata
  
  register = Library()
  
  
  @register.filter
  def single_quotes(description):
      return description.replace('\"', '\'')
  
  
  @register.inclusion_tag('simpleseo/metadata.html', takes_context=True)
- def get_seo(context):
+ def get_seo(context, **kwargs):
+     path = context['request'].path
      lang_code = get_language()[:2]
-     path = context['request'].path
      try:
-         metadata = SeoMetadata.objects.get(path=path, lang_code=lang_code)
+         metadata = model_to_dict(SeoMetadata.objects.get(path=path,
+                                                          lang_code=lang_code))
      except SeoMetadata.DoesNotExist:
-         metadata = None
+         metadata = {}
-     if metadata is None:
-         return {'title': settings.FALLBACK_TITLE,
-                 'description': settings.FALLBACK_DESCRIPTION}
-     return {'title': metadata.title, 'description': metadata.description}
+     result = {}
+     for item in ['title', 'description']:
+         result[item] = (metadata.get(item) or
+                         kwargs.get(item) or
+                         getattr(settings, 'FALLBACK_{0}'.format(item.upper())))
+     return result
  
  
  @register.simple_tag(takes_context=True)
- def get_seo_title(context):
+ def get_seo_title(context, default=''):
-     return get_seo(context)['title']
+     return get_seo(context, title=default).get('title')
  
  
  @register.simple_tag(takes_context=True)
- def get_seo_description(context):
+ def get_seo_description(context, default=''):
-     return get_seo(context)['description']
+     return get_seo(context, description=default).get('description')
  
Allow to set default value in template
## Code Before:
from django.template import Library
from django.utils.translation import get_language

from simpleseo import settings
from simpleseo.models import SeoMetadata

register = Library()


@register.filter
def single_quotes(description):
    return description.replace('\"', '\'')


@register.inclusion_tag('simpleseo/metadata.html', takes_context=True)
def get_seo(context):
    lang_code = get_language()[:2]
    path = context['request'].path
    try:
        metadata = SeoMetadata.objects.get(path=path, lang_code=lang_code)
    except SeoMetadata.DoesNotExist:
        metadata = None
    if metadata is None:
        return {'title': settings.FALLBACK_TITLE,
                'description': settings.FALLBACK_DESCRIPTION}
    return {'title': metadata.title, 'description': metadata.description}


@register.simple_tag(takes_context=True)
def get_seo_title(context):
    return get_seo(context)['title']


@register.simple_tag(takes_context=True)
def get_seo_description(context):
    return get_seo(context)['description']

## Instruction:
Allow to set default value in template
## Code After:
from django.forms.models import model_to_dict
from django.template import Library
from django.utils.translation import get_language

from simpleseo import settings
from simpleseo.models import SeoMetadata

register = Library()


@register.filter
def single_quotes(description):
    return description.replace('\"', '\'')


@register.inclusion_tag('simpleseo/metadata.html', takes_context=True)
def get_seo(context, **kwargs):
    path = context['request'].path
    lang_code = get_language()[:2]
    try:
        metadata = model_to_dict(SeoMetadata.objects.get(path=path,
                                                         lang_code=lang_code))
    except SeoMetadata.DoesNotExist:
        metadata = {}
    result = {}
    for item in ['title', 'description']:
        result[item] = (metadata.get(item) or
                        kwargs.get(item) or
                        getattr(settings, 'FALLBACK_{0}'.format(item.upper())))
    return result


@register.simple_tag(takes_context=True)
def get_seo_title(context, default=''):
    return get_seo(context, title=default).get('title')


@register.simple_tag(takes_context=True)
def get_seo_description(context, default=''):
    return get_seo(context, description=default).get('description')
