app/soc/views/helper/templatetags/comments_helpers.py
changeset 1714 bfdef9380954
child 1897 634617545037
equal deleted inserted replaced
1713:e39ae44d0298 1714:bfdef9380954
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """A Django template tag library containing Comments helpers.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <srabbelier@gmail.com>',
       
    22   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    23   ]
       
    24 
       
    25 
       
    26 from django import template
       
    27 
       
    28 from soc.logic.models import user as user_logic
       
    29 from soc.views.helper import redirects
       
    30 
       
    31 
       
    32 register = template.Library()
       
    33 
       
    34 
       
    35 @register.inclusion_tag('soc/templatetags/_as_comments.html',
       
    36                         takes_context=True)
       
    37 def as_comments(context, work):
       
    38   """Returns a HTML representation of a work's comments.
       
    39   """
       
    40 
       
    41   context['comments'] =  work.comments
       
    42   return context
       
    43 
       
    44 @register.inclusion_tag('soc/templatetags/_as_comment.html',
       
    45                         takes_context=True)
       
    46 def as_comment(context, comment):
       
    47   """Returns a HTML representation of a comment.
       
    48   """
       
    49 
       
    50   edit_link = ''
       
    51   current_user = user_logic.logic.getForCurrentAccount()
       
    52 
       
    53   if current_user and comment.author.key() == current_user.key():
       
    54     params = {'url_name': context['comment_on_url_name']}
       
    55     edit_link = redirects.getEditRedirect(comment, params)
       
    56 
       
    57   context.update({
       
    58       'author': comment.author.name,
       
    59       'content': comment.content,
       
    60       'created': comment.created,
       
    61       'edit_link': edit_link,
       
    62       'modified_on': comment.modified,
       
    63       'modified_by': comment.modified_by.name if comment.modified_by else '',
       
    64       'comment_class': "public" if comment.is_public else "private",
       
    65       })
       
    66 
       
    67   return context
       
    68 
       
    69 @register.inclusion_tag('soc/templatetags/_as_review.html',
       
    70                         takes_context=True)
       
    71 def as_review(context, review):
       
    72   """Returns a HTML representation of a review.
       
    73   """
       
    74 
       
    75   # TODO(ljvderijk) once review editing is allowed redo this
       
    76 
       
    77   context.update({
       
    78       'author': review.author_name(),
       
    79       'content': review.content,
       
    80       'created': review.created,
       
    81       'score': review.score,
       
    82       'is_public': review.is_public,
       
    83       'comment_class': "public" if review.is_public else "private",
       
    84       })
       
    85 
       
    86   return context
       
    87