Fix user_self calling editGet with a wrong parameter
[Melange.git] / app / soc / views / models / presence.py
blob51a5bb8ba882effd26a8cbd92630d6eef000751c
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 widgets
37 from soc.views.models import base
39 import soc.models.presence
40 import soc.logic.models.presence
41 import soc.logic.dicts
42 import soc.views.helper
43 import soc.views.helper.widgets
46 class View(base.View):
47 """View methods for the Presence model.
48 """
50 def __init__(self, params):
51 """Defines the fields and methods required for the base View class
52 to provide the user with list, public, create, edit and delete views.
54 Params:
55 params: a dict with params for this View
56 """
58 rights = access.Checker(params)
59 rights['home'] = ['allow']
61 new_params = {}
62 new_params['rights'] = rights
64 new_params['extra_dynaexclude'] = ['home']
65 new_params['home_template'] = 'soc/presence/home.html'
67 new_params['create_extra_dynaproperties'] = {
68 # add cleaning of the link id and feed url
69 'clean_link_id': cleaning.clean_link_id('link_id'),
70 'clean_feed_url': cleaning.clean_feed_url,
73 new_params['edit_extra_dynaproperties'] = {
74 'home_link_id': widgets.ReferenceField(
75 reference_url='document', filter=['__scoped__'],
76 filter_fields={'prefix': params['document_prefix']},
77 required=False, label=ugettext('Home page Document link ID'),
78 help_text=soc.models.work.Work.link_id.help_text),
81 patterns = []
83 page_name = "Home"
84 patterns += [(r'^%(url_name)s/(?P<access_type>home)/%(key_fields)s$',
85 'soc.views.models.%(module_name)s.home',
86 page_name)]
88 new_params['extra_django_patterns'] = patterns
90 params = dicts.merge(params, new_params, sub_merge=True)
92 super(View, self).__init__(params=params)
94 @home.cache
95 @decorators.check_access
96 def home(self, request, access_type,
97 page_name=None, params=None, **kwargs):
98 """See base.View.public().
100 Overrides public_template to point at 'home_template'.
103 key_name = self._logic.getKeyNameFromFields(kwargs)
104 redirect = '/%s/show/%s' % (self._params['url_name'], key_name)
106 new_params = {}
107 new_params['public_template'] = self._params['home_template']
108 new_params['public_redirect'] = redirect
110 params = dicts.merge(params, new_params)
112 return self.public(request, access_type,
113 page_name=page_name, params=params, **kwargs)
115 def _public(self, request, entity, context):
116 """See base.View._public().
119 if not entity:
120 return
122 try:
123 home_doc = entity.home
124 except db.Error:
125 home_doc = None
127 if not home_doc:
128 return False
130 home_doc.content = helper.templates.unescape(home_doc.content)
131 context['home_document'] = home_doc
133 return True
135 def _editGet(self, request, entity, form):
136 """See base.View._editGet().
139 try:
140 if entity.home:
141 form.fields['home_link_id'].initial = entity.home.link_id
142 except db.Error:
143 # TODO(Pawel.Solyga): use logging to log exception
144 return
146 super(View, self)._editGet(request, entity, form)
148 def _editPost(self, request, entity, fields):
149 """See base.View._editPost().
152 if 'home_link_id' not in fields:
153 return super(View, self)._editPost(request, entity, fields)
155 if not fields['home_link_id'] and entity.home:
156 properties = {'home_for': None}
157 document_logic.logic.updateEntityProperties(entity.home, properties)
159 home_doc = fields.get('resolved_home_link_id')
160 fields['home'] = home_doc
162 if home_doc:
163 properties = {'home_for': entity}
164 document_logic.logic.updateEntityProperties(home_doc, properties)
166 super(View, self)._editPost(request, entity, fields)