<commit_before>#----------------------------------------------------------
# ir_http modular http routing
#----------------------------------------------------------
import logging

import werkzeug.exceptions
import werkzeug.routing

import openerp
from openerp import http
from openerp.http import request
from openerp.osv import osv

_logger = logging.getLogger(__name__)

class RequestUID(object):
    pass

class ModelConverter(werkzeug.routing.BaseConverter):

    def __init__(self, url_map, model=False):
        super(ModelConverter, self).__init__(url_map)
        self.model = model
        # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
        self.regex = '([0-9]+)'

    def to_python(self, value):
        # TODO:
        # - raise routing.ValidationError() if no browse record can be createdm
        # - support slug 
        return request.registry[self.model].browse(request.cr, RequestUID(), int(value), context=request.context)

    def to_url(self, value):
        return value.id

class ModelsConverter(werkzeug.routing.BaseConverter):

    def __init__(self, url_map, model=False):
        super(ModelsConverter, self).__init__(url_map)
        self.model = model
        # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
        self.regex = '([0-9,]+)'

    def to_python(self, value):
        # TODO:
        # - raise routing.ValidationError() if no browse record can be createdm
        # - support slug
        return request.registry[self.model].browse(request.cr, RequestUID(), [int(i) for i in value.split(',')], context=request.context)

    def to_url(self, value):
        return ",".join([i.id for i in value])

class ir_http(osv.AbstractModel):
    _name = 'ir.http'
    
    _description = "HTTP routing"

    def _find_handler(self):
        # TODO move to __init__(self, registry, cr)
        if not hasattr(self, 'routing_map'):
            _logger.info("Generating routing map")
            cr = request.cr
            m = request.registry.get('ir.module.module')
            ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')])
            installed = set(x['name'] for x in m.read(cr, 1, ids, ['name']))
            mods = ['', "web"] + sorted(installed)
            self.routing_map = http.routing_map(mods, False, converters={'model': ModelConverter, 'models': ModelsConverter})

        # fallback to non-db handlers
        path = request.httprequest.path
        urls = self.routing_map.bind_to_environ(request.httprequest.environ)
<commit_msg>[FIX] make routing converters modular<commit_after>#----------------------------------------------------------
# ir_http modular http routing
#----------------------------------------------------------
import logging

import werkzeug.exceptions
import werkzeug.routing

import openerp
from openerp import http
from openerp.http import request
from openerp.osv import osv

_logger = logging.getLogger(__name__)

class RequestUID(object):
    pass

class ModelConverter(werkzeug.routing.BaseConverter):

    def __init__(self, url_map, model=False):
        super(ModelConverter, self).__init__(url_map)
        self.model = model
        # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
        self.regex = '([0-9]+)'

    def to_python(self, value):
        # TODO:
        # - raise routing.ValidationError() if no browse record can be createdm
        # - support slug 
        return request.registry[self.model].browse(request.cr, RequestUID(), int(value), context=request.context)

    def to_url(self, value):
        return value.id

class ModelsConverter(werkzeug.routing.BaseConverter):

    def __init__(self, url_map, model=False):
        super(ModelsConverter, self).__init__(url_map)
        self.model = model
        # TODO add support for slug in the form [A-Za-z0-9-] bla-bla-89 -> id 89
        self.regex = '([0-9,]+)'

    def to_python(self, value):
        # TODO:
        # - raise routing.ValidationError() if no browse record can be createdm
        # - support slug
        return request.registry[self.model].browse(request.cr, RequestUID(), [int(i) for i in value.split(',')], context=request.context)

    def to_url(self, value):
        return ",".join([i.id for i in value])

class ir_http(osv.AbstractModel):
    _name = 'ir.http'
    
    _description = "HTTP routing"

    def _get_converters(self):
        return {'model': ModelConverter, 'models': ModelsConverter}

    def _find_handler(self):
        # TODO move to __init__(self, registry, cr)
        if not hasattr(self, 'routing_map'):
            _logger.info("Generating routing map")
            cr = request.cr
            m = request.registry.get('ir.module.module')
            ids = m.search(cr, openerp.SUPERUSER_ID, [('state', '=', 'installed'), ('name', '!=', 'web')])
            installed = set(x['name'] for x in m.read(cr, 1, ids, ['name']))
            mods = ['', "web"] + sorted(installed)
            self.routing_map = http.routing_map(mods, False, converters=self._get_converters())

        # fallback to non-db handlers
        path = request.httprequest.path
        urls = self.routing_map.bind_to_environ(request.httprequest.environ)

{'fraction_non_alphanumeric': 0.16527355623100304, 'fraction_numerical': 0.006458966565349544, 'mean_word_length': 2.4615384615384617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6867240', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1563, 'n_words': 400}