1 # -*- coding: utf-8 -*-
4 from itertools import islice
12 import werkzeug.wrappers
16 from openerp.addons.web import http
17 from openerp.http import request, Response
19 logger = logging.getLogger(__name__)
21 # Completely arbitrary limits
22 MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
23 LOC_PER_SITEMAP = 45000
24 SITEMAP_CACHE_TIME = datetime.timedelta(hours=12)
26 class Website(openerp.addons.web.controllers.main.Home):
27 #------------------------------------------------------
29 #------------------------------------------------------
30 @http.route('/', type='http', auth="public", website=True)
31 def index(self, **kw):
34 main_menu = request.registry['ir.model.data'].get_object(request.cr, request.uid, 'website', 'main_menu')
38 first_menu = main_menu.child_id and main_menu.child_id[0]
40 if not (first_menu.url.startswith(('/page/', '/?', '/#')) or (first_menu.url=='/')):
41 return request.redirect(first_menu.url)
42 if first_menu.url.startswith('/page/'):
43 return request.registry['ir.http'].reroute(first_menu.url)
44 return self.page(page)
46 @http.route(website=True, auth="public")
47 def web_login(self, *args, **kw):
48 # TODO: can't we just put auth=public, ... in web client ?
49 return super(Website, self).web_login(*args, **kw)
51 @http.route('/page/<page:page>', type='http', auth="public", website=True)
52 def page(self, page, **opt):
56 # allow shortcut for /page/<website_xml_id>
58 page = 'website.%s' % page
61 request.website.get_template(page)
64 if request.website.is_publisher():
65 page = 'website.page_404'
67 return request.registry['ir.http']._handle_exception(e, 404)
69 return request.render(page, values)
71 @http.route(['/robots.txt'], type='http', auth="public")
73 return request.render('website.robots', {'url_root': request.httprequest.url_root}, mimetype='text/plain')
75 @http.route('/sitemap.xml', type='http', auth="public", website=True)
76 def sitemap_xml_index(self):
77 cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
78 ira = request.registry['ir.attachment']
79 iuv = request.registry['ir.ui.view']
80 mimetype ='application/xml;charset=utf-8'
83 def create_sitemap(url, content):
84 ira.create(cr, uid, dict(
85 datas=content.encode('base64'),
92 sitemap = ira.search_read(cr, uid, [('url', '=' , '/sitemap.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context)
94 # Check if stored version is still valid
95 server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
96 create_date = datetime.datetime.strptime(sitemap[0]['create_date'], server_format)
97 delta = datetime.datetime.now() - create_date
98 if delta < SITEMAP_CACHE_TIME:
99 content = sitemap[0]['datas'].decode('base64')
102 # Remove all sitemaps in ir.attachments as we're going to regenerated them
103 sitemap_ids = ira.search(cr, uid, [('url', '=like' , '/sitemap%.xml'), ('type', '=', 'binary')], context=context)
105 ira.unlink(cr, uid, sitemap_ids, context=context)
109 locs = request.website.enumerate_pages()
111 start = pages * LOC_PER_SITEMAP
113 'locs': islice(locs, start, start + LOC_PER_SITEMAP),
114 'url_root': request.httprequest.url_root[:-1],
116 urls = iuv.render(cr, uid, 'website.sitemap_locs', values, context=context)
118 page = iuv.render(cr, uid, 'website.sitemap_xml', dict(content=urls), context=context)
122 create_sitemap('/sitemap-%d.xml' % pages, page)
126 return request.not_found()
130 # Sitemaps must be split in several smaller files with a sitemap index
131 content = iuv.render(cr, uid, 'website.sitemap_index_xml', dict(
132 pages=range(1, pages + 1),
133 url_root=request.httprequest.url_root,
135 create_sitemap('/sitemap.xml', content)
137 return request.make_response(content, [('Content-Type', mimetype)])
139 #------------------------------------------------------
141 #------------------------------------------------------
142 @http.route('/website/add/<path:path>', type='http', auth="user", website=True)
143 def pagenew(self, path, noredirect=False, add_menu=None):
144 xml_id = request.registry['website'].new_page(request.cr, request.uid, path, context=request.context)
146 model, id = request.registry["ir.model.data"].get_object_reference(request.cr, request.uid, 'website', 'main_menu')
147 request.registry['website.menu'].create(request.cr, request.uid, {
149 'url': "/page/" + xml_id,
151 }, context=request.context)
152 # Reverse action in order to allow shortcut for /page/<website_xml_id>
153 url = "/page/" + re.sub(r"^website\.", '', xml_id)
156 return werkzeug.wrappers.Response(url, mimetype='text/plain')
157 return werkzeug.utils.redirect(url)
159 @http.route(['/website/snippets'], type='json', auth="public", website=True)
161 return request.website._render('website.snippets')
163 @http.route('/website/reset_templates', type='http', auth='user', methods=['POST'], website=True)
164 def reset_template(self, templates, redirect='/'):
165 templates = request.httprequest.form.getlist('templates')
166 modules_to_update = []
167 for temp_id in templates:
168 view = request.registry['ir.ui.view'].browse(request.cr, request.uid, int(temp_id), context=request.context)
171 view.model_data_id.write({
174 if view.model_data_id.module not in modules_to_update:
175 modules_to_update.append(view.model_data_id.module)
177 if modules_to_update:
178 module_obj = request.registry['ir.module.module']
179 module_ids = module_obj.search(request.cr, request.uid, [('name', 'in', modules_to_update)], context=request.context)
181 module_obj.button_immediate_upgrade(request.cr, request.uid, module_ids, context=request.context)
182 return request.redirect(redirect)
184 @http.route('/website/customize_template_get', type='json', auth='user', website=True)
185 def customize_template_get(self, xml_id, full=False, bundles=False):
186 """ Lists the templates customizing ``xml_id``. By default, only
187 returns optional templates (which can be toggled on and off), if
188 ``full=True`` returns all templates customizing ``xml_id``
189 ``bundles=True`` returns also the asset bundles
191 imd = request.registry['ir.model.data']
192 view_model, view_theme_id = imd.get_object_reference(
193 request.cr, request.uid, 'website', 'theme')
195 user = request.registry['res.users']\
196 .browse(request.cr, request.uid, request.uid, request.context)
197 user_groups = set(user.groups_id)
199 views = request.registry["ir.ui.view"]\
200 ._views_get(request.cr, request.uid, xml_id, bundles=bundles, context=request.context)
204 if not user_groups.issuperset(v.groups_id):
206 if full or (v.application != 'always' and v.inherit_id.id != view_theme_id):
207 if v.inherit_id not in done:
209 'name': v.inherit_id.name,
212 'inherit_id': v.inherit_id.id,
216 done.add(v.inherit_id)
221 'inherit_id': v.inherit_id.id,
223 'active': v.application in ('always', 'enabled'),
227 @http.route('/website/get_view_translations', type='json', auth='public', website=True)
228 def get_view_translations(self, xml_id, lang=None):
229 lang = lang or request.context.get('lang')
230 views = self.customize_template_get(xml_id, full=True)
231 views_ids = [view.get('id') for view in views if view.get('active')]
232 domain = [('type', '=', 'view'), ('res_id', 'in', views_ids), ('lang', '=', lang)]
233 irt = request.registry.get('ir.translation')
234 return irt.search_read(request.cr, request.uid, domain, ['id', 'res_id', 'value','state','gengo_translation'], context=request.context)
236 @http.route('/website/set_translations', type='json', auth='public', website=True)
237 def set_translations(self, data, lang):
238 irt = request.registry.get('ir.translation')
239 for view_id, trans in data.items():
240 view_id = int(view_id)
242 initial_content = t['initial_content'].strip()
243 new_content = t['new_content'].strip()
244 tid = t['translation_id']
246 old_trans = irt.search_read(
247 request.cr, request.uid,
249 ('type', '=', 'view'),
250 ('res_id', '=', view_id),
252 ('src', '=', initial_content),
255 tid = old_trans[0]['id']
257 vals = {'value': new_content}
258 irt.write(request.cr, request.uid, [tid], vals)
265 'source': initial_content,
266 'value': new_content,
268 if t.get('gengo_translation'):
269 new_trans['gengo_translation'] = t.get('gengo_translation')
270 new_trans['gengo_comment'] = t.get('gengo_comment')
271 irt.create(request.cr, request.uid, new_trans)
274 @http.route('/website/attach', type='http', auth='user', methods=['POST'], website=True)
275 def attach(self, func, upload=None, url=None):
276 Attachments = request.registry['ir.attachment']
278 website_url = message = None
281 name = url.split("/").pop()
282 attachment_id = Attachments.create(request.cr, request.uid, {
286 'res_model': 'ir.ui.view',
290 image_data = upload.read()
291 image = Image.open(cStringIO.StringIO(image_data))
293 if w*h > 42e6: # Nokia Lumia 1020 photo resolution
295 u"Image size excessive, uploaded images must be smaller "
296 u"than 42 million pixel")
298 attachment_id = Attachments.create(request.cr, request.uid, {
299 'name': upload.filename,
300 'datas': image_data.encode('base64'),
301 'datas_fname': upload.filename,
302 'res_model': 'ir.ui.view',
305 [attachment] = Attachments.read(
306 request.cr, request.uid, [attachment_id], ['website_url'],
307 context=request.context)
308 website_url = attachment['website_url']
310 logger.exception("Failed to upload image to attachment")
313 return """<script type='text/javascript'>
314 window.parent['%s'](%s, %s);
315 </script>""" % (func, json.dumps(website_url), json.dumps(message))
317 @http.route(['/website/publish'], type='json', auth="public", website=True)
318 def publish(self, id, object):
320 _object = request.registry[object]
321 obj = _object.browse(request.cr, request.uid, _id)
324 if 'website_published' in _object._all_columns:
325 values['website_published'] = not obj.website_published
326 _object.write(request.cr, request.uid, [_id],
327 values, context=request.context)
329 obj = _object.browse(request.cr, request.uid, _id)
330 return bool(obj.website_published)
332 #------------------------------------------------------
334 #------------------------------------------------------
336 def get_view_ids(self, xml_ids):
338 imd = request.registry['ir.model.data']
339 for xml_id in xml_ids:
341 xml = xml_id.split(".")
342 view_model, id = imd.get_object_reference(request.cr, request.uid, xml[0], xml[1])
348 @http.route(['/website/theme_customize_get'], type='json', auth="public", website=True)
349 def theme_customize_get(self, xml_ids):
350 view = request.registry["ir.ui.view"]
353 ids = self.get_view_ids(xml_ids)
354 for v in view.browse(request.cr, request.uid, ids, context=request.context):
355 if v.application != "disabled":
356 enable.append(v.xml_id)
358 disable.append(v.xml_id)
359 return [enable, disable]
361 @http.route(['/website/theme_customize'], type='json', auth="public", website=True)
362 def theme_customize(self, enable, disable):
363 """ enable or Disable lists of ``xml_id`` of the inherit templates
365 cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
366 view = pool["ir.ui.view"]
368 def set_application(ids, application):
370 for v in view.browse(cr, uid, self.get_view_ids(ids), context=context):
371 if v.application == 'always':
373 if v.application != application:
374 write_ids.append(v.id)
377 view.write(cr, uid, write_ids, {'application': application})
379 set_application(disable, 'disabled')
380 set_application(enable, 'enabled')
384 @http.route(['/website/theme_customize_reload'], type='http', auth="public", website=True)
385 def theme_customize_reload(self, href, enable, disable):
386 self.theme_customize(enable and enable.split(",") or [],disable and disable.split(",") or [])
387 return request.redirect(href + ("&theme=true" if "#" in href else "#theme=true"))
389 #------------------------------------------------------
391 #------------------------------------------------------
392 @http.route(['/website/kanban'], type='http', auth="public", methods=['POST'], website=True)
393 def kanban(self, **post):
394 return request.website.kanban_col(**post)
396 def placeholder(self, response):
397 return request.registry['website']._image_placeholder(response)
401 '/website/image/<model>/<id>/<field>'
402 ], auth="public", website=True)
403 def website_image(self, model, id, field, max_width=None, max_height=None):
404 """ Fetches the requested field and ensures it does not go above
405 (max_width, max_height), resizing it if necessary.
407 If the record is not found or does not have the requested field,
408 returns a placeholder image via :meth:`~.placeholder`.
410 Sets and checks conditional response parameters:
411 * :mailheader:`ETag` is always set (and checked)
412 * :mailheader:`Last-Modified is set iif the record has a concurrency
413 field (``__last_update``)
415 The requested field is assumed to be base64-encoded image data in
418 response = werkzeug.wrappers.Response()
419 return request.registry['website']._image(
420 request.cr, request.uid, model, id, field, response, max_width, max_height)
423 #------------------------------------------------------
425 #------------------------------------------------------
426 @http.route('/website/action/<path_or_xml_id_or_id>', type='http', auth="public", website=True)
427 def actions_server(self, path_or_xml_id_or_id, **post):
428 cr, uid, context = request.cr, request.uid, request.context
429 res, action_id, action = None, None, None
430 ServerActions = request.registry['ir.actions.server']
432 # find the action_id: either an xml_id, the path, or an ID
433 if isinstance(path_or_xml_id_or_id, basestring) and '.' in path_or_xml_id_or_id:
434 action_id = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, request.uid, path_or_xml_id_or_id, raise_if_not_found=False)
436 action_ids = ServerActions.search(cr, uid, [('website_path', '=', path_or_xml_id_or_id), ('website_published', '=', True)], context=context)
437 action_id = action_ids and action_ids[0] or None
440 action_id = int(path_or_xml_id_or_id)
444 # check it effectively exists
446 action_ids = ServerActions.exists(cr, uid, [action_id], context=context)
447 action_id = action_ids and action_ids[0] or None
448 # run it, return only if we got a Response object
450 action = ServerActions.browse(cr, uid, action_id, context=context)
451 if action.state == 'code' and action.website_published:
452 action_res = ServerActions.run(cr, uid, [action_id], context=context)
453 if isinstance(action_res, werkzeug.wrappers.Response):
457 return request.redirect('/')