2
votes

I need to get current url from browser and extract the name of the opened model. I have 3 models: audit, dysfunction and action, action has a selection field containing 2 choices: audit or dysfunction. So when I create an action, I can choose by myself, but when I create an audit, then in the audit form create an action, I want the selection field to take 'audit' as type from the first. I noticed that when we enter the action form from the audit form the URL still holds the name of the first model which is 'audit'. Audit model has a many2many relation with action model. I wrote this line in the init function:

print (self.env._current_browser()._current_page) 

but it gives me this error:

AttributeError: 'Environment' object has no attribute '_current_browser'

I alse tried this code:

    import os.path
    from urlparse import urlparse,parse_qs
    print 'hello update'
    url = os.environ["REQUEST_URI"] 
    parsed = urlparse.urlparse(url) 
    print urlparse.parse_qs(parsed.query)['model']

It gives me this error:

KeyError: 'REQUEST_URI'

I also tried this code:

from openerp import http

print http.request.httprequest.full_path

It gives me this:

/web/dataset/call_kw/action/create?

But I want it to return this url:

http://localhost:8069/web?#id=10&view_type=form&model=audit&menu_id=201&action=221

I'm working with python 2.7, odoo 8, windows 7.

Please Help. Thanks.

2
Could you please provide detailed code. What is self supposed to be e.g.?rocksteady
@Tesssnim Look here ackoverflow.com/questions/14468862/how-to-get-current-url-in-python-web-pageRichard Rublev
@RichardRublev I tried it before It gives me this error: File "C:\Users\Utilisateur\git\persol\odoo\addons\action\action.py", line 63, in _get_origin url = os.environ["REQUEST_URI"] File "C:\Python27\lib\os.py", line 425, in getitem return self.data[key.upper()] KeyError: 'REQUEST_URI'Tessnim
@Tessnim You are using Windows?Richard Rublev
@RichardRublev Yes I amTessnim

2 Answers

0
votes

You can get current path using httprequest

from openerp import http

print http.request.httprequest.full_path

But you should not call it in init method, AFAIK init is going to be called once at startup.

Edit:

If what you need is to pass an info from a model to another, I suggest you passing the value through context.

Say the audit has a many2one relation to action model.

class audit(models.Model):

    _name = "audit"

    action = fields.Many2one('action', string="Action")

Pass the context on your view :

<field name="action" context="{'from':'This is from audit model'}"/>

and when you're at action model you can call the context using this method:

self._context.get('from', False)

It doesn't have to be a field to pass context, you can do it in button too.

0
votes

I found this today:

I just need to add this attribute to the action field inside of audit form

context="{'default_origin':'audit'}"

Noting that inside of the action model I have:

origin = fields.Selection([
                               ('audit', 'Audit'),
                               ('nonconformity', 'nonConformity'),                
                               ('dysfunction', 'Dysfunction')])