main.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. #/#############################################################################
  3. #
  4. # BizzAppDev
  5. # Copyright (C) 2015-TODAY bizzappdev(<http://www.bizzappdev.com>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #/#############################################################################
  21. import os
  22. import re
  23. import urllib
  24. import urlparse
  25. import openerp
  26. from openerp.addons.web import http
  27. from openerp.addons.web.controllers.main import Binary
  28. from openerp.http import request, route
  29. from openerp.tools import config
  30. openerpweb = http
  31. def db_monodb(req):
  32. # if only one db exists, return it else return False
  33. return db_redirect(req, True)[0]
  34. def db_redirect(req, match_first_only_if_unique):
  35. db = False
  36. redirect = False
  37. # 1 try the db in the url
  38. db_url = req.params.get('db')
  39. if db_url:
  40. return (db_url, False)
  41. dbs = db_list(req, True)
  42. # 2 use the database from the cookie if it's listable and still listed
  43. cookie_db = req.httprequest.cookies.get('last_used_database')
  44. if cookie_db in dbs:
  45. db = cookie_db
  46. # 3 use the first db if user can list databases
  47. if dbs and not db and (not match_first_only_if_unique or len(dbs) == 1):
  48. db = dbs[0]
  49. # redirect to the chosen db if multiple are available
  50. if db and len(dbs) > 1:
  51. query = dict(urlparse.parse_qsl(
  52. req.httprequest.query_string, keep_blank_values=True))
  53. query.update({'db': db})
  54. redirect = req.httprequest.path + '?' + urllib.urlencode(query)
  55. return (db, redirect)
  56. def db_list(req, force=False):
  57. proxy = req.session.proxy("db")
  58. dbs = proxy.list(force)
  59. h = req.httprequest.environ['HTTP_HOST'].split(':')[0]
  60. d = h.split('.')[0]
  61. r = openerp.tools.config['dbfilter'].replace('%h', h).replace('%d', d)
  62. dbs = [i for i in dbs if re.match(r, i)]
  63. return dbs
  64. class mode(openerpweb.Controller):
  65. _cp_path = "/web/mode"
  66. @openerpweb.jsonrequest
  67. def get_mode(self, req, mode=False, db=False):
  68. if not db:
  69. dbs = db_list(req)
  70. db = dbs and dbs[0] or False
  71. return config.get(mode, {}).get(db, False)
  72. class cp_Binary(Binary):
  73. _cp_path = "/web/binary"
  74. @http.route([
  75. '/web/binary/company_logo',
  76. '/logo',
  77. '/logo.png',
  78. ], type='http', auth="none")
  79. def company_logo(self, dbname=None, **kw):
  80. req = request
  81. if req.session._db:
  82. dbname = req.session._db
  83. uid = req.session._uid
  84. elif dbname is None:
  85. dbname = db_monodb(req)
  86. mode = config.get('develop', {}).get(
  87. dbname, False) and 'develop' or False
  88. mode = not mode and config.get('test', {}).get(
  89. dbname, False) and 'test' or mode
  90. if not mode:
  91. return super(cp_Binary, self).company_logo(dbname=dbname, **kw)
  92. # TODO add etag, refactor to use /image code for etag
  93. module_path = list(os.path.split(__file__)[
  94. :-1]) + ["%s_logo.png" % mode]
  95. image = open(os.path.join(*module_path), 'r')
  96. image_data = image.read()
  97. image.close()
  98. headers = [
  99. ('Content-Type', 'image/png'),
  100. ('Content-Length', len(image_data)),
  101. ]
  102. return req.make_response(image_data, headers)
  103. class SwitchController(http.Controller):
  104. @route('/easy_switch_user/switch', type='json', auth="none")
  105. def switch(self, login, password):
  106. request = http.request
  107. uid = request.session.authenticate(request.db, login, password)
  108. if uid is False:
  109. raise Exception('Login Failed')
  110. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: