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.
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.
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.
56 params: a dict with params for this View
59 rights
= access
.Checker(params
)
60 rights
['home'] = ['allow']
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
),
85 patterns
+= [(r
'^%(url_name)s/(?P<access_type>home)/%(key_fields)s$',
86 'soc.views.models.%(module_name)s.home',
89 new_params
['extra_django_patterns'] = patterns
91 params
= dicts
.merge(params
, new_params
, sub_merge
=True)
93 super(View
, self
).__init
__(params
=params
)
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
)
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().
124 home_doc
= entity
.home
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
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
},
146 allowed_to_edit
= True
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().
163 form
.fields
['home_link_id'].initial
= entity
.home
.link_id
165 # TODO(Pawel.Solyga): use logging to log exception
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
185 properties
= {'home_for': entity
}
186 document_logic
.logic
.updateEntityProperties(home_doc
, properties
)
188 super(View
, self
)._editPost
(request
, entity
, fields
)