main.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import werkzeug
  2. from openerp.http import request
  3. from openerp.addons.web import http
  4. from openerp.addons.website.controllers.main import Website
  5. from openerp.addons.base.ir.ir_qweb import QWebTemplateNotFound
  6. from openerp.addons.website_less.ir_qweb import AssetsBundle
  7. from openerp.addons.web.controllers.main import Home, make_conditional, BUNDLE_MAXAGE
  8. class Home_less(Home):
  9. @http.route([
  10. '/web/js/<xmlid>',
  11. '/web/js/<xmlid>/<version>',
  12. ], type='http', auth='public')
  13. def js_bundle(self, xmlid, version=None, **kw):
  14. try:
  15. bundle = AssetsBundle(xmlid)
  16. except QWebTemplateNotFound:
  17. return request.not_found()
  18. response = request.make_response(bundle.js(), [('Content-Type', 'application/javascript')])
  19. return make_conditional(response, bundle.last_modified, max_age=BUNDLE_MAXAGE)
  20. @http.route([
  21. '/web/css/<xmlid>',
  22. '/web/css/<xmlid>/<version>',
  23. ], type='http', auth='public')
  24. def css_bundle(self, xmlid, version=None, **kw):
  25. try:
  26. bundle = AssetsBundle(xmlid)
  27. except QWebTemplateNotFound:
  28. return request.not_found()
  29. response = request.make_response(bundle.css(), [('Content-Type', 'text/css')])
  30. return make_conditional(response, bundle.last_modified, max_age=BUNDLE_MAXAGE)
  31. class Website_less(Website):
  32. #------------------------------------------------------
  33. # Themes
  34. #------------------------------------------------------
  35. def get_view_ids(self, xml_ids):
  36. ids = []
  37. imd = request.registry['ir.model.data']
  38. for xml_id in xml_ids:
  39. if "." in xml_id:
  40. xml = xml_id.split(".")
  41. view_model, id = imd.get_object_reference(request.cr, request.uid, xml[0], xml[1])
  42. else:
  43. id = int(xml_id)
  44. ids.append(id)
  45. return ids
  46. @http.route(['/website/theme_customize_get'], type='json', auth="public", website=True)
  47. def theme_customize_get(self, xml_ids):
  48. view = request.registry["ir.ui.view"]
  49. enable = []
  50. disable = []
  51. ids = self.get_view_ids(xml_ids)
  52. context = dict(request.context or {}, active_test=True)
  53. for v in view.browse(request.cr, request.uid, ids, context=context):
  54. if v.active:
  55. enable.append(v.xml_id)
  56. else:
  57. disable.append(v.xml_id)
  58. return [enable, disable]
  59. @http.route(['/website/theme_customize'], type='json', auth="public", website=True)
  60. def theme_customize(self, enable, disable):
  61. """ enable or Disable lists of ``xml_id`` of the inherit templates
  62. """
  63. cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
  64. view = pool["ir.ui.view"]
  65. context = dict(request.context or {}, active_test=True)
  66. def set_active(ids, active):
  67. if ids:
  68. view.write(cr, uid, self.get_view_ids(ids), {'active': active}, context=context)
  69. set_active(disable, False)
  70. set_active(enable, True)
  71. return True
  72. @http.route(['/website/theme_customize_reload'], type='http', auth="public", website=True)
  73. def theme_customize_reload(self, href, enable, disable):
  74. self.theme_customize(enable and enable.split(",") or [], disable and disable.split(",") or [])
  75. return request.redirect(href + ("&theme=true" if "#" in href else "#theme=true"))
  76. @http.route(['/website/multi_render'], type='json', auth="public", website=True)
  77. def multi_render(self, ids_or_xml_ids, values=None):
  78. res = {}
  79. for id_or_xml_id in ids_or_xml_ids:
  80. res[id_or_xml_id] = request.registry["ir.ui.view"].render(request.cr, request.uid,
  81. id_or_xml_id, values=values, engine='ir.qweb', context=request.context)
  82. return res
  83. @http.route([
  84. '/website/image',
  85. '/website/image/<xmlid>',
  86. '/website/image/<xmlid>/<field>',
  87. '/website/image/<xmlid>/<int:max_width>x<int:max_height>',
  88. '/website/image/<xmlid>/<field>/<int:max_width>x<int:max_height>',
  89. '/website/image/<model>/<id>/<field>',
  90. '/website/image/<model>/<id>/<field>/<int:max_width>x<int:max_height>',
  91. ], auth="public", website=True)
  92. def website_image(self, model=None, id=None, field=None, xmlid=None, max_width=None, max_height=None):
  93. """ Fetches the requested field and ensures it does not go above
  94. (max_width, max_height), resizing it if necessary.
  95. If the record is not found or does not have the requested field,
  96. returns a placeholder image via :meth:`~.placeholder`.
  97. Sets and checks conditional response parameters:
  98. * :mailheader:`ETag` is always set (and checked)
  99. * :mailheader:`Last-Modified is set iif the record has a concurrency
  100. field (``__last_update``)
  101. The requested field is assumed to be base64-encoded image data in
  102. all cases.
  103. xmlid can be used to load the image. But the field image must by base64-encoded
  104. """
  105. if xmlid and "." in xmlid:
  106. try:
  107. record = request.env['ir.model.data'].xmlid_to_object(xmlid)
  108. model, id = record._name, record.id
  109. except:
  110. raise werkzeug.exceptions.NotFound()
  111. env.ref
  112. if model == 'ir.attachment' and not field:
  113. if record.sudo().type == 'url':
  114. field = "url"
  115. else:
  116. field = "datas"
  117. elif model and id and field:
  118. idsha = id.split('_')
  119. try:
  120. id = idsha[0]
  121. except IndexError:
  122. raise werkzeug.exceptions.NotFound()
  123. else:
  124. raise werkzeug.exceptions.NotFound()
  125. response = werkzeug.wrappers.Response()
  126. return request.registry['website']._image(
  127. request.cr, request.uid, model, id, field, response, max_width, max_height)