# *-* coding=utf-8 *-*
from django import template
from django.conf import settings
from django.utils.html import format_html, strip_tags
from django.utils.text import capfirst

from blogoland.confs import DEFAULT_DATE_FORMAT
from blogoland.models import Post

register = template.Library()

DATE_FORMAT = getattr(settings, 'BLOGOLAND_DATE_FORMAT', DEFAULT_DATE_FORMAT)

# Helper function
def build_img_tag(img):
    """
    Build a HTML Img TAG with the given img object.
    """
    img_tag = '<img src="{0}" alt="{1}" />'.format(img.image.url, img.title)
    return mark_safe(img_tag)


@register.simple_tag(takes_context=True)
def post_title(context):    
    """
    Returns the title with capfirst filter applied. If post isn't public
    this tag will append a '[DRAFT]' string and output the final title.
    """
    post = context['object']
    title = capfirst(post.title)

    if not post.is_public():
        title = '[DRAFT] %s' % title 

    return title
    
@register.simple_tag(takes_context=True)
def post_date(context):
    """
    Returns the formated date. 
    Default: '%d-%m-%Y'
    """
    post = context['object']
    return post.publication_date.strftime(DATE_FORMAT)

@register.simple_tag(takes_context=True)
def post_content(context):
    """
    Check if the given instance wrapped in the context is a Post one. Then 
    parse the content of the post to HTML.
    """
    try:
        post = context['object']
        if isinstance(post, Post):
            return format_html(post.content)
    except: 
        return ''


@register.simple_tag(takes_context=True)
def detail_image(context):
    """
    Render the first detail image of the Post
    """
    try:
        obj = context['object']
        img = obj.image_set.filter(img_type='detail').first()
        img_tag = build_img_tag(img)
        return img_tag
    except:
        return

@register.simple_tag(takes_context=True)
def thumbnail_image(context):
    """
    Render the first thumbnail image of the Post
    """
    try:
        obj = context['object']
        img = obj.image_set.filter(img_type='thumbnail').first()
        img_tag = build_img_tag(img)
        return img_tag
    except:
        return

@register.simple_tag(takes_context=True)
def get_gallery_images(context):
    """
    Returns a list of img related objects selected as 'gallery'
    """
    try:
        obj = context['object']
        return obj.image_set.filter(img_type='gallery')
    except:
        return []
@@ -119,16 +119,27 @@
 at_html,
+ mark_safe,
  strip_t
@@ -142,16 +142,16 @@
 ip_tags%0A
-
 from dja
@@ -481,24 +481,25 @@
 ct.%0A    %22%22%22%0A
+%0A
     img_tag 
@@ -594,17 +594,16 @@
 g_tag)%0A%0A
-%0A
 @registe
@@ -1557,17 +1557,16 @@
 urn ''%0A%0A
-%0A
 @registe
@@ -1602,16 +1602,21 @@
 ue)%0Adef 
+post_
 detail_i
@@ -1701,35 +1701,36 @@
    try:%0A        
-obj
+post
  = context%5B'obje
@@ -1740,35 +1740,36 @@
 %5D%0A        img = 
-obj
+post
 .image_set.filte
@@ -1930,16 +1930,21 @@
 ue)%0Adef 
+post_
 thumbnai
@@ -2035,35 +2035,36 @@
    try:%0A        
-obj
+post
  = context%5B'obje
@@ -2082,19 +2082,20 @@
   img = 
-obj
+post
 .image_s
@@ -2271,16 +2271,21 @@
 def get_
+post_
 gallery_
@@ -2398,19 +2398,20 @@
         
-obj
+post
  = conte
@@ -2438,19 +2438,20 @@
  return 
-obj
+post
 .image_s

fix import and name error