Example #1
0
    def _frontend_pre_dispatch(cls):
        super()._frontend_pre_dispatch()

        if not request.context.get('tz'):
            with contextlib.suppress(pytz.UnknownTimeZoneError):
                tz = request.geoip.get('time_zone', '')
                request.update_context(tz=pytz.timezone(tz).zone)

        website = request.env['website'].get_current_website()
        user = request.env.user

        # This is mainly to avoid access errors in website controllers
        # where there is no context (eg: /shop), and it's not going to
        # propagate to the global context of the tab. If the company of
        # the website is not in the allowed companies of the user, set
        # the main company of the user.
        website_company_id = website._get_cached('company_id')
        if user.id == website._get_cached('user_id'):
            # avoid a read on res_company_user_rel in case of public user
            allowed_company_ids = [website_company_id]
        elif website_company_id in user._get_company_ids():
            allowed_company_ids = [website_company_id]
        else:
            allowed_company_ids = user.company_id.ids

        request.update_context(
            allowed_company_ids=allowed_company_ids,
            website_id=website.id,
            **cls._get_web_editor_context(),
        )

        request.website = website.with_context(request.context)
Example #2
0
    def button_choose_theme(self):
        """
            Remove any existing theme on the current website and install the theme ``self`` instead.

            The actual loading of the theme on the current website will be done
            automatically on ``write`` thanks to the upgrade and/or install.

            When installating a new theme, upgrade the upstream chain first to make sure
            we have the latest version of the dependencies to prevent inconsistencies.

            :return: dict with the next action to execute
        """
        self.ensure_one()
        website = self.env['website'].get_current_website()

        self._theme_remove(website)

        # website.theme_id must be set before upgrade/install to trigger the load in ``write``
        website.theme_id = self

        # this will install 'self' if it is not installed yet
        if request:
            request.update_context(apply_new_theme=True)
        self._theme_upgrade_upstream()

        active_todo = self.env['ir.actions.todo'].search(
            [('state', '=', 'open')], limit=1)
        result = None
        if active_todo:
            result = active_todo.action_launch()
        else:
            result = website.button_go_website(mode_edit=True)
        return result
Example #3
0
    def web_totp(self, redirect=None, **kwargs):
        if request.session.uid:
            return request.redirect(self._login_redirect(request.session.uid, redirect=redirect))

        if not request.session.pre_uid:
            return request.redirect('/web/login')

        error = None

        user = request.env['res.users'].browse(request.session.pre_uid)
        if user and request.httprequest.method == 'GET':
            cookies = request.httprequest.cookies
            key = cookies.get(TRUSTED_DEVICE_COOKIE)
            if key:
                checked_credentials = request.env['auth_totp.device']._check_credentials(scope="browser", key=key)
                if checked_credentials == user.id:
                    request.session.finalize(request.env)
                    return request.redirect(self._login_redirect(request.session.uid, redirect=redirect))

        elif user and request.httprequest.method == 'POST' and kwargs.get('totp_token'):
            try:
                with user._assert_can_auth():
                    user._totp_check(int(re.sub(r'\s', '', kwargs['totp_token'])))
            except AccessDenied as e:
                error = str(e)
            except ValueError:
                error = _("Invalid authentication code format.")
            else:
                request.session.finalize(request.env)
                request.update_env(user=request.session.uid)
                request.update_context(**request.session.context)
                response = request.redirect(self._login_redirect(request.session.uid, redirect=redirect))
                if kwargs.get('remember'):
                    name = _("%(browser)s on %(platform)s",
                        browser=request.httprequest.user_agent.browser.capitalize(),
                        platform=request.httprequest.user_agent.platform.capitalize(),
                    )
                    geoip = request.geoip
                    if geoip:
                        name += " (%s, %s)" % (geoip['city'], geoip['country_name'])

                    key = request.env['auth_totp.device']._generate("browser", name)
                    response.set_cookie(
                        key=TRUSTED_DEVICE_COOKIE,
                        value=key,
                        max_age=TRUSTED_DEVICE_AGE,
                        httponly=True,
                        samesite='Lax'
                    )
                # Crapy workaround for unupdatable Odoo Mobile App iOS (Thanks Apple :@)
                request.session.touch()
                return response

        # Crapy workaround for unupdatable Odoo Mobile App iOS (Thanks Apple :@)
        request.session.touch()
        return request.render('auth_totp.auth_totp_form', {
            'user': user,
            'error': error,
            'redirect': redirect,
        })
Example #4
0
 def portal_message_fetch(self,
                          res_model,
                          res_id,
                          domain=False,
                          limit=False,
                          offset=False,
                          **kw):
     # add 'rating_include' in context, to fetch them in portal_message_format
     if kw.get('rating_include'):
         request.update_context(rating_include=True)
     result = super(PortalChatter, self).portal_message_fetch(res_model,
                                                              res_id,
                                                              domain=domain,
                                                              limit=limit,
                                                              offset=offset,
                                                              **kw)
     result.update(self._portal_rating_stats(res_model, res_id, **kw))
     return result
Example #5
0
    def load(self, action_id, additional_context=None):
        Actions = request.env['ir.actions.actions']
        value = False
        try:
            action_id = int(action_id)
        except ValueError:
            try:
                action = request.env.ref(action_id)
                assert action._name.startswith('ir.actions.')
                action_id = action.id
            except Exception:
                action_id = 0  # force failed read

        base_action = Actions.browse([action_id]).sudo().read(['type'])
        if base_action:
            action_type = base_action[0]['type']
            if action_type == 'ir.actions.report':
                request.update_context(bin_size=True)
            if additional_context:
                request.update_context(**additional_context)
            action = request.env[action_type].sudo().browse([action_id]).read()
            if action:
                value = clean_action(action[0], env=request.env)
        return value
Example #6
0
 def _frontend_pre_dispatch(cls):
     request.update_context(lang=request.lang._get_cached('code'))
     if request.httprequest.cookies.get('frontend_lang') != request.lang._get_cached('code'):
         request.future_response.set_cookie('frontend_lang', request.lang._get_cached('code'))
Example #7
0
 def _pre_dispatch(cls, rule, args):
     super()._pre_dispatch(rule, args)
     ctx = cls._get_web_editor_context()
     request.update_context(**ctx)