def _humanize_datetime(value):
    """
    For date and time values shows how many seconds, minutes or hours ago
    compared to current timestamp returns representing string.
    """
    if not value:
        return ""

    s = '%d. %d. %d %d:%02d' % (value.day, value.month, value.year, value.hour, value.minute)
    return mark_safe('<span class="timeago" title="%s">%s</span>' % (
        s,
        unicode(_internal_humanize_datetime(value, django_settings.HUMANIZE_DATETIME_LIMIT))
    ))
def _humanize_datetime(value):
    """
    For date and time values shows how many seconds, minutes or hours ago
    compared to current timestamp returns representing string.
    """
    if not value:
        return ""

    s = '%d.&nbsp;%d.&nbsp;%d&nbsp;%d:%02d' % (
        value.day, value.month, value.year, value.hour, value.minute)
    return mark_safe(
        '<span class="timeago" title="%s">%s</span>' %
        (s,
         unicode(
             _internal_humanize_datetime(
                 value, django_settings.HUMANIZE_DATETIME_LIMIT))))
def parse_reply_to(html, posts_per_pages, answer, request):
    """
        @return: parse post html text and replace "reply to" meta symbol to regular link
        @param html, html content with replyto symbols
        @param posts_per_pages is dict with position answer (post) on paginator pages (key is post PK, value is page number)
    """
    patt = re.compile(r"replyto:#(?P<pk>\d+)")

    for match in patt.finditer(html):
        try:
            post = Post.objects.get(thread=answer.thread_id,
                                    deleted=False,
                                    pk=int(match.groupdict()["pk"]))

            page = posts_per_pages.get(post.pk)
            request_page = request.GET.get("page", 0)
            if isinstance(request_page, basestring) and request_page.isdigit():
                request_page = int(request_page)

            page_attr = "?page=%s" % page if (page and
                                              (page != request_page)) else ""

            datetime_str = unicode(
                _internal_humanize_datetime(
                    post.dt_created, django_settings.HUMANIZE_DATETIME_LIMIT))
            author = post.author.screen_name
            link_text = _(
                "Reply to the post by %(author)s &ndash; %(datetime_str)s") % {
                    "datetime_str": datetime_str,
                    "author": author
                }
            new_string = "<a href='%(anchor)s' class='js-post-preview' data-post_url='%(post_url)s' data-comment_pk='%(pk)s' title='%(datetime_str)s'>%(link_text)s</a>" % {
                "anchor": "%s#post-id-%s" % (page_attr, post.pk),
                "pk": post.pk,
                "post_url": reverse("discussion_answer", args=[post.pk]),
                "link_text": link_text,
                "datetime_str": datetime_str
            }
        except Post.DoesNotExist:
            new_string = _(
                "Quoted post was deleted or does not exist in this discussion."
            )
        new_string = u'<div class="replyto"><span>%s</span></div>' % new_string

        html = html.replace(match.group(), new_string)

    return html
def parse_reply_to(html, posts_per_pages, answer, request):
    """
        @return: parse post html text and replace "reply to" meta symbol to regular link
        @param html, html content with replyto symbols
        @param posts_per_pages is dict with position answer (post) on paginator pages (key is post PK, value is page number)
    """
    patt = re.compile(r"replyto:#(?P<pk>\d+)")

    for match in patt.finditer(html):
        try:
            post = Post.objects.get(thread=answer.thread_id, deleted=False, pk=int(match.groupdict()["pk"]))

            page = posts_per_pages.get(post.pk)
            request_page = request.GET.get("page", 0)
            if isinstance(request_page, basestring) and request_page.isdigit():
                request_page = int(request_page)

            page_attr = "?page=%s" % page if (page and (page != request_page)) else ""

            datetime_str = unicode(_internal_humanize_datetime(post.dt_created, django_settings.HUMANIZE_DATETIME_LIMIT))
            author = post.author.screen_name
            link_text = _("Reply to the post by %(author)s &ndash; %(datetime_str)s") % {"datetime_str": datetime_str, "author": author}
            new_string = "<a href='%(anchor)s' class='js-post-preview' data-post_url='%(post_url)s' data-comment_pk='%(pk)s' title='%(datetime_str)s'>%(link_text)s</a>" % {
                "anchor": "%s#post-id-%s" % (page_attr, post.pk),
                "pk": post.pk,
                "post_url": reverse("discussion_answer", args=[post.pk]),
                "link_text": link_text,
                "datetime_str": datetime_str
            }
        except Post.DoesNotExist:
            new_string = _("Quoted post was deleted or does not exist in this discussion.")
        new_string = u'<div class="replyto"><span>%s</span></div>' % new_string

        html = html.replace(match.group(), new_string)

    return html