Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
6 ตอบกลับ
8309 มุมมอง

Hello, I have an Odoo server on odoo.sh and want to execute some pieces of code only when running there. Is there a way I can check, on a python method, if the server is running on production, locally, stagging, etc?

Thank you!

อวตาร
ละทิ้ง

May i know ,what is your purpose for doing this ?

ผู้เขียน

I my server I have, for example a method that makes a request to updates the invontory of our ecommerce. Something like:

def sync_inventory(self):

stock = function_that_get_the_current_stock()

update_ecommerce_stock(stock)

What I need I a way of doing something like:

def sync_inventory(self):

stock = function_that_get_the_current_stock()

if verify_if_server_is_on_production():

update_ecommerce_stock(stock)

Was I clear?

ผู้เขียน

Sorry about the bad indentation... I couln't tab and the spaces I input didn't work...

คำตอบที่ดีที่สุด

Following workround might be helpful to check if is "production" or "local":

def _check_environment_is_production(self):
web_base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
localhost_names = ['localhost']
return False if not web_base_url or any(x in web_base_url for x in localhost_names) else True
อวตาร
ละทิ้ง

Please note that when exporting a backup of production from odoo.sh, for me at least this test on my dev machine still returns TRUE. And unfortunately the parameter ODOO_STAGE suggeted by https://www.odoo.sh/faq doesn't exist by default.

คำตอบที่ดีที่สุด

Try this one

from openerp import api, models

_logger = logging.getLogger(__name__)

import xmlrpclib


class TestConnection(models.Model):

    _name = "test.connection"   


    @api.model

    def check_connection(self):        

        url = url

        db = db_name

        username = db_user

        password = db_password

        try:

            common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))

            common.version()

            ### connection established, do your logic

        except:

            _logger.warning('*********Unable to connect the server ***********')

            ### connection Failed


อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Does anyone have an answer for this?

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
Odoo performance issue แก้ไขแล้ว
1
ม.ค. 24
15766
1
ก.ค. 25
401
0
มิ.ย. 25
2
1
มิ.ย. 23
15678
1
ธ.ค. 22
5784