From 44569c06a2ccee306825468c27d789c7eeb9e1fe Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Tue, 23 Apr 2013 23:06:44 +0200 Subject: [PATCH] [FIX] http controllers inheritance minimalist and backward compatible implementation bzr revid: al@openerp.com-20130423210644-hmvglhh10zlrwp9q --- addons/web/controllers/main.py | 10 +++++----- addons/web/http.py | 25 ++++++++++++++++++++----- addons/web_diagram/controllers/main.py | 2 +- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 01126694059..01624ceb1ce 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1439,7 +1439,7 @@ class Action(openerpweb.Controller): else: return False -class Export(View): +class Export(openerpweb.Controller): _cp_path = "/web/export" @openerpweb.jsonrequest @@ -1580,7 +1580,7 @@ class Export(View): (prefix + '/' + k, prefix_string + '/' + v) for k, v in self.fields_info(req, model, export_fields).iteritems()) - #noinspection PyPropertyDefinition +class ExportFormat(object): @property def content_type(self): """ Provides the format's content type """ @@ -1628,7 +1628,7 @@ class Export(View): ('Content-Type', self.content_type)], cookies={'fileToken': int(token)}) -class CSVExport(Export): +class CSVExport(ExportFormat, http.Controller): _cp_path = '/web/export/csv' fmt = {'tag': 'csv', 'label': 'CSV'} @@ -1663,7 +1663,7 @@ class CSVExport(Export): fp.close() return data -class ExcelExport(Export): +class ExcelExport(ExportFormat, http.Controller): _cp_path = '/web/export/xls' fmt = { 'tag': 'xls', @@ -1702,7 +1702,7 @@ class ExcelExport(Export): fp.close() return data -class Reports(View): +class Reports(openerpweb.Controller): _cp_path = "/web/report" POLLING_DELAY = 0.25 TYPES_MAPPING = { diff --git a/addons/web/http.py b/addons/web/http.py index d12cdfb13a1..f56778ea1ba 100644 --- a/addons/web/http.py +++ b/addons/web/http.py @@ -356,17 +356,31 @@ def httprequest(f): addons_module = {} addons_manifest = {} controllers_class = [] +controllers_class_path = {} controllers_object = {} +controllers_object_path = {} controllers_path = {} class ControllerType(type): def __init__(cls, name, bases, attrs): super(ControllerType, cls).__init__(name, bases, attrs) - controllers_class.append(("%s.%s" % (cls.__module__, cls.__name__), cls)) + name_class = ("%s.%s" % (cls.__module__, cls.__name__), cls) + controllers_class.append(name_class) + path = attrs.get('_cp_path') + if path not in controllers_class_path: + controllers_class_path[path] = name_class class Controller(object): __metaclass__ = ControllerType + def __new__(cls, *args, **kwargs): + subclasses = [c for c in cls.__subclasses__() if c._cp_path == cls._cp_path] + if subclasses: + name = "%s (extended by %s)" % (cls.__name__, ', '.join(sub.__name__ for sub in subclasses)) + cls = type(name, tuple(reversed(subclasses)), {}) + + return object.__new__(cls) + #---------------------------------------------------------- # Session context manager #---------------------------------------------------------- @@ -566,10 +580,11 @@ class Root(object): addons_manifest[module] = manifest self.statics['/%s/static' % module] = path_static - for k, v in controllers_class: - if k not in controllers_object: - o = v() - controllers_object[k] = o + for k, v in controllers_class_path.items(): + if k not in controllers_object_path and hasattr(v[1], '_cp_path'): + o = v[1]() + controllers_object[v[0]] = o + controllers_object_path[k] = o if hasattr(o, '_cp_path'): controllers_path[o._cp_path] = o diff --git a/addons/web_diagram/controllers/main.py b/addons/web_diagram/controllers/main.py index c598a7e4652..3f3c1065504 100644 --- a/addons/web_diagram/controllers/main.py +++ b/addons/web_diagram/controllers/main.py @@ -1,6 +1,6 @@ import openerp -class DiagramView(openerp.addons.web.controllers.main.View): +class DiagramView(openerp.addons.web.http.Controller): _cp_path = "/web_diagram/diagram" @openerp.addons.web.http.jsonrequest