board.py
78 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
|
r1000 | import re | ||
|
r320 | from django.shortcuts import get_object_or_404 | ||
from django import template | ||||
|
r1000 | ELLIPSIZER = '...' | ||
REGEX_LINES = re.compile(r'(<div class="br"></div>)', re.U | re.S) | ||||
REGEX_TAG = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S) | ||||
|
r1027 | IMG_ACTION_URL = '[<a href="{}">{}</a>]' | ||
|
r692 | |||
|
r320 | register = template.Library() | ||
|
r460 | actions = [ | ||
{ | ||||
'name': 'google', | ||||
'link': 'http://google.com/searchbyimage?image_url=%s', | ||||
}, | ||||
{ | ||||
'name': 'iqdb', | ||||
'link': 'http://iqdb.org/?url=%s', | ||||
}, | ||||
] | ||||
|
r320 | |||
@register.simple_tag(name='post_url') | ||||
def post_url(*args, **kwargs): | ||||
post_id = args[0] | ||||
|
r692 | post = get_object_or_404('Post', id=post_id) | ||
|
r320 | |||
|
r621 | return post.get_url() | ||
|
r614 | |||
|
r460 | @register.simple_tag(name='image_actions') | ||
def image_actions(*args, **kwargs): | ||||
image_link = args[0] | ||||
if len(args) > 1: | ||||
|
r1027 | image_link = 'http://' + args[1] + image_link # TODO https? | ||
|
r460 | |||
|
r1027 | return ', '.join([IMG_ACTION_URL.format( | ||
action['link'] % image_link, action['name'])for action in actions]) | ||||
|
r537 | |||
@register.inclusion_tag('boards/post.html', name='post_view') | ||||
|
r594 | def post_view(post, moderator=False, need_open_link=False, truncated=False, | ||
|
r1056 | reply_link=False, **kwargs): | ||
|
r538 | """ | ||
Get post | ||||
""" | ||||
|
r988 | thread = post.get_thread() | ||
|
r1032 | is_opening = post.is_opening() | ||
|
r583 | |||
|
r1032 | if is_opening: | ||
opening_post_id = post.id | ||||
|
r949 | else: | ||
|
r1032 | opening_post_id = thread.get_opening_post_id() | ||
|
r614 | |||
|
r1093 | css_class = 'post' | ||
if thread.archived: | ||||
css_class += ' archive_post' | ||||
elif not thread.can_bump(): | ||||
css_class += ' dead_post' | ||||
|
r537 | return { | ||
'post': post, | ||||
|
r540 | 'moderator': moderator, | ||
|
r583 | 'is_opening': is_opening, | ||
'thread': thread, | ||||
|
r1093 | 'css_class': css_class, | ||
|
r594 | 'need_open_link': need_open_link, | ||
'truncated': truncated, | ||||
|
r614 | 'opening_post_id': opening_post_id, | ||
|
r1056 | 'reply_link': reply_link, | ||
|
r614 | } | ||
|
r1000 | |||