Ejemplo n.º 1
0
def test_scripts(args):
    """ Tries to launch standalone scripts tagged with @post_testing """
    # load the registry once for script discovery
    registry = flectra.registry(args.database)
    for module_name in registry._init_modules:
        # import tests for loaded modules
        flectra.tests.loader.get_test_modules(module_name)

    # fetch and filter scripts to test
    funcs = list(
        unique(func for tag in args.standalone.split(',')
               for func in standalone_tests[tag]))

    start_time = time.time()
    for index, func in enumerate(funcs, start=1):
        with flectra.api.Environment.manage():
            with flectra.registry(args.database).cursor() as cr:
                env = flectra.api.Environment(cr, flectra.SUPERUSER_ID, {})
                _logger.info("Executing standalone script: %s (%d / %d)",
                             func.__name__, index, len(funcs))
                try:
                    func(env)
                except Exception:
                    _logger.error("Standalone script %s failed",
                                  func.__name__,
                                  exc_info=True)

    _logger.info("%d standalone scripts executed in %.2fs" %
                 (len(funcs), time.time() - start_time))
Ejemplo n.º 2
0
 def load_test(module_name, idref, mode):
     cr.commit()
     try:
         _load_data(cr, module_name, idref, mode, 'test')
         return True
     except Exception:
         _test_logger.exception(
             'module %s: an exception occurred in a test', module_name)
         return False
     finally:
         if tools.config.options['test_commit']:
             cr.commit()
         else:
             cr.rollback()
             # avoid keeping stale xml_id, etc. in cache
             flectra.registry(cr.dbname).clear_caches()
Ejemplo n.º 3
0
    def _refresh_google_token_json(self, refresh_token, service):  # exchange_AUTHORIZATION vs Token (service = calendar)
        get_param = self.env['ir.config_parameter'].sudo().get_param
        client_id = get_param('google_%s_client_id' % (service,), default=False)
        client_secret = get_param('google_%s_client_secret' % (service,), default=False)

        if not client_id or not client_secret:
            raise UserError(_("The account for the Google service '%s' is not configured") % service)

        headers = {"content-type": "application/x-www-form-urlencoded"}
        data = {
            'refresh_token': refresh_token,
            'client_id': client_id,
            'client_secret': client_secret,
            'grant_type': 'refresh_token',
        }

        try:
            dummy, response, dummy = self._do_request(GOOGLE_TOKEN_ENDPOINT, params=data, headers=headers, type='POST', preuri='')
            return response
        except requests.HTTPError as error:
            if error.response.status_code == 400:  # invalid grant
                with registry(request.session.db).cursor() as cur:
                    self.env(cur)['res.users'].browse(self.env.uid).write({'google_%s_rtoken' % service: False})
            error_key = error.response.json().get("error", "nc")
            _logger.exception("Bad google request : %s !", error_key)
            error_msg = _("Something went wrong during your token generation. Maybe your Authorization Code is invalid or already expired [%s]") % error_key
            raise self.env['res.config.settings'].get_config_warning(error_msg)
Ejemplo n.º 4
0
    def _handle_exception(cls, exception):
        is_frontend_request = bool(getattr(request, 'is_frontend', False))
        if not is_frontend_request:
            # Don't touch non frontend requests exception handling
            return super(IrHttp, cls)._handle_exception(exception)
        try:
            response = super(IrHttp, cls)._handle_exception(exception)

            if isinstance(response, Exception):
                exception = response
            else:
                # if parent excplicitely returns a plain response, then we don't touch it
                return response
        except Exception as e:
            if 'werkzeug' in config['dev_mode']:
                raise e
            exception = e

        code, values = cls._get_exception_code_values(exception)

        if code is None:
            # Hand-crafted HTTPException likely coming from abort(),
            # usually for a redirect response -> return it directly
            return exception

        if not request.uid:
            cls._auth_method_public()

        # We rollback the current transaction before initializing a new
        # cursor to avoid potential deadlocks.

        # If the current (failed) transaction was holding a lock, the new
        # cursor might have to wait for this lock to be released further
        # down the line. However, this will only happen after the
        # request is done (and in fact it won't happen). As a result, the
        # current thread/worker is frozen until its timeout is reached.

        # So rolling back the transaction will release any potential lock
        # and, since we are in a case where an exception was raised, the
        # transaction shouldn't be committed in the first place.
        request.env.cr.rollback()

        with registry(request.env.cr.dbname).cursor() as cr:
            env = api.Environment(cr, request.uid, request.env.context)
            if code == 500:
                _logger.error("500 Internal Server Error:\n\n%s",
                              values['traceback'])
                values = cls._get_values_500_error(env, values, exception)
            elif code == 403:
                _logger.warning("403 Forbidden:\n\n%s", values['traceback'])
            elif code == 400:
                _logger.warning("400 Bad Request:\n\n%s", values['traceback'])
            try:
                code, html = cls._get_error_html(env, code, values)
            except Exception:
                code, html = 418, env['ir.ui.view']._render_template(
                    'http_routing.http_error', values)

        return werkzeug.wrappers.Response(
            html, status=code, content_type='text/html;charset=utf-8')
Ejemplo n.º 5
0
def test_full(args):
    """ Test full install/uninstall/reinstall cycle for all modules """
    with flectra.api.Environment.manage():
        with flectra.registry(args.database).cursor() as cr:
            env = flectra.api.Environment(cr, flectra.SUPERUSER_ID, {})

            def valid(module):
                return not (module.name in BLACKLIST
                            or module.name.startswith(IGNORE)
                            or module.state in ('installed', 'uninstallable'))

            modules = env['ir.module.module'].search([]).filtered(valid)

            # order modules in topological order
            modules = modules.browse(
                topological_sort({
                    module.id: module.dependencies_id.depend_id.ids
                    for module in modules
                }))
            modules_todo = [(module.id, module.name) for module in modules]

    resume = args.resume_at
    skip = set(args.skip.split(',')) if args.skip else set()
    for module_id, module_name in modules_todo:
        if module_name == resume:
            resume = None

        if resume or module_name in skip:
            install(args.database, module_id, module_name)
        else:
            cycle(args.database, module_id, module_name)
Ejemplo n.º 6
0
def check_session(session):
    with flectra.registry(session.db).cursor() as cr:
        self = flectra.api.Environment(cr, session.uid, {})['res.users'].browse(session.uid)
        if flectra.tools.misc.consteq(self._compute_session_token(session.sid), session.session_token):
            return True
        self._invalidate_session_cache()
        return False
Ejemplo n.º 7
0
    def log_xml(self, xml_string, func):
        self.ensure_one()

        if self.debug_logging:
            self.flush()
            db_name = self._cr.dbname

            # Use a new cursor to avoid rollback that could be caused by an upper method
            try:
                db_registry = registry(db_name)
                with db_registry.cursor() as cr:
                    env = api.Environment(cr, SUPERUSER_ID, {})
                    IrLogging = env['ir.logging']
                    IrLogging.sudo().create({
                        'name': 'delivery.carrier',
                        'type': 'server',
                        'dbname': db_name,
                        'level': 'DEBUG',
                        'message': xml_string,
                        'path': self.delivery_type,
                        'func': func,
                        'line': 1
                    })
            except psycopg2.Error:
                pass
Ejemplo n.º 8
0
def uninstall(db_name, module_id, module_name):
    with flectra.api.Environment.manage():
        with flectra.registry(db_name).cursor() as cr:
            env = flectra.api.Environment(cr, flectra.SUPERUSER_ID, {})
            module = env['ir.module.module'].browse(module_id)
            module.button_immediate_uninstall()
    _logger.info('%s uninstalled', module_name)
Ejemplo n.º 9
0
 def flectra_execute(self, dbname, model, method, args, kwargs=None):
     kwargs = kwargs or {}
     db = flectra.sql_db.db_connect(dbname)
     flectra.registry(dbname).check_signaling()
     with flectra.api.Environment.manage(), db.cursor() as cr:
         try:
             _logger.debug('%s: %s %s', method, args, kwargs)
             env = flectra.api.Environment(cr, SUPERUSER_ID, {})
             getattr(env[model], method)(*args, **kwargs)
         except:
             _logger.error(
                 'Error while executing method=%s, args=%s, kwargs=%s',
                 method,
                 args,
                 kwargs,
                 exc_info=True)
Ejemplo n.º 10
0
    def _dispatch(cls):
        """
        In case of rerouting for translate (e.g. when visiting flectrahq.com/fr_BE/),
        _dispatch calls reroute() that returns _dispatch with altered request properties.
        The second _dispatch will continue until end of process. When second _dispatch is finished, the first _dispatch
        call receive the new altered request and continue.
        At the end, 2 calls of _dispatch (and this override) are made with exact same request properties, instead of one.
        As the response has not been sent back to the client, the visitor cookie does not exist yet when second _dispatch call
        is treated in _handle_webpage_dispatch, leading to create 2 visitors with exact same properties.
        To avoid this, we check if, !!! before calling super !!!, we are in a rerouting request. If not, it means that we are
        handling the original request, in which we should create the visitor. We ignore every other rerouting requests.
        """
        is_rerouting = hasattr(request, 'routing_iteration')

        if request.session.db:
            reg = registry(request.session.db)
            with reg.cursor() as cr:
                env = api.Environment(cr, SUPERUSER_ID, {})
                request.website_routing = env['website'].get_current_website(
                ).id

        response = super(Http, cls)._dispatch()

        if not is_rerouting:
            cls._register_website_track(response)
        return response
Ejemplo n.º 11
0
 def count_database(self, database):
     with flectra.api.Environment.manage():
         registry = flectra.registry(config['db_name'])
         with registry.cursor() as cr:
             uid = flectra.SUPERUSER_ID
             env = flectra.api.Environment(cr, uid, {})
             self.count_env(env)
Ejemplo n.º 12
0
def environment():
    """ Return an environment with a new cursor for the current database; the
        cursor is committed and closed after the context block.
    """
    registry = flectra.registry(common.get_db_name())
    with registry.cursor() as cr:
        yield flectra.api.Environment(cr, ADMIN_USER_ID, {})
Ejemplo n.º 13
0
 def __getattr__(self, name):
     cr = self._cursor
     if cr is None:
         from flectra import registry
         cr = self._cursor = registry(self.dbname).cursor()
         for _ in range(self._depth):
             cr.__enter__()
     return getattr(cr, name)
Ejemplo n.º 14
0
def authenticate(token):
    try:
        a = 4 - len(token) % 4
        if a != 0:
            token += '==' if a == 2 else '='
        SERVER, db, login, uid, ts = base64.urlsafe_b64decode(
            str(token).encode()).decode().split(',')
        if int(ts) + 60 * 60 * 24 * 7 * 10 < time.time():
            return False
        # todo verify
        # registry = RegistryManager.get(db)
        flectra.registry(db).check_signaling()
        cr = flectra.registry(db).cursor()
        env = api.Environment(cr, int(uid), {})
    except Exception as e:
        return str(e)
    return env
Ejemplo n.º 15
0
def environment():
    """ Return an environment with a new cursor for the current database; the
        cursor is committed and closed after the context block.
    """
    reg = registry(common.get_db_name())
    with reg.cursor() as cr:
        yield api.Environment(cr, SUPERUSER_ID, {})
        cr.commit()
Ejemplo n.º 16
0
 def called_after():
     db_registry = registry(dbname)
     with api.Environment.manage(), db_registry.cursor() as cr:
         env = api.Environment(cr, uid, context)
         try:
             func(self.with_env(env), *args, **kwargs)
         except Exception as e:
             _logger.warning("Could not sync record now: %s" % self)
             _logger.exception(e)
Ejemplo n.º 17
0
def create_log_record(**kwargs):
    test_mode = request.registry.test_cr
    # don't create log in test mode as it's impossible in case of error in sql
    # request (we cannot use second cursor and we cannot use aborted
    # transaction)
    if not test_mode:
        with flectra.registry(request.session.db).cursor() as cr:
            # use new to save data even in case of an error in the old cursor
            env = flectra.api.Environment(cr, request.session.uid, {})
            _create_log_record(env, **kwargs)
Ejemplo n.º 18
0
def execute(db, uid, obj, method, *args, **kw):
    threading.currentThread().dbname = db
    with flectra.registry(db).cursor() as cr:
        check_method_name(method)
        res = execute_cr(cr, uid, obj, method, *args, **kw)
        if res is None:
            _logger.info(
                'The method %s of the object %s can not return `None` !',
                method, obj)
        return res
Ejemplo n.º 19
0
    def create(self, vals):
        if self.env.context and self.env.context.get('bve'):
            vals['state'] = 'base'
        res = super(IrModel, self).create(vals)

        # this sql update is necessary since a write method here would
        # be not working (an orm constraint is restricting the modification
        # of the state field while updating ir.model)
        q = "UPDATE ir_model SET state = 'manual' WHERE id = %s"
        self.env.cr.execute(q, (res.id, ))

        # # update registry
        if self.env.context.get('bve'):
            # setup models; this reloads custom models in registry
            self.pool.setup_models(self._cr)

            # signal that registry has changed
            registry(self.env.cr.dbname).signal_changes()

        return res
Ejemplo n.º 20
0
 def poll(self, channels, last, options=None):
     if request.env.user.has_group('base.group_user'):
         ip_address = request.httprequest.remote_addr
         users_log = request.env['res.users.log'].search_count([
             ('create_uid', '=', request.env.user.id),
             ('ip', '=', ip_address),
             ('create_date', '>=', Datetime.to_string(Datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)))])
         if not users_log:
             with registry(request.env.cr.dbname).cursor() as cr:
                 env = Environment(cr, request.env.user.id, {})
                 env['res.users.log'].create({'ip': ip_address})
     return super(BusController, self).poll(channels, last, options=options)
Ejemplo n.º 21
0
    def _do_mark_done(self):
        active_ids = self.env.context.get('active_ids', False)

        with api.Environment.manage():
            new_cr = registry(self._cr.dbname).cursor()
            self = self.with_env(self.env(cr=new_cr))
            productions = self.env['mrp.production'].browse(active_ids)
            for production in productions:
                if production.check_to_done:
                    production.button_mark_done()
            new_cr.commit()
            new_cr.close()
Ejemplo n.º 22
0
 def receive(self, req):
     """ End-point to receive mail from an external SMTP server. """
     dbs = req.jsonrequest.get('databases')
     for db in dbs:
         message = base64.b64decode(dbs[db])
         try:
             db_registry = registry(db)
             with db_registry.cursor() as cr:
                 env = api.Environment(cr, SUPERUSER_ID, {})
                 env['mail.thread'].message_process(None, message)
         except psycopg2.Error:
             pass
     return True
Ejemplo n.º 23
0
def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False,
               attachments=None, message_id=None, references=None, openobject_id=False, debug=False, subtype='plain', headers=None,
               smtp_server=None, smtp_port=None, ssl=False, smtp_user=None, smtp_password=None, cr=None, uid=None):
    """Low-level function for sending an email (deprecated).

    :deprecate: since OpenERP 6.1, please use ir.mail_server.send_email() instead.
    :param email_from: A string used to fill the `From` header, if falsy,
                       config['email_from'] is used instead.  Also used for
                       the `Reply-To` header if `reply_to` is not provided
    :param email_to: a sequence of addresses to send the mail to.
    """

    # If not cr, get cr from current thread database
    local_cr = None
    if not cr:
        db_name = getattr(threading.currentThread(), 'dbname', None)
        if db_name:
            local_cr = cr = flectra.registry(db_name).cursor()
        else:
            raise Exception("No database cursor found, please pass one explicitly")

    # Send Email
    try:
        mail_server_pool = flectra.registry(cr.dbname)['ir.mail_server']
        res = False
        # Pack Message into MIME Object
        email_msg = mail_server_pool.build_email(email_from, email_to, subject, body, email_cc, email_bcc, reply_to,
                   attachments, message_id, references, openobject_id, subtype, headers=headers)

        res = mail_server_pool.send_email(cr, uid or 1, email_msg, mail_server_id=None,
                       smtp_server=smtp_server, smtp_port=smtp_port, smtp_user=smtp_user, smtp_password=smtp_password,
                       smtp_encryption=('ssl' if ssl else None), smtp_debug=debug)
    except Exception:
        _logger.exception("tools.email_send failed to deliver email")
        return False
    finally:
        if local_cr:
            cr.close()
    return res
Ejemplo n.º 24
0
def test_uninstall(args):
    """ Tries to uninstall/reinstall one ore more modules"""
    domain = [('name', 'in', args.uninstall.split(',')),
              ('state', '=', 'installed')]
    with flectra.api.Environment.manage():
        with flectra.registry(args.database).cursor() as cr:
            env = flectra.api.Environment(cr, flectra.SUPERUSER_ID, {})
            modules = env['ir.module.module'].search(domain)
            modules_todo = [(module.id, module.name) for module in modules]

    for module_id, module_name in modules_todo:
        uninstall(args.database, module_id, module_name)
        install(args.database, module_id, module_name)
Ejemplo n.º 25
0
    def setUp(self):
        # check that the registry is properly reset
        registry = flectra.registry()
        fnames = set(registry[self.MODEL]._fields)

        @self.addCleanup
        def check_registry():
            assert set(registry[self.MODEL]._fields) == fnames

        super(TestCustomFields, self).setUp()

        # use a test cursor instead of a real cursor
        self.registry.enter_test_mode(self.cr)
        self.addCleanup(self.registry.leave_test_mode)
Ejemplo n.º 26
0
    def oauth2callback(self, **kw):
        """ This route/function is called by Google when user Accept/Refuse the consent of Google """
        state = json.loads(kw['state'])
        dbname = state.get('d')
        service = state.get('s')
        url_return = state.get('f')

        with registry(dbname).cursor() as cr:
            if kw.get('code'):
                request.env(cr, request.session.uid)['google.%s' % service].set_all_tokens(kw['code'])
                return redirect(url_return)
            elif kw.get('error'):
                return redirect("%s%s%s" % (url_return, "?error=", kw['error']))
            else:
                return redirect("%s%s" % (url_return, "?error=Unknown_error"))
Ejemplo n.º 27
0
    def poll(self, dbname, channels, last, options=None, timeout=TIMEOUT):
        if options is None:
            options = {}
        # Dont hang ctrl-c for a poll request, we need to bypass private
        # attribute access because we dont know before starting the thread that
        # it will handle a longpolling request
        if not flectra.evented:
            current = threading.current_thread()
            current._daemonic = True
            # rename the thread to avoid tests waiting for a longpolling
            current.setName("openerp.longpolling.request.%s" % current.ident)

        registry = flectra.registry(dbname)

        # immediatly returns if past notifications exist
        with registry.cursor() as cr:
            env = api.Environment(cr, SUPERUSER_ID, {})
            notifications = env['bus.bus'].poll(channels, last, options)

        # immediatly returns in peek mode
        if options.get('peek'):
            return dict(notifications=notifications, channels=channels)

        # or wait for future ones
        if not notifications:
            if not self.started:
                # Lazy start of events listener
                self.start()

            event = self.Event()
            for channel in channels:
                self.channels.setdefault(hashable(channel), set()).add(event)
            try:
                event.wait(timeout=timeout)
                with registry.cursor() as cr:
                    env = api.Environment(cr, SUPERUSER_ID, {})
                    notifications = env['bus.bus'].poll(
                        channels, last, options)
            except Exception:
                # timeout
                pass
            finally:
                # gc pointers to event
                for channel in channels:
                    channel_events = self.channels.get(hashable(channel))
                    if channel_events and event in channel_events:
                        channel_events.remove(event)
        return notifications
Ejemplo n.º 28
0
    def oauth2callback(self, **kw):
        """ This route/function is called by Google when user Accept/Refuse the consent of Google """
        state = json.loads(kw['state'])
        dbname = state.get('d')
        service = state.get('s')
        url_return = state.get('f')

        with registry(dbname).cursor() as cr:
            if kw.get('code'):
                access_token, refresh_token, ttl = request.env['google.service']._get_google_tokens(kw['code'], service)
                # LUL TODO only defined in google_calendar
                request.env.user._set_auth_tokens(access_token, refresh_token, ttl)
                return redirect(url_return)
            elif kw.get('error'):
                return redirect("%s%s%s" % (url_return, "?error=", kw['error']))
            else:
                return redirect("%s%s" % (url_return, "?error=Unknown_error"))
Ejemplo n.º 29
0
    def run_scheduler(self, use_new_cursor=False, company_id=False):
        """ Call the scheduler in order to check the running procurements (super method), to check the minimum stock rules
        and the availability of moves. This function is intended to be run for all the companies at the same time, so
        we run functions as SUPERUSER to avoid intercompanies and access rights issues. """
        try:
            if use_new_cursor:
                cr = registry(self._cr.dbname).cursor()
                self = self.with_env(self.env(cr=cr))  # TDE FIXME

            self._run_scheduler_tasks(use_new_cursor=use_new_cursor, company_id=company_id)
        finally:
            if use_new_cursor:
                try:
                    self._cr.close()
                except Exception:
                    pass
        return {}
Ejemplo n.º 30
0
    def setUp(self):
        # check that the registry is properly reset
        registry = flectra.registry()
        fnames = set(registry[self.MODEL]._fields)

        @self.addCleanup
        def check_registry():
            assert set(registry[self.MODEL]._fields) == fnames

        super(TestCustomFields, self).setUp()

        # use a test cursor instead of a real cursor
        self.registry.enter_test_mode()
        self.addCleanup(self.registry.leave_test_mode)

        # do not reload the registry after removing a field
        self.env = self.env(context={'_force_unlink': True})