Skip to Content
Menu
This question has been flagged

HI Guys im trying to migrate the old web/binary/saveas controller from v8 into v10 since id like to have access to my attachments via website url and attachment_id like this: http://localhost/8069/web/binary/saveas?filename_field=datas_fname&field=datas&model=ir.attachment&id=220


Heres my code:

class Binary(http.Controller):

    @http.route('/web/binary/saveas', type='http', auth="public") @serialize_exception def saveas(self, model, field, id=None, filename_field=None, context=None, **kw):

        """ Download link for files stored as binary fields.

        If the ``id`` parameter is omitted, fetches the default value for the binary field (via ``default_get``), otherwise fetches the field for that precise record.

        :param str model: name of the model to fetch the binary from :param str field: binary field :param str id: id of the record from which to fetch the binary :param str filename_field: field holding the file's name, if any :returns: :class:`werkzeug.wrappers.Response` """ if context is None:

            context = {} Model = request.env[model] cr, uid, context = request.cr, request.uid, request.context fields = [field] content_type = 'application/octet-stream' if filename_field:

            fields.append(filename_field) if id:

            fields.append('file_type') res = Model.read([int(id)], fields) if res.get('file_type'):

                content_type = res['file_type'] else:

            res = Model.default_get(fields) filecontent = base64.b64decode(res.get(field) or '') if not filecontent:

            return request.not_found() else:

            filename = '%s_%s' % (model.replace('.', '_'), id) if filename_field:

                filename = res.get(filename_field, '') or filename return request.make_response( filecontent, [('Content-Type', content_type), ('Content-Disposition', content_disposition(filename))])


Heres the Error I get:

> /opt/odoo/10/custom/testing/website_doc/controllers/main.py(93)saveas()-> if res.get('file_type'): (Pdb) nextTraceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 508, in handle_one_response self.run_application() File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 494, in run_application self.result = self.application(self.environ, self.start_response) File "/opt/odoo/10/srv/odoo/service/wsgi_server.py", line 184, in application return application_unproxied(environ, start_response) File "/opt/odoo/10/srv/odoo/service/wsgi_server.py", line 170, in application_unproxied result = handler(environ, start_response) File "/opt/odoo/10/srv/odoo/http.py", line 1306, in __call__ return self.dispatch(environ, start_response) File "/opt/odoo/10/srv/odoo/http.py", line 1280, in __call__ return self.app(environ, start_wrapped) File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 588, in __call__ return self.app(environ, start_response) File "/opt/odoo/10/srv/odoo/http.py", line 1471, in dispatch result = ir_http._dispatch() File "/opt/odoo/10/srv/addons/website_sale/models/ir_http.py", line 15, in _dispatch return super(IrHttp, cls)._dispatch() File "/opt/odoo/10/srv/addons/website/models/ir_http.py", line 209, in _dispatch resp = super(Http, cls)._dispatch() File "/opt/odoo/10/srv/addons/web_editor/models/ir_http.py", line 21, in _dispatch return super(IrHttp, cls)._dispatch() File "/opt/odoo/10/srv/addons/utm/models/ir_http.py", line 20, in _dispatch response = super(IrHttp, cls)._dispatch() File "/opt/odoo/10/srv/odoo/addons/base/ir/ir_http.py", line 199, in _dispatch return cls._handle_exception(e) File "/opt/odoo/10/srv/addons/website/models/ir_http.py", line 261, in _handle_exception return super(Http, cls)._handle_exception(exception) File "/opt/odoo/10/srv/odoo/addons/base/ir/ir_http.py", line 169, in _handle_exception return request._handle_exception(exception) File "/opt/odoo/10/srv/odoo/http.py", line 766, in _handle_exception return super(HttpRequest, self)._handle_exception(exception) File "/opt/odoo/10/srv/odoo/addons/base/ir/ir_http.py", line 195, in _dispatch result = request.dispatch() File "/opt/odoo/10/srv/odoo/http.py", line 825, in dispatch r = self._call_function(**self.params) File "/opt/odoo/10/srv/odoo/http.py", line 331, in _call_function return checked_call(self.db, *args, **kwargs) File "/opt/odoo/10/srv/odoo/service/model.py", line 119, in wrapper return f(dbname, *args, **kwargs) File "/opt/odoo/10/srv/odoo/http.py", line 324, in checked_call result = self.endpoint(*a, **kw) File "/opt/odoo/10/srv/odoo/http.py", line 933, in __call__ return self.method(*args, **kw) File "/opt/odoo/10/srv/odoo/http.py", line 504, in response_wrap response = f(*args, **kw) File "/opt/odoo/10/custom/testing/website_doc/controllers/main.py", line 93, in saveas if res.get('file_type'): AttributeError: 'list' object has no attribute 'get'{'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch', 'HTTP_ACCEPT_LANGUAGE': 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'website_lang=en_US; _ga=GA1.1.1642433694.1483291085; planner_crm_last_page=Welcome0; session_id=5a17190397517d6fe8be8de0e1b84a301622e4c9; planner_website_last_page=Welcome0', 'HTTP_DNT': '1', 'HTTP_HOST': 'hp1:8020', 'HTTP_REFERER': 'http://hp1:8020/doc/how-to/formulare-711/externe-formulare-492', 'HTTP_UPGRADE_INSECURE_REQUESTS': '1', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', 'PATH_INFO': '/web/binary/saveas', 'QUERY_STRING': 'filename_field=datas_fname&field=datas&model=ir.attachment&id=222', 'REMOTE_ADDR': '192.168.0.10', 'REMOTE_PORT': '49785', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': 'hp1.don-it-service.de', 'SERVER_PORT': '8020', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7', 'werkzeug.request': <Request 'http://hp1:8020/web/binary/saveas?filename_field=datas_fname&field=datas&model=ir.attachment&id=222' [GET]>, 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7fb3775cf1e0>, 'wsgi.input': <gevent.pywsgi.Input object at 0x7fb36519f0d0>, 'wsgi.multiprocess': False, 'wsgi.multithread': False, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)} failed with AttributeError 192.168.0.10 - - [2017-01-06 11:32:15] "GET /web/binary/saveas?filename_field=datas_fname&field=datas&model=ir.attachment&id=222 HTTP/1.1" 500 161 4.483348


can anyone provide a hint on what im doing wrong?



Best Regards

Eugen


Avatar
Discard
Related Posts Replies Views Activity
0
Mar 15
7028
0
Mar 15
4431
1
Jul 24
2791
1
Dec 23
51857
1
May 21
8400