How can i return act_window with context in function on_change odoo 8? - onchange

def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
if stage.name == 'Proposal':
print stage.name
return {
'type': 'ir.actions.act_window',
'res_model': 'sale.order',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
}
when i do this onchange i got an error
onchange_stage_values = self.onchange_stage_id(cr, uid, ids, vals.get('stage_id'), context=context)['value']
KeyError: 'value'`

It gives you a keyerror as the dictionary does not have any key by the name of 'value'.
The only keys present in the dictionary that you are returning are 'type', 'res_model', 'view_type', 'view_mode', and 'target'.
If you want to access the whole dictionary:
onchange_stage_values = self.onchange_stage_id(cr, uid, ids, vals.get('stage_id'), context=context)
This will put the whole dictionary in onchange_stage_values

Related

Update context in old api

This is the original method but I want to call it with super and ad my context to it, but it's old API and I'm kinda confused here.
After move_scrap write method should be called, but nothing happens and write is not called
and with_context of course not working
class stock_move_scrap(osv.osv_memory):
_name = "stock.move.scrap"
_description = "Scrap Products"
def move_scrap(self, cr, uid, ids, context=None):
""" To move scrapped products
#param self: The object pointer.
#param cr: A database cursor
#param uid: ID of the user currently logged in
#param ids: the ID or list of IDs if we want more than one
#param context: A standard dictionary
#return:
"""
if context is None:
context = {}
move_obj = self.pool.get('stock.move')
move_ids = context['active_ids']
for data in self.browse(cr, uid, ids):
move_obj.action_scrap(cr, uid, move_ids,
data.product_qty, data.location_id.id, restrict_lot_id=data.restrict_lot_id.id,
context=context)
if context.get('active_id'):
move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
if move.picking_id:
return {
'view_type': 'form',
'view_mode': 'form',
'res_model': 'stock.picking',
'type': 'ir.actions.act_window',
'res_id': move.picking_id.id,
'context': context
}
return {'type': 'ir.actions.act_window_close'}
i did try something like this
def move_scrap(self, cr, uid, ids, context=None):
if context is None:
context = {}
ctx = context.copy()
ctx['allow_scrap'] = True
super(stock_move_scrap,self).move_scrap(cr, uid, [], context=ctx)
Your issue is that you are removing the ids from the super call by replacing the expected ids arg with [].
Change you last line from:
super(stock_move_scrap,self).move_scrap(cr, uid, [], context=ctx)
to:
super(stock_move_scrap,self).move_scrap(cr, uid, ids, context=ctx)

How to generate a tree view and a pivot view when clicking a button in wizard

I want to filter the both tree view and pivot by date_from and date_to
Please help
Python Code:
from odoo import models, fields, api
from datetime import datetime,timedelta
class despatch(models.TransientModel):
_name = "od.despatch"
date_from = fields.Date('Date From',required=True)
date_to = fields.Date('Date To',required=True,default=fields.Date.context_today)
def od_gen(self):
invoice = self.env['account.invoice']
invoice_ids = invoice.search([('od_despatch_date','>=',self.date_from),('od_despatch_date','<=',self.date_to)])
print invoice_ids
data = self.od_mk_qry(self.date_from,self.date_to)
return {
'name':'Despatch',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'od.despatch',
'res_id': self.id,
'target': 'new',
'type': 'ir.actions.act_window',
}
def od_mk_qry(self,date_from,date_to):
qry = """ SELECT od_despatch_date,partner_id,date_invoice from account_invoice where od_despatch_date >= '%s' AND od_despatch_date <= '%s' """ % (date_from,date_to)
print qry
return qry
I'm assuming your wizard is 'od.despatch' and you need show data of the 'account.invoice' model, then, in return{} you need to add domain to filter the data that are you looking for, and change 'res_model' to 'account.invoice' because it is the data that you want to show.
#api.multi
def od_gen(self):
view_mode = 'tree,pivot'
#You need to define(xml) the views of tree and pivot before
tree_view_id = self.env.ref('your_module.your_tree_view').id
tree_pivot_id = self.env.ref('your_module.your_pivot_view').id
domain = [('od_despatch_date','>=',self.date_from),('od_despatch_date','<=',self.date_to)]
return {
'name':'Despatch',
'type': 'ir.actions.act_window',
'res_model': 'account.invoice',
'view_mode': view_mode,
'views' : [(view_tree_id, 'tree'),
(view_pivot_id, 'pivot')],
'res_id': False,
'target': 'self',
'domain': domain,
}
I hope this answer can be helful for you.

How to return form edit view in odoo?

In the below code is working for form view
search_ids = self.env['sale.order'].search([])
last_id = search_ids and max(search_ids)
return {
'name': _('Revise Quote'),
'view_type': 'form',
'view_mode': 'form',
'res_model': 'sale.order',
'res_id': last_id.id,
'type': 'ir.actions.act_window',
}
How to redirect to edit view?
In the calendar module I can see they return an additional key 'flags'.
Edit: I got the chance to test it, as I received a similar task, and I can confirm that the below flags do the trick.
calendar/calendar.py
def open_after_detach_event(self, cr, uid, ids, context=None):
...
return {
'type': 'ir.actions.act_window',
'res_model': 'calendar.event',
'view_mode': 'form',
'res_id': new_id,
'target': 'current',
'flags': {'form': {'action_buttons': True, 'options': {'mode': 'edit'}}}
}
I don't think that you can open edit view directly.
Edit is working in Odoo like this, when you start editing you are not editing actual record its something like virtual one (copied example of real) and after pressing save you are updating records in db.
So you cant just open edit view on virtual record using action return that's impossible using standard methods.
Try this in /web/static/src/js/view_form.js (line no :116)
change value of initial_mode value from view to edit. It will affect all form views.
_.defaults(this.options, {
"not_interactible_on_create": false,
// "initial_mode": "view",
"initial_mode": "edit",
"disable_autofocus": false,
"footer_to_buttons": false,
});
Hope it will solve your problem.

Send Email with attachment using scheduler in ODOO

Scheduler should generate a report and save it in ir.attachments, i.e PDF report should be stored in ir.attachment table, then need to send mail with that report as attachments.
1) How to store the report (PDF) in ir.attachment table
In ODOO 9
So far I tried:
#api.model
def _redmine_mail(self):
ir_model_data = self.env['ir.model.data']
try:
template_id = ir_model_data.get_object_reference('pms', 'email_template_edi_timesheet')[1]
print template_id
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
ctx = dict()
ctx.update({
'default_model': 'redmine.timesheet',
'default_res_id': 1,
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
'mark_so_as_sent': True
})
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form_id, 'form')],
'view_id': compose_form_id,
'target': 'new',
'context': ctx,
}
The _redmine_mail is triggered via Scheduler.
1) How to generate the report and store it in ir.attachents ?
2) No need to open the email wizard, mail should be directly send with attachment of report.
Thanks in Advance

Open a record from wizard

I have created a wizard in odoo v8. from there I am selecting a record which is many2one field.
how to open a form view of that selected record in wizard?
You need to return a action like,
suppose you have a models.Transient model like
class wiz(models.TransientModel):
_name ='custom.wiz'
man2one_field = fields.Many2one('co_model_name', 'CoModel')
def open_wiz(self):
return {
'name': _('New Open Wizard'),
'view_type': 'form',
"view_mode": 'form',
'res_model': 'model',
'type': 'ir.actions.act_window',
'domain': [('id', '=', self.man2one_field.id)],
}