from django import template
import re
from itf.emailer.models import *

register = template.Library()

def insert_images_mailer(value, article_id):
    return insert_images(value, article_id, "mailer")

def insert_images_web(value, article_id):
    return insert_images(value, article_id, "web")

def insert_images(value, article_id, typ):
    regex = 'img\:(.*?)\r?\n'
    article = EmailerArticle.objects.get(id=article_id)
    matches = re.findall(regex, value)
    for m in matches:
        imgHTML = article.getImageHTML(m.strip(), typ)
        value = value.replace("img:" + m, imgHTML)
    return value

    

register.filter('insert_images_mailer', insert_images_mailer)
register.filter('insert_images_web', insert_images_web)
