app/soc/views/models/comment.py
changeset 1678 80411f57f31a
child 1687 8203c805edc7
equal deleted inserted replaced
1677:b2cf6ad50a2a 1678:80411f57f31a
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 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 """Views for comments.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22     '"Matthew Wilkes" <matthew@matthewwilkes.co.uk>',
       
    23   ]
       
    24 
       
    25 import time
       
    26 
       
    27 from google.appengine.api import users
       
    28 from google.appengine.ext.db import Key
       
    29 
       
    30 from django import forms
       
    31 
       
    32 from soc.logic import cleaning
       
    33 from soc.logic import dicts
       
    34 from soc.logic import validate
       
    35 from soc.logic.models.user import logic as user_logic
       
    36 from soc.logic.models.comment import logic as comment_logic
       
    37 from soc.logic.models.document import logic as document_logic
       
    38 from soc.logic.models.linkable import logic as link_logic
       
    39 from soc.models import linkable
       
    40 from soc.views import helper
       
    41 from soc.views.helper import access
       
    42 from soc.views.helper import redirects
       
    43 from soc.views.helper import params as params_helper
       
    44 from soc.views.models import base
       
    45 
       
    46 import soc.models.comment
       
    47 import soc.logic.models.comment
       
    48 import soc.logic.dicts
       
    49 import soc.views.helper
       
    50 import soc.views.helper.widgets
       
    51 
       
    52 
       
    53 class View(base.View):
       
    54   """View methods for the comment model.
       
    55   """
       
    56 
       
    57   def __init__(self, params=None):
       
    58     """Defines the fields and methods required for the base View class
       
    59     to provide the user with list, public, create, edit and delete views.
       
    60 
       
    61     Params:
       
    62       params: a dict with params for this View
       
    63       comment_on_name: e.g. 'Document'
       
    64       comment_on_url_name: e.g. 'document'
       
    65     """
       
    66 
       
    67     rights = access.Checker(params)
       
    68     rights['create'] = [('checkSeeded', ['checkIsDocumentReadable','scope_path'])]
       
    69     rights['edit'] = [('checkIsMyEntity', [comment_logic,'author', True])]
       
    70     rights['delete'] = [('checkIsMyEntity', [comment_logic,'author', True])]
       
    71 
       
    72     new_params = {}
       
    73     new_params['logic'] = comment_logic
       
    74     new_params['rights'] = rights
       
    75 
       
    76     new_params['name'] = "Comment"
       
    77 
       
    78     new_params['create_template'] = 'soc/comment/edit.html'
       
    79     new_params['edit_template'] = 'soc/comment/edit.html'
       
    80 
       
    81     new_params['no_show'] = True
       
    82     new_params['no_admin'] = True
       
    83     new_params['no_create_raw'] = True
       
    84     new_params['no_create_with_key_fields'] = True
       
    85     new_params['no_list_raw'] = True
       
    86 
       
    87     new_params['create_extra_dynaproperties'] = {
       
    88         'on': forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
       
    89                                              required=False),
       
    90         'content': forms.fields.CharField(
       
    91             widget=helper.widgets.TinyMCE(attrs={'rows':10, 'cols':40})),
       
    92         'scope_path': forms.CharField(widget=forms.HiddenInput, required=True),
       
    93         }
       
    94     new_params['extra_dynaexclude'] = ['author', 'commented', 'link_id', 'modified_by']
       
    95 
       
    96     new_params['edit_extra_dynaproperties'] = {
       
    97         'link_id': forms.CharField(widget=forms.HiddenInput, required=True),
       
    98         'created_by': forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
       
    99                                              required=False),
       
   100         }
       
   101 
       
   102     params = dicts.merge(params, new_params)
       
   103     super(View, self).__init__(params=params)
       
   104 
       
   105   def _editContext(self, request, context):
       
   106     """see base.View._editContext.
       
   107     """
       
   108 
       
   109     entity = context['entity']
       
   110 
       
   111     if entity:
       
   112       on = entity.commented
       
   113     else:
       
   114       seed = context['seed']
       
   115       on =  seed['commented']
       
   116 
       
   117     params = {'url_name': self._params['comment_on_url_name']}
       
   118     redirect = redirects.getPublicRedirect(on, params)
       
   119 
       
   120     context['comment_on_url_name'] = self._params['comment_on_url_name']
       
   121     context['comment_on_name'] = self._params['comment_on_name']
       
   122     context['work_link'] = redirect
       
   123 
       
   124   def _editPost(self, request, entity, fields, params=None):
       
   125     """See base.View._editPost().
       
   126     """
       
   127 
       
   128     user = user_logic.getForCurrentAccount()
       
   129     scope_path = fields['scope_path']
       
   130 
       
   131     if not entity:
       
   132       fields['author'] = user
       
   133       fields['link_id'] = 't%i' % (time.time())
       
   134     else:
       
   135       fields['author'] = entity.author
       
   136       fields['link_id'] = entity.link_id
       
   137       fields['modified_by'] = user
       
   138 
       
   139     fields['commented'] = self._getWorkByKeyName(scope_path).key()
       
   140 
       
   141     super(View, self)._editPost(request, entity, fields)
       
   142 
       
   143   def _editGet(self, request, entity, form):
       
   144     """See base.View._editGet().
       
   145     """
       
   146 
       
   147     form.fields['created_by'].initial = entity.author.name
       
   148     form.fields['on'].initial = entity.commented.name
       
   149     form.fields['link_id'].initial = entity.link_id
       
   150     form.fields['scope_path'].initial = entity.scope_path
       
   151 
       
   152     super(View, self)._editGet(request, entity, form)
       
   153 
       
   154   def _getWorkByKeyName(self, keyname):
       
   155     """Returns the work for the specified key name.
       
   156     """
       
   157 
       
   158     logic = self._params['comment_on_logic']
       
   159     return logic.getFromKeyName(keyname)
       
   160 
       
   161   def _editSeed(self, request, seed):
       
   162     """Checks if scope_path is seeded and puts it into to_user.
       
   163 
       
   164     For parameters see base._editSeed()
       
   165     """
       
   166 
       
   167     scope_path = seed['scope_path']
       
   168     work = self._getWorkByKeyName(scope_path)
       
   169     seed['on'] = work.title
       
   170     seed['commented'] = work
       
   171 
       
   172   def getMenusForScope(self, entity, params):
       
   173     """Returns the featured menu items for one specifc entity.
       
   174 
       
   175     A link to the home page of the specified entity is also included.
       
   176 
       
   177     Args:
       
   178       entity: the entity for which the entry should be constructed
       
   179       params: a dict with params for this View.
       
   180     """
       
   181 
       
   182     return []