3 # Copyright 2008 the Melange authors.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 """Views for comments.
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Matthew Wilkes" <matthew@matthewwilkes.co.uk>',
28 from django
import forms
30 from soc
.logic
import dicts
31 from soc
.logic
.models
.user
import logic
as user_logic
32 from soc
.logic
.models
.comment
import logic
as comment_logic
33 from soc
.views
import helper
34 from soc
.views
.helper
import access
35 from soc
.views
.helper
import redirects
36 from soc
.views
.models
import base
39 class View(base
.View
):
40 """View methods for the comment model.
43 def __init__(self
, params
=None):
44 """Defines the fields and methods required for the base View class
45 to provide the user with list, public, create, edit and delete views.
48 params: a dict with params for this View
49 comment_on_name: e.g. 'Document'
50 comment_on_url_name: e.g. 'document'
53 rights
= access
.Checker(params
)
54 rights
['create'] = [('checkSeeded', ['checkIsDocumentReadable',
56 rights
['edit'] = [('checkIsMyEntity', [comment_logic
, 'author', True])]
57 rights
['delete'] = [('checkIsMyEntity', [comment_logic
, 'author', True])]
60 new_params
['logic'] = comment_logic
61 new_params
['rights'] = rights
63 new_params
['name'] = "Comment"
65 new_params
['create_template'] = 'soc/comment/edit.html'
66 new_params
['edit_template'] = 'soc/comment/edit.html'
68 new_params
['no_show'] = True
69 new_params
['no_admin'] = True
70 new_params
['no_create_raw'] = True
71 new_params
['no_create_with_key_fields'] = True
72 new_params
['no_list_raw'] = True
74 new_params
['create_extra_dynaproperties'] = {
75 'on': forms
.fields
.CharField(widget
=helper
.widgets
.ReadOnlyInput(),
77 'content': forms
.fields
.CharField(
78 widget
=helper
.widgets
.TinyMCE(attrs
={'rows':10, 'cols':40})),
79 'scope_path': forms
.CharField(widget
=forms
.HiddenInput
, required
=True),
81 new_params
['extra_dynaexclude'] = ['author', 'link_id', 'modified_by']
83 new_params
['edit_extra_dynaproperties'] = {
84 'link_id': forms
.CharField(widget
=forms
.HiddenInput
, required
=True),
85 'created_by': forms
.fields
.CharField(
86 widget
=helper
.widgets
.ReadOnlyInput(), required
=False),
89 params
= dicts
.merge(params
, new_params
)
90 super(View
, self
).__init
__(params
=params
)
92 def _editContext(self
, request
, context
):
93 """see base.View._editContext.
96 entity
= context
['entity']
101 seed
= context
['seed']
102 on
= seed
['commented']
104 params
= {'url_name': self
._params
['comment_on_url_name']}
105 redirect
= redirects
.getPublicRedirect(on
, params
)
107 context
['comment_on_url_name'] = self
._params
['comment_on_url_name']
108 context
['comment_on_name'] = self
._params
['comment_on_name']
109 context
['work_link'] = redirect
111 def _editPost(self
, request
, entity
, fields
):
112 """See base.View._editPost().
115 user
= user_logic
.getForCurrentAccount()
116 scope_path
= fields
['scope_path']
119 fields
['author'] = user
120 fields
['link_id'] = 't%i' % (int(time
.time()*100))
122 fields
['author'] = entity
.author
123 fields
['link_id'] = entity
.link_id
124 fields
['modified_by'] = user
126 fields
['commented'] = self
._getWorkByKeyName
(scope_path
).key()
128 super(View
, self
)._editPost
(request
, entity
, fields
)
130 def _editGet(self
, request
, entity
, form
):
131 """See base.View._editGet().
134 form
.fields
['created_by'].initial
= entity
.author
.name
135 form
.fields
['on'].initial
= entity
.scope
.name
136 form
.fields
['link_id'].initial
= entity
.link_id
137 form
.fields
['scope_path'].initial
= entity
.scope_path
139 super(View
, self
)._editGet
(request
, entity
, form
)
141 def _getWorkByKeyName(self
, keyname
):
142 """Returns the work for the specified key name.
145 logic
= self
._params
['comment_on_logic']
146 return logic
.getFromKeyName(keyname
)
148 def _editSeed(self
, request
, seed
):
149 """See base._editSeed().
152 scope_path
= seed
['scope_path']
153 work
= self
._getWorkByKeyName
(scope_path
)
154 seed
['on'] = work
.title
155 seed
['commented'] = work
157 def getMenusForScope(self
, entity
, params
):
158 """Returns the featured menu items for one specifc entity.
160 A link to the home page of the specified entity is also included.
163 entity: the entity for which the entry should be constructed
164 params: a dict with params for this View.