import re from django.shortcuts import get_object_or_404 from django import template ELLIPSIZER = '...' REGEX_LINES = re.compile(r'(
)', re.U | re.S) REGEX_TAG = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S) IMG_ACTION_URL = '[{}]' register = template.Library() actions = [ { 'name': 'google', 'link': 'http://google.com/searchbyimage?image_url=%s', }, { 'name': 'iqdb', 'link': 'http://iqdb.org/?url=%s', }, ] @register.simple_tag(name='post_url') def post_url(*args, **kwargs): post_id = args[0] post = get_object_or_404('Post', id=post_id) return post.get_url() @register.simple_tag(name='image_actions') def image_actions(*args, **kwargs): image_link = args[0] if len(args) > 1: image_link = 'http://' + args[1] + image_link # TODO https? return ', '.join([IMG_ACTION_URL.format( action['link'] % image_link, action['name'])for action in actions]) @register.inclusion_tag('boards/post.html', name='post_view') def post_view(post, moderator=False, need_open_link=False, truncated=False, reply_link=False, **kwargs): """ Get post """ thread = post.get_thread() is_opening = post.is_opening() if is_opening: opening_post_id = post.id else: opening_post_id = thread.get_opening_post_id() css_class = 'post' if thread.archived: css_class += ' archive_post' elif not thread.can_bump(): css_class += ' dead_post' return { 'post': post, 'moderator': moderator, 'is_opening': is_opening, 'thread': thread, 'css_class': css_class, 'need_open_link': need_open_link, 'truncated': truncated, 'opening_post_id': opening_post_id, 'reply_link': reply_link, }