Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to call another model's function in a controller?

By
Michael Watchorn
on 3/7/14, 10:23 PM 2,295 views

Why am I getting the error:

"fault_code": "'hr.attendance' object has no attribute 'attendance_action_change'", "type": "server_exception"

I have this code in my controller, which works all the way up to the last line at which point it is having trouble calling the function in the hr.attendance module, but why? (It calls the authenticate function without problems)

    @http.httprequest
    def MyFunction(self, req, user_id, **kw):
        req.session.authenticate('db-name', 'username', 'password')
        hr = req.session.model("hr.attendance")        
        ids=[]      #creating my empty list of people to change attendance state
        ids.append(user_id)   #building my id list of people who will get their attendance state changed (only ever 1 person)
        print req.session._uid  #verifying I have something to submit as a uid
        cr = openerp.pooler.get_db('db-name').cursor()  #get a connection to the DB
        attendance_change_result = hr.attendance_action_change(cr, req.session._uid, ids)

EDIT: I think I should be inheriting the module I'm trying to use. So I tried that, but I still need to inherit web.http.Controller, so I'm guessing I need "_inherits". Here's my 2nd try:


import openerp
import openerp.addons.web.http as http
PUNCHCARD_TEMPLATE = """< !DOCTYPE html>
< html>
    < head>
[HTML omitted for brevity,  HTML also malformed so it will show in window]
        < /form>
    < /body>
< /html>
"""
class PunchcardController(http.Controller):
    _inherits={'hr.attendance':'hr_attendance_id','http.Controller':'Controller_id'}
    cp_path = '/my_punchcard'

    @http.httprequest
    def index(self, req, s_action=None, **kw):
        template = PUNCHCARD_TEMPLATE
        return template

    @http.httprequest
    def PunchIn(self, req, user_id, **kw):
        template = "< html>< head>< /head>< body>AND NOW USER # " + str(user_id) + " IS IN< /body>< /html>"

        req.session.authenticate('db-name', 'username', 'password')        

#        hr = req.session.model("hr.attendance")

        ids=[]                      #creating my empty list of people to change attendance state
        ids.append(user_id)         #building my id list of people who will get their attendance state changed (only ever 1 person)
        print req.session._uid      #verifying I have something to submit as a uid
        cr = openerp.pooler.get_db('db-name').cursor()  #get a connection to the DB
        attendance_change_result = super(PunchcardController, self).attendance_action_change(cr, req.session._uid, ids)

        if attendance_change_result:
            print "sign in/out should have changed" 
        return template

PunchcardController()        

When I didn't have http.Controller as an argument to the method then i got error 404 when I tried to load the 1st page. So it seems i need that in there, but then how should I also inherit hr.attendance?

0
Gustavo Seluja
On 9/21/15, 2:54 PM

Have you found an answer to your question? I'm looking for a similar answer for Openerp V7. Thank you

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 3/7/14, 10:23 PM
Seen: 2295 times
Last updated: 9/21/15, 2:54 PM