1 # -*- coding: utf-8 -*-
3 from itertools import islice
12 import werkzeug.wrappers
16 from openerp.osv import fields
17 from openerp.addons.website.models import website
18 from openerp.addons.web import http
19 from openerp.http import request, Response
21 logger = logging.getLogger(__name__)
23 # Completely arbitrary limits
24 MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
25 LOC_PER_SITEMAP = 45000
29 from functools import wraps
33 def wrapper(*args, **kw):
35 result = f(*args, **kw)
38 print 'func:%r args:[%r, %r] took: %2.4f sec' % \
39 (f.__name__, args, kw, te-ts)
45 class Website(openerp.addons.web.controllers.main.Home):
46 #------------------------------------------------------
48 #------------------------------------------------------
49 @http.route('/', type='http', auth="public", website=True, multilang=True)
50 def index(self, **kw):
52 main_menu = request.registry['ir.model.data'].get_object(request.cr, request.uid, 'website', 'main_menu')
53 first_menu = main_menu.child_id and main_menu.child_id[0]
55 if first_menu and not ((first_menu.url == '/') or first_menu.url.startswith('/#') or first_menu.url.startswith('/?')):
56 return request.redirect(first_menu.url)
59 return self.page("website.homepage")
61 @http.route(website=True, auth="public", multilang=True)
62 def web_login(self, *args, **kw):
63 # TODO: can't we just put auth=public, ... in web client ?
64 return super(Website, self).web_login(*args, **kw)
66 @http.route('/page/<path:page>', type='http', auth="public", website=True, multilang=True)
67 def page(self, page, **opt):
71 # allow shortcut for /page/<website_xml_id>
73 page = 'website.%s' % page
76 request.website.get_template(page)
79 if request.context['editable']:
80 page = 'website.page_404'
82 return request.registry['ir.http']._handle_exception(e, 404)
84 return request.render(page, values)
86 @http.route(['/robots.txt'], type='http', auth="public")
88 return request.render('website.robots', {'url_root': request.httprequest.url_root}, mimetype='text/plain')
91 @http.route('/sitemap.xml', type='http', auth="public", website=True)
92 def sitemap_xml_index(self):
94 for loc in request.website.enumerate_pages():
96 if count <= LOC_PER_SITEMAP:
97 return self.__sitemap_xml(0)
98 # Sitemaps must be split in several smaller files with a sitemap index
100 'pages': range(int(math.ceil(float(count) / LOC_PER_SITEMAP))),
101 'url_root': request.httprequest.url_root
104 'Content-Type': 'application/xml;charset=utf-8',
106 return request.render('website.sitemap_index_xml', values, headers=headers)
108 @http.route('/sitemap-<int:page>.xml', type='http', auth="public", website=True)
109 def sitemap_xml(self, page):
110 return self.__sitemap_xml(page)
113 def __sitemap_xml(self, index=0):
114 start = index * LOC_PER_SITEMAP
115 locs = islice(request.website.enumerate_pages(), start , start + LOC_PER_SITEMAP)
118 'url_root': request.httprequest.url_root.rstrip('/')
121 'Content-Type': 'application/xml;charset=utf-8',
123 return request.render('website.sitemap_xml', values, headers=headers).flatten()
125 #------------------------------------------------------
127 #------------------------------------------------------
128 @http.route('/website/add/<path:path>', type='http', auth="user", website=True)
129 def pagenew(self, path, noredirect=False, add_menu=None):
130 xml_id = request.registry['website'].new_page(request.cr, request.uid, path, context=request.context)
132 model, id = request.registry["ir.model.data"].get_object_reference(request.cr, request.uid, 'website', 'main_menu')
133 request.registry['website.menu'].create(request.cr, request.uid, {
135 'url': "/page/" + xml_id,
137 }, context=request.context)
138 # Reverse action in order to allow shortcut for /page/<website_xml_id>
139 url = "/page/" + re.sub(r"^website\.", '', xml_id)
142 return werkzeug.wrappers.Response(url, mimetype='text/plain')
143 return werkzeug.utils.redirect(url)
145 @http.route('/website/theme_change', type='http', auth="user", website=True)
146 def theme_change(self, theme_id=False, **kwargs):
147 imd = request.registry['ir.model.data']
148 view = request.registry['ir.ui.view']
150 view_model, view_option_id = imd.get_object_reference(
151 request.cr, request.uid, 'website', 'theme')
153 request.cr, request.uid, [('inherit_id', '=', view_option_id)],
154 context=request.context)
155 view.write(request.cr, request.uid, views, {'inherit_id': False},
156 context=request.context)
159 module, xml_id = theme_id.split('.')
160 view_model, view_id = imd.get_object_reference(
161 request.cr, request.uid, module, xml_id)
162 view.write(request.cr, request.uid, [view_id],
163 {'inherit_id': view_option_id}, context=request.context)
165 return request.render('website.themes', {'theme_changed': True})
167 @http.route(['/website/snippets'], type='json', auth="public", website=True)
169 return request.website._render('website.snippets')
171 @http.route('/website/reset_templates', type='http', auth='user', methods=['POST'], website=True)
172 def reset_template(self, templates, redirect='/'):
173 templates = request.httprequest.form.getlist('templates')
174 modules_to_update = []
175 for temp_id in templates:
176 view = request.registry['ir.ui.view'].browse(request.cr, request.uid, int(temp_id), context=request.context)
177 view.model_data_id.write({
180 if view.model_data_id.module not in modules_to_update:
181 modules_to_update.append(view.model_data_id.module)
182 module_obj = request.registry['ir.module.module']
183 module_ids = module_obj.search(request.cr, request.uid, [('name', 'in', modules_to_update)], context=request.context)
184 module_obj.button_immediate_upgrade(request.cr, request.uid, module_ids, context=request.context)
185 return request.redirect(redirect)
187 @http.route('/website/customize_template_toggle', type='json', auth='user', website=True)
188 def customize_template_set(self, view_id):
189 view_obj = request.registry.get("ir.ui.view")
190 view = view_obj.browse(request.cr, request.uid, int(view_id),
191 context=request.context)
195 value = view.inherit_option_id and view.inherit_option_id.id or False
196 view_obj.write(request.cr, request.uid, [view_id], {
198 }, context=request.context)
201 @http.route('/website/customize_template_get', type='json', auth='user', website=True)
202 def customize_template_get(self, xml_id, optional=True):
203 imd = request.registry['ir.model.data']
204 view_model, view_theme_id = imd.get_object_reference(
205 request.cr, request.uid, 'website', 'theme')
207 user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, request.context)
208 group_ids = [g.id for g in user.groups_id]
210 view = request.registry.get("ir.ui.view")
211 views = view._views_get(request.cr, request.uid, xml_id, context=request.context)
215 if v.groups_id and [g for g in v.groups_id if g.id not in group_ids]:
217 if v.inherit_option_id and v.inherit_option_id.id != view_theme_id or not optional:
218 if v.inherit_option_id.id not in done:
220 'name': v.inherit_option_id.name,
223 'inherit_id': v.inherit_id.id,
227 done[v.inherit_option_id.id] = True
232 'inherit_id': v.inherit_id.id,
234 'active': (v.inherit_id.id == v.inherit_option_id.id) or (not optional and v.inherit_id.id)
238 @http.route('/website/get_view_translations', type='json', auth='public', website=True)
239 def get_view_translations(self, xml_id, lang=None):
240 lang = lang or request.context.get('lang')
241 views = self.customize_template_get(xml_id, optional=False)
242 views_ids = [view.get('id') for view in views if view.get('active')]
243 domain = [('type', '=', 'view'), ('res_id', 'in', views_ids), ('lang', '=', lang)]
244 irt = request.registry.get('ir.translation')
245 return irt.search_read(request.cr, request.uid, domain, ['id', 'res_id', 'value','state','gengo_translation'], context=request.context)
247 @http.route('/website/set_translations', type='json', auth='public', website=True)
248 def set_translations(self, data, lang):
249 irt = request.registry.get('ir.translation')
250 for view_id, trans in data.items():
251 view_id = int(view_id)
253 initial_content = t['initial_content'].strip()
254 new_content = t['new_content'].strip()
255 tid = t['translation_id']
257 old_trans = irt.search_read(
258 request.cr, request.uid,
260 ('type', '=', 'view'),
261 ('res_id', '=', view_id),
263 ('src', '=', initial_content),
266 tid = old_trans[0]['id']
268 vals = {'value': new_content}
269 irt.write(request.cr, request.uid, [tid], vals)
276 'source': initial_content,
277 'value': new_content,
279 if t.get('gengo_translation'):
280 new_trans['gengo_translation'] = t.get('gengo_translation')
281 new_trans['gengo_comment'] = t.get('gengo_comment')
282 irt.create(request.cr, request.uid, new_trans)
285 @http.route('/website/attach', type='http', auth='user', methods=['POST'], website=True)
286 def attach(self, func, upload=None, url=None):
287 Attachments = request.registry['ir.attachment']
289 website_url = message = None
292 name = url.split("/").pop()
293 attachment_id = Attachments.create(request.cr, request.uid, {
297 'res_model': 'ir.ui.view',
301 image_data = upload.read()
302 image = Image.open(cStringIO.StringIO(image_data))
304 if w*h > 42e6: # Nokia Lumia 1020 photo resolution
306 u"Image size excessive, uploaded images must be smaller "
307 u"than 42 million pixel")
309 attachment_id = Attachments.create(request.cr, request.uid, {
310 'name': upload.filename,
311 'datas': image_data.encode('base64'),
312 'datas_fname': upload.filename,
313 'res_model': 'ir.ui.view',
316 [attachment] = Attachments.read(
317 request.cr, request.uid, [attachment_id], ['website_url'],
318 context=request.context)
319 website_url = attachment['website_url']
321 logger.exception("Failed to upload image to attachment")
324 return """<script type='text/javascript'>
325 window.parent['%s'](%s, %s);
326 </script>""" % (func, json.dumps(website_url), json.dumps(message))
328 @http.route(['/website/publish'], type='json', auth="public", website=True)
329 def publish(self, id, object):
331 _object = request.registry[object]
332 obj = _object.browse(request.cr, request.uid, _id)
335 if 'website_published' in _object._all_columns:
336 values['website_published'] = not obj.website_published
337 _object.write(request.cr, request.uid, [_id],
338 values, context=request.context)
340 obj = _object.browse(request.cr, request.uid, _id)
341 return bool(obj.website_published)
343 #------------------------------------------------------
345 #------------------------------------------------------
346 @http.route(['/website/kanban'], type='http', auth="public", methods=['POST'], website=True)
347 def kanban(self, **post):
348 return request.website.kanban_col(**post)
350 def placeholder(self, response):
351 return request.registry['website']._image_placeholder(response)
355 '/website/image/<model>/<id>/<field>'
356 ], auth="public", website=True)
357 def website_image(self, model, id, field, max_width=maxint, max_height=maxint):
358 """ Fetches the requested field and ensures it does not go above
359 (max_width, max_height), resizing it if necessary.
361 Resizing is bypassed if the object provides a $field_big, which will
362 be interpreted as a pre-resized version of the base field.
364 If the record is not found or does not have the requested field,
365 returns a placeholder image via :meth:`~.placeholder`.
367 Sets and checks conditional response parameters:
368 * :mailheader:`ETag` is always set (and checked)
369 * :mailheader:`Last-Modified is set iif the record has a concurrency
370 field (``__last_update``)
372 The requested field is assumed to be base64-encoded image data in
375 response = werkzeug.wrappers.Response()
376 return request.registry['website']._image(
377 request.cr, request.uid, model, id, field, response)
380 #------------------------------------------------------
382 #------------------------------------------------------
383 @http.route('/website/action/<path_or_xml_id_or_id>', type='http', auth="public", website=True)
384 def actions_server(self, path_or_xml_id_or_id, **post):
385 cr, uid, context = request.cr, request.uid, request.context
386 res, action_id, action = None, None, None
387 ServerActions = request.registry['ir.actions.server']
389 # find the action_id: either an xml_id, the path, or an ID
390 if isinstance(path_or_xml_id_or_id, basestring) and '.' in path_or_xml_id_or_id:
391 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)
393 action_ids = ServerActions.search(cr, uid, [('website_path', '=', path_or_xml_id_or_id), ('website_published', '=', True)], context=context)
394 action_id = action_ids and action_ids[0] or None
397 action_id = int(path_or_xml_id_or_id)
401 # check it effectively exists
403 action_ids = ServerActions.exists(cr, uid, [action_id], context=context)
404 action_id = action_ids and action_ids[0] or None
405 # run it, return only if we got a Response object
407 action = ServerActions.browse(cr, uid, action_id, context=context)
408 if action.state == 'code' and action.website_published:
409 action_res = ServerActions.run(cr, uid, [action_id], context=context)
410 if isinstance(action_res, Response):
414 return request.redirect('/')