5d908671a8929e4e9293b7f7ce0dcb92237b7d19
[odoo/odoo.git] / addons / website / controllers / main.py
1 # -*- coding: utf-8 -*-
2 import cStringIO
3 import datetime
4 from itertools import islice
5 import json
6 import logging
7 import re
8
9 from sys import maxint
10
11 import werkzeug.utils
12 import werkzeug.wrappers
13 from PIL import Image
14
15 import openerp
16 from openerp.addons.web import http
17 from openerp.http import request, Response
18
19 logger = logging.getLogger(__name__)
20
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)
25
26 class Website(openerp.addons.web.controllers.main.Home):
27     #------------------------------------------------------
28     # View
29     #------------------------------------------------------
30     @http.route('/', type='http', auth="public", website=True)
31     def index(self, **kw):
32         page = 'homepage'
33         try:
34             main_menu = request.registry['ir.model.data'].get_object(request.cr, request.uid, 'website', 'main_menu')
35         except Exception:
36             pass
37         else:
38             first_menu = main_menu.child_id and main_menu.child_id[0]
39             if first_menu:
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                     page = first_menu.url[6:]
44
45         return self.page(page)
46
47     @http.route(website=True, auth="public")
48     def web_login(self, *args, **kw):
49         # TODO: can't we just put auth=public, ... in web client ?
50         return super(Website, self).web_login(*args, **kw)
51
52     @http.route('/page/<page:page>', type='http', auth="public", website=True)
53     def page(self, page, **opt):
54         values = {
55             'path': page,
56         }
57         # allow shortcut for /page/<website_xml_id>
58         if '.' not in page:
59             page = 'website.%s' % page
60
61         try:
62             request.website.get_template(page)
63         except ValueError, e:
64             # page not found
65             if request.website.is_publisher():
66                 page = 'website.page_404'
67             else:
68                 return request.registry['ir.http']._handle_exception(e, 404)
69
70         return request.render(page, values)
71
72     @http.route(['/robots.txt'], type='http', auth="public")
73     def robots(self):
74         return request.render('website.robots', {'url_root': request.httprequest.url_root}, mimetype='text/plain')
75
76     @http.route('/sitemap.xml', type='http', auth="public", website=True)
77     def sitemap_xml_index(self):
78         cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
79         ira = request.registry['ir.attachment']
80         iuv = request.registry['ir.ui.view']
81         mimetype ='application/xml;charset=utf-8'
82         content = None
83
84         def create_sitemap(url, content):
85             ira.create(cr, uid, dict(
86                 datas=content.encode('base64'),
87                 mimetype=mimetype,
88                 type='binary',
89                 name=url,
90                 url=url,
91             ), context=context)
92
93         sitemap = ira.search_read(cr, uid, [('url', '=' , '/sitemap.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context)
94         if sitemap:
95             # Check if stored version is still valid
96             server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
97             create_date = datetime.datetime.strptime(sitemap[0]['create_date'], server_format)
98             delta = datetime.datetime.now() - create_date
99             if delta < SITEMAP_CACHE_TIME:
100                 content = sitemap[0]['datas'].decode('base64')
101
102         if not content:
103             # Remove all sitemaps in ir.attachments as we're going to regenerated them
104             sitemap_ids = ira.search(cr, uid, [('url', '=like' , '/sitemap%.xml'), ('type', '=', 'binary')], context=context)
105             if sitemap_ids:
106                 ira.unlink(cr, uid, sitemap_ids, context=context)
107
108             pages = 0
109             first_page = None
110             locs = request.website.enumerate_pages()
111             while True:
112                 start = pages * LOC_PER_SITEMAP
113                 values = {
114                     'locs': islice(locs, start, start + LOC_PER_SITEMAP),
115                     'url_root': request.httprequest.url_root[:-1],
116                 }
117                 urls = iuv.render(cr, uid, 'website.sitemap_locs', values, context=context)
118                 if urls.strip():
119                     page = iuv.render(cr, uid, 'website.sitemap_xml', dict(content=urls), context=context)
120                     if not first_page:
121                         first_page = page
122                     pages += 1
123                     create_sitemap('/sitemap-%d.xml' % pages, page)
124                 else:
125                     break
126             if not pages:
127                 return request.not_found()
128             elif pages == 1:
129                 content = first_page
130             else:
131                 # Sitemaps must be split in several smaller files with a sitemap index
132                 content = iuv.render(cr, uid, 'website.sitemap_index_xml', dict(
133                     pages=range(1, pages + 1),
134                     url_root=request.httprequest.url_root,
135                 ), context=context)
136             create_sitemap('/sitemap.xml', content)
137
138         return request.make_response(content, [('Content-Type', mimetype)])
139
140     #------------------------------------------------------
141     # Edit
142     #------------------------------------------------------
143     @http.route('/website/add/<path:path>', type='http', auth="user", website=True)
144     def pagenew(self, path, noredirect=False, add_menu=None):
145         xml_id = request.registry['website'].new_page(request.cr, request.uid, path, context=request.context)
146         if add_menu:
147             model, id  = request.registry["ir.model.data"].get_object_reference(request.cr, request.uid, 'website', 'main_menu')
148             request.registry['website.menu'].create(request.cr, request.uid, {
149                     'name': path,
150                     'url': "/page/" + xml_id,
151                     'parent_id': id,
152                 }, context=request.context)
153         # Reverse action in order to allow shortcut for /page/<website_xml_id>
154         url = "/page/" + re.sub(r"^website\.", '', xml_id)
155
156         if noredirect:
157             return werkzeug.wrappers.Response(url, mimetype='text/plain')
158         return werkzeug.utils.redirect(url)
159
160     @http.route('/website/theme_change', type='http', auth="user", website=True)
161     def theme_change(self, theme_id=False, **kwargs):
162         imd = request.registry['ir.model.data']
163         Views = request.registry['ir.ui.view']
164
165         _, theme_template_id = imd.get_object_reference(
166             request.cr, request.uid, 'website', 'theme')
167         views = Views.search(request.cr, request.uid, [
168             ('inherit_id', '=', theme_template_id),
169             ('application', '=', 'enabled'),
170         ], context=request.context)
171         Views.write(request.cr, request.uid, views, {
172             'application': 'disabled',
173         }, context=request.context)
174
175         if theme_id:
176             module, xml_id = theme_id.split('.')
177             _, view_id = imd.get_object_reference(
178                 request.cr, request.uid, module, xml_id)
179             Views.write(request.cr, request.uid, [view_id], {
180                 'application': 'enabled'
181             }, context=request.context)
182
183         return request.render('website.themes', {'theme_changed': True})
184
185     @http.route(['/website/snippets'], type='json', auth="public", website=True)
186     def snippets(self):
187         return request.website._render('website.snippets')
188
189     @http.route('/website/reset_templates', type='http', auth='user', methods=['POST'], website=True)
190     def reset_template(self, templates, redirect='/'):
191         templates = request.httprequest.form.getlist('templates')
192         modules_to_update = []
193         for temp_id in templates:
194             view = request.registry['ir.ui.view'].browse(request.cr, request.uid, int(temp_id), context=request.context)
195             view.model_data_id.write({
196                 'noupdate': False
197             })
198             if view.model_data_id.module not in modules_to_update:
199                 modules_to_update.append(view.model_data_id.module)
200         module_obj = request.registry['ir.module.module']
201         module_ids = module_obj.search(request.cr, request.uid, [('name', 'in', modules_to_update)], context=request.context)
202         module_obj.button_immediate_upgrade(request.cr, request.uid, module_ids, context=request.context)
203         return request.redirect(redirect)
204
205     @http.route('/website/customize_template_get', type='json', auth='user', website=True)
206     def customize_template_get(self, xml_id, full=False):
207         """ Lists the templates customizing ``xml_id``. By default, only
208         returns optional templates (which can be toggled on and off), if
209         ``full=True`` returns all templates customizing ``xml_id``
210         """
211         imd = request.registry['ir.model.data']
212         view_model, view_theme_id = imd.get_object_reference(
213             request.cr, request.uid, 'website', 'theme')
214
215         user = request.registry['res.users']\
216             .browse(request.cr, request.uid, request.uid, request.context)
217         user_groups = set(user.groups_id)
218
219         views = request.registry["ir.ui.view"]\
220             ._views_get(request.cr, request.uid, xml_id, context=request.context)
221         done = set()
222         result = []
223         for v in views:
224             if not user_groups.issuperset(v.groups_id):
225                 continue
226             if full or (v.application != 'always' and v.inherit_id.id != view_theme_id):
227                 if v.inherit_id not in done:
228                     result.append({
229                         'name': v.inherit_id.name,
230                         'id': v.id,
231                         'xml_id': v.xml_id,
232                         'inherit_id': v.inherit_id.id,
233                         'header': True,
234                         'active': False
235                     })
236                     done.add(v.inherit_id)
237                 result.append({
238                     'name': v.name,
239                     'id': v.id,
240                     'xml_id': v.xml_id,
241                     'inherit_id': v.inherit_id.id,
242                     'header': False,
243                     'active': v.application in ('always', 'enabled'),
244                 })
245         return result
246
247     @http.route('/website/get_view_translations', type='json', auth='public', website=True)
248     def get_view_translations(self, xml_id, lang=None):
249         lang = lang or request.context.get('lang')
250         views = self.customize_template_get(xml_id, full=True)
251         views_ids = [view.get('id') for view in views if view.get('active')]
252         domain = [('type', '=', 'view'), ('res_id', 'in', views_ids), ('lang', '=', lang)]
253         irt = request.registry.get('ir.translation')
254         return irt.search_read(request.cr, request.uid, domain, ['id', 'res_id', 'value','state','gengo_translation'], context=request.context)
255
256     @http.route('/website/set_translations', type='json', auth='public', website=True)
257     def set_translations(self, data, lang):
258         irt = request.registry.get('ir.translation')
259         for view_id, trans in data.items():
260             view_id = int(view_id)
261             for t in trans:
262                 initial_content = t['initial_content'].strip()
263                 new_content = t['new_content'].strip()
264                 tid = t['translation_id']
265                 if not tid:
266                     old_trans = irt.search_read(
267                         request.cr, request.uid,
268                         [
269                             ('type', '=', 'view'),
270                             ('res_id', '=', view_id),
271                             ('lang', '=', lang),
272                             ('src', '=', initial_content),
273                         ])
274                     if old_trans:
275                         tid = old_trans[0]['id']
276                 if tid:
277                     vals = {'value': new_content}
278                     irt.write(request.cr, request.uid, [tid], vals)
279                 else:
280                     new_trans = {
281                         'name': 'website',
282                         'res_id': view_id,
283                         'lang': lang,
284                         'type': 'view',
285                         'source': initial_content,
286                         'value': new_content,
287                     }
288                     if t.get('gengo_translation'):
289                         new_trans['gengo_translation'] = t.get('gengo_translation')
290                         new_trans['gengo_comment'] = t.get('gengo_comment')
291                     irt.create(request.cr, request.uid, new_trans)
292         return True
293
294     @http.route('/website/attach', type='http', auth='user', methods=['POST'], website=True)
295     def attach(self, func, upload=None, url=None):
296         Attachments = request.registry['ir.attachment']
297
298         website_url = message = None
299         if not upload:
300             website_url = url
301             name = url.split("/").pop()
302             attachment_id = Attachments.create(request.cr, request.uid, {
303                 'name':name,
304                 'type': 'url',
305                 'url': url,
306                 'res_model': 'ir.ui.view',
307             }, request.context)
308         else:
309             try:
310                 image_data = upload.read()
311                 image = Image.open(cStringIO.StringIO(image_data))
312                 w, h = image.size
313                 if w*h > 42e6: # Nokia Lumia 1020 photo resolution
314                     raise ValueError(
315                         u"Image size excessive, uploaded images must be smaller "
316                         u"than 42 million pixel")
317
318                 attachment_id = Attachments.create(request.cr, request.uid, {
319                     'name': upload.filename,
320                     'datas': image_data.encode('base64'),
321                     'datas_fname': upload.filename,
322                     'res_model': 'ir.ui.view',
323                 }, request.context)
324
325                 [attachment] = Attachments.read(
326                     request.cr, request.uid, [attachment_id], ['website_url'],
327                     context=request.context)
328                 website_url = attachment['website_url']
329             except Exception, e:
330                 logger.exception("Failed to upload image to attachment")
331                 message = unicode(e)
332
333         return """<script type='text/javascript'>
334             window.parent['%s'](%s, %s);
335         </script>""" % (func, json.dumps(website_url), json.dumps(message))
336
337     @http.route(['/website/publish'], type='json', auth="public", website=True)
338     def publish(self, id, object):
339         _id = int(id)
340         _object = request.registry[object]
341         obj = _object.browse(request.cr, request.uid, _id)
342
343         values = {}
344         if 'website_published' in _object._all_columns:
345             values['website_published'] = not obj.website_published
346         _object.write(request.cr, request.uid, [_id],
347                       values, context=request.context)
348
349         obj = _object.browse(request.cr, request.uid, _id)
350         return bool(obj.website_published)
351
352     #------------------------------------------------------
353     # Helpers
354     #------------------------------------------------------
355     @http.route(['/website/kanban'], type='http', auth="public", methods=['POST'], website=True)
356     def kanban(self, **post):
357         return request.website.kanban_col(**post)
358
359     def placeholder(self, response):
360         return request.registry['website']._image_placeholder(response)
361
362     @http.route([
363         '/website/image',
364         '/website/image/<model>/<id>/<field>'
365         ], auth="public", website=True)
366     def website_image(self, model, id, field, max_width=None, max_height=None):
367         """ Fetches the requested field and ensures it does not go above
368         (max_width, max_height), resizing it if necessary.
369
370         If the record is not found or does not have the requested field,
371         returns a placeholder image via :meth:`~.placeholder`.
372
373         Sets and checks conditional response parameters:
374         * :mailheader:`ETag` is always set (and checked)
375         * :mailheader:`Last-Modified is set iif the record has a concurrency
376           field (``__last_update``)
377
378         The requested field is assumed to be base64-encoded image data in
379         all cases.
380         """
381         response = werkzeug.wrappers.Response()
382         return request.registry['website']._image(
383                     request.cr, request.uid, model, id, field, response, max_width, max_height)
384
385
386     #------------------------------------------------------
387     # Server actions
388     #------------------------------------------------------
389     @http.route('/website/action/<path_or_xml_id_or_id>', type='http', auth="public", website=True)
390     def actions_server(self, path_or_xml_id_or_id, **post):
391         cr, uid, context = request.cr, request.uid, request.context
392         res, action_id, action = None, None, None
393         ServerActions = request.registry['ir.actions.server']
394
395         # find the action_id: either an xml_id, the path, or an ID
396         if isinstance(path_or_xml_id_or_id, basestring) and '.' in path_or_xml_id_or_id:
397             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)
398         if not action_id:
399             action_ids = ServerActions.search(cr, uid, [('website_path', '=', path_or_xml_id_or_id), ('website_published', '=', True)], context=context)
400             action_id = action_ids and action_ids[0] or None
401         if not action_id:
402             try:
403                 action_id = int(path_or_xml_id_or_id)
404             except ValueError:
405                 pass
406
407         # check it effectively exists
408         if action_id:
409             action_ids = ServerActions.exists(cr, uid, [action_id], context=context)
410             action_id = action_ids and action_ids[0] or None
411         # run it, return only if we got a Response object
412         if action_id:
413             action = ServerActions.browse(cr, uid, action_id, context=context)
414             if action.state == 'code' and action.website_published:
415                 action_res = ServerActions.run(cr, uid, [action_id], context=context)
416                 if isinstance(action_res, Response):
417                     res = action_res
418         if res:
419             return res
420         return request.redirect('/')
421