app/soc/views/helper/templatetags/comments_helpers.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sat, 07 Mar 2009 15:19:53 +0000
changeset 1714 bfdef9380954
child 1897 634617545037
permissions -rw-r--r--
Moved some template tags out of forms_helpers into comments_helpers.py. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed

#!/usr/bin/python2.5
#
# Copyright 2009 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A Django template tag library containing Comments helpers.
"""

__authors__ = [
  '"Sverre Rabbelier" <srabbelier@gmail.com>',
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
  ]


from django import template

from soc.logic.models import user as user_logic
from soc.views.helper import redirects


register = template.Library()


@register.inclusion_tag('soc/templatetags/_as_comments.html',
                        takes_context=True)
def as_comments(context, work):
  """Returns a HTML representation of a work's comments.
  """

  context['comments'] =  work.comments
  return context

@register.inclusion_tag('soc/templatetags/_as_comment.html',
                        takes_context=True)
def as_comment(context, comment):
  """Returns a HTML representation of a comment.
  """

  edit_link = ''
  current_user = user_logic.logic.getForCurrentAccount()

  if current_user and comment.author.key() == current_user.key():
    params = {'url_name': context['comment_on_url_name']}
    edit_link = redirects.getEditRedirect(comment, params)

  context.update({
      'author': comment.author.name,
      'content': comment.content,
      'created': comment.created,
      'edit_link': edit_link,
      'modified_on': comment.modified,
      'modified_by': comment.modified_by.name if comment.modified_by else '',
      'comment_class': "public" if comment.is_public else "private",
      })

  return context

@register.inclusion_tag('soc/templatetags/_as_review.html',
                        takes_context=True)
def as_review(context, review):
  """Returns a HTML representation of a review.
  """

  # TODO(ljvderijk) once review editing is allowed redo this

  context.update({
      'author': review.author_name(),
      'content': review.content,
      'created': review.created,
      'score': review.score,
      'is_public': review.is_public,
      'comment_class': "public" if review.is_public else "private",
      })

  return context