Small indentation fix.
[Melange.git] / app / soc / views / models / presence.py
blob11830def28bcc4d03880e03466c9b040f2bcb8e7
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 Models with a "presence" on a Melange site.
18 """
20 __authors__ = [
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
25 from google.appengine.ext import db
27 from django.utils.translation import ugettext
29 from soc.cache import home
30 from soc.logic import cleaning
31 from soc.logic import dicts
32 from soc.logic.models import document as document_logic
33 from soc.views import helper
34 from soc.views.helper import access
35 from soc.views.helper import decorators
36 from soc.views.helper import redirects
37 from soc.views.helper import widgets
38 from soc.views.models import base
40 import soc.models.presence
41 import soc.logic.models.presence
42 import soc.logic.dicts
43 import soc.views.helper
44 import soc.views.helper.widgets
47 class View(base.View):
48 """View methods for the Presence model.
49 """
51 def __init__(self, params):
52 """Defines the fields and methods required for the base View class
53 to provide the user with list, public, create, edit and delete views.
55 Params:
56 params: a dict with params for this View
57 """
59 rights = access.Checker(params)
60 rights['home'] = ['allow']
62 new_params = {}
63 new_params['rights'] = rights
65 new_params['extra_dynaexclude'] = ['home']
66 new_params['home_template'] = 'soc/presence/home.html'
68 new_params['create_extra_dynaproperties'] = {
69 # add cleaning of the link id and feed url
70 'clean_link_id': cleaning.clean_link_id('link_id'),
71 'clean_feed_url': cleaning.clean_feed_url,
74 new_params['edit_extra_dynaproperties'] = {
75 'home_link_id': widgets.ReferenceField(
76 reference_url='document', filter=['__scoped__'],
77 filter_fields={'prefix': params['document_prefix']},
78 required=False, label=ugettext('Home page Document link ID'),
79 help_text=soc.models.work.Work.link_id.help_text),
82 patterns = []
84 page_name = "Home"
85 patterns += [(r'^%(url_name)s/(?P<access_type>home)/%(key_fields)s$',
86 'soc.views.models.%(module_name)s.home',
87 page_name)]
89 new_params['extra_django_patterns'] = patterns
91 params = dicts.merge(params, new_params, sub_merge=True)
93 super(View, self).__init__(params=params)
95 @home.cache
96 @decorators.check_access
97 def home(self, request, access_type,
98 page_name=None, params=None, **kwargs):
99 """See base.View.public().
101 Overrides public_template to point at 'home_template'.
104 key_name = self._logic.getKeyNameFromFields(kwargs)
105 redirect = '/%s/show/%s' % (self._params['url_name'], key_name)
107 new_params = {}
108 new_params['public_template'] = self._params['home_template']
109 new_params['public_redirect'] = redirect
111 params = dicts.merge(params, new_params)
113 return self.public(request, access_type,
114 page_name=page_name, params=params, **kwargs)
116 def _public(self, request, entity, context):
117 """See base.View._public().
120 if not entity:
121 return
123 try:
124 home_doc = entity.home
125 except db.Error:
126 home_doc = None
128 if not home_doc:
129 return False
131 home_doc.content = helper.templates.unescape(home_doc.content)
132 context['home_document'] = home_doc
134 # check if the current user is allowed edit the home document
135 rights = self._params['rights']
137 allowed_to_edit = False
139 try:
140 # use the IsDocumentWritable check because we have no django args
141 rights.checkIsDocumentWritable({'key_name': home_doc.key().name(),
142 'prefix': home_doc.prefix,
143 'scope_path': home_doc.scope_path,
144 'link_id': home_doc.link_id},
145 'key_name')
146 allowed_to_edit = True
147 except:
148 pass
150 if allowed_to_edit:
151 # put the link to edit to home document in context
152 context['home_document_edit_redirect'] = redirects.getEditRedirect(
153 home_doc, {'url_name': 'document'})
155 return super(View, self)._public(request, entity, context)
157 def _editGet(self, request, entity, form):
158 """See base.View._editGet().
161 try:
162 if entity.home:
163 form.fields['home_link_id'].initial = entity.home.link_id
164 except db.Error:
165 # TODO(Pawel.Solyga): use logging to log exception
166 return
168 super(View, self)._editGet(request, entity, form)
170 def _editPost(self, request, entity, fields):
171 """See base.View._editPost().
174 if 'home_link_id' not in fields:
175 return super(View, self)._editPost(request, entity, fields)
177 if not fields['home_link_id'] and entity.home:
178 properties = {'home_for': None}
179 document_logic.logic.updateEntityProperties(entity.home, properties)
181 home_doc = fields.get('resolved_home_link_id')
182 fields['home'] = home_doc
184 if home_doc:
185 properties = {'home_for': entity}
186 document_logic.logic.updateEntityProperties(home_doc, properties)
188 super(View, self)._editPost(request, entity, fields)