Style fixes and removal of unused imports in soc.views.models.
[Melange.git] / app / soc / views / models / document.py
blob300168d7c0091a939058b3baef88ac5aaabf1994
1 #!/usr/bin/python2.5
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 Documents.
18 """
20 __authors__ = [
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Lennard de Rijk" <ljvderijk@gmail.com>',
23 '"Pawel Solyga" <pawel.solyga@gmail.com>',
27 from django import forms
29 from soc.logic import cleaning
30 from soc.logic import dicts
31 from soc.logic.models.document import logic as document_logic
32 from soc.logic.models.user import logic as user_logic
33 from soc.views.helper import access
34 from soc.views.helper import decorators
35 from soc.views.helper import redirects
36 from soc.views.helper import widgets
37 from soc.views.models import base
40 class View(base.View):
41 """View methods for the Document model.
42 """
44 def __init__(self, params=None):
45 """Defines the fields and methods required for the base View class
46 to provide the user with list, public, create, edit and delete views.
48 Params:
49 params: a dict with params for this View
50 """
52 rights = access.Checker(params)
53 rights['any_access'] = ['allow']
54 rights['show'] = ['checkIsDocumentReadable']
55 rights['create'] = ['checkIsUser']
56 rights['edit'] = ['checkIsDocumentWritable']
57 rights['delete'] = ['checkIsDocumentWritable']
58 rights['list'] = ['checkDocumentList']
59 rights['pick'] = ['checkDocumentPick']
61 new_params = {}
62 new_params['logic'] = document_logic
63 new_params['rights'] = rights
65 new_params['name'] = "Document"
66 new_params['pickable'] = True
68 new_params['export_content_type'] = 'text/text'
69 new_params['export_extension'] = '.html'
70 new_params['export_function'] = lambda x: (x.content, x.link_id)
71 new_params['delete_redirect'] = '/'
72 new_params['list_key_order'] = [
73 'link_id', 'scope_path', 'name', 'short_name', 'title',
74 'content', 'prefix','read_access','write_access']
76 new_params['no_create_raw'] = True
77 new_params['no_create_with_scope'] = True
78 new_params['no_create_with_key_fields'] = True
79 new_params['no_list_raw'] = True
80 new_params['sans_link_id_create'] = True
81 new_params['sans_link_id_list'] = True
83 new_params['create_dynafields'] = [
84 {'name': 'link_id',
85 'base': forms.fields.CharField,
86 'label': 'Document Link ID',
90 new_params['create_extra_dynaproperties'] = {
91 'content': forms.fields.CharField(
92 widget=widgets.FullTinyMCE(attrs={'rows': 25, 'cols': 100})),
93 'scope_path': forms.fields.CharField(widget=forms.HiddenInput,
94 required=True),
95 'prefix': forms.fields.CharField(widget=widgets.ReadOnlyInput(),
96 required=True),
97 'clean_content': cleaning.clean_html_content('content'),
98 'clean_link_id': cleaning.clean_link_id('link_id'),
99 'clean_scope_path': cleaning.clean_scope_path('scope_path'),
100 'clean': cleaning.validate_document_acl(self, True),
102 new_params['extra_dynaexclude'] = ['author', 'created', 'home_for',
103 'modified_by', 'modified']
105 new_params['edit_extra_dynaproperties'] = {
106 'doc_key_name': forms.fields.CharField(widget=forms.HiddenInput),
107 'created_by': forms.fields.CharField(
108 widget=widgets.ReadOnlyInput(), required=False),
109 'last_modified_by': forms.fields.CharField(
110 widget=widgets.ReadOnlyInput(), required=False),
111 'clean': cleaning.validate_document_acl(self),
114 params = dicts.merge(params, new_params)
116 super(View, self).__init__(params=params)
118 def list(self, request, access_type,
119 page_name=None, params=None, filter=None, order=None, **kwargs):
120 """See base.View.list.
123 return super(View, self).list(request, access_type, page_name=page_name,
124 params=params, filter=kwargs)
126 def _editPost(self, request, entity, fields):
127 """See base.View._editPost().
130 user = user_logic.getForCurrentAccount()
132 if not entity:
133 fields['author'] = user
134 else:
135 fields['author'] = entity.author
137 fields['modified_by'] = user
139 super(View, self)._editPost(request, entity, fields)
141 def _editGet(self, request, entity, form):
142 """See base.View._editGet().
145 form.fields['created_by'].initial = entity.author.name
146 form.fields['last_modified_by'].initial = entity.modified_by.name
147 form.fields['doc_key_name'].initial = entity.key().name()
149 super(View, self)._editGet(request, entity, form)
151 def getMenusForScope(self, entity, params):
152 """Returns the featured menu items for one specifc entity.
154 A link to the home page of the specified entity is also included.
156 Args:
157 entity: the entity for which the entry should be constructed
158 params: a dict with params for this View.
161 filter = {
162 'prefix' : params['url_name'],
163 'scope_path': entity.key().name(),
164 'is_featured': True,
167 entities = self._logic.getForFields(filter)
169 submenus = []
171 # add a link to the home page
172 submenu = (redirects.getHomeRedirect(entity, params), "Home", 'show')
173 submenus.append(submenu)
175 # add a link to all featured documents
176 for entity in entities:
177 #TODO only if a document is readable it might be added
178 submenu = (redirects.getPublicRedirect(entity, self._params),
179 entity.short_name, 'show')
180 submenus.append(submenu)
182 return submenus
185 view = View()
187 admin = decorators.view(view.admin)
188 create = decorators.view(view.create)
189 edit = decorators.view(view.edit)
190 delete = decorators.view(view.delete)
191 list = decorators.view(view.list)
192 public = decorators.view(view.public)
193 export = decorators.view(view.export)
194 pick = decorators.view(view.pick)