How do I print a report AND avoid the wizard window being closed? - openerp-7

This is a sample method I use to print a report from a wizard:
def mymethod(self, cr, uid, ids, context=None):
"""
"""
return {
'type': 'ir.actions.report.xml',
'report_name': 'trescloud_ats_2013_report',
'datas': {
'model': 'sri.ats.2013',
'res_ids': ids
}
}
How do I also avoid the wizard being closed?

Call the report with a button with type="action" instead of type="object". Like this you don't need to call a python method. If you need to run some python code before as well, then you should open again the wizard with the proper elements in the return of the function.
<button name="%(module_name.report_id)d"
type="action" />

Related

Question on How # and # String Trigger Works in Odoo Chatter?

Hope you guys doin well,
I'm curious about how can a certain string like # and # can trigger a popup of a user and channels in Odoo Chatter & Discuss. This chatter has mail.thread Models related to it but i can't seem to understand how is it possible to create my own custom trigger in chatter?
Is it belong to certain views and not in models?
Any help will be appreciated!
Those are suggestion delimiters for the models used to update the suggestion list.
const suggestionDelimiters = ['#', ':', '#', '/'];
For example # is used to fetch partners (mail.partner model) matching a given search term. The _updateSuggestionList function will search the suggestions, sort them then will show (update) the popup list
To add a new custom trigger, you need add the delimiter to the suggestionDelimiters list and alter the _computeSuggestionModelName function to return the related model, the model must be defined like the mail.partner model.
If you want to patch an existing model, check the following example (taken from the hr module):
/** #odoo-module **/
import {
registerInstancePatchModel,
registerFieldPatchModel,
} from '#mail/model/model_core';
import { attr, one2one } from '#mail/model/model_field';
// Patch model
registerInstancePatchModel('mail.partner', 'hr/static/src/models/partner/partner.js', {
async checkIsEmployee() {
await this.async(() => this.messaging.models['hr.employee'].performRpcSearchRead({
context: { active_test: false },
domain: [['user_partner_id', '=', this.id]],
fields: ['user_id', 'user_partner_id'],
}));
this.update({ hasCheckedEmployee: true });
},
});
// Patch fields
registerFieldPatchModel('mail.partner', 'hr/static/src/models/partner/partner.js', {
hasCheckedEmployee: attr({
default: false,
}),
});
There is another way, and that is using the / or "command" popup. You can add your own item in the popup list and trigger any actions when a user selects your item from the popup.
Inherit from mail.channel
class MailChannel(models.Model):
_inherit = 'mail.channel'
Add a new method that starts with _define_command_XXXXX. This registers the item in the popup.
def _define_command_sample(self):
return {'help': _('Here is some sample help')}
Add a new method that starts with _execute_command_XXXXX. This method is executed when the user uses your command.
def _execute_command_sample(self, **kwargs):
# Send a "temporary" message to the user
self._send_transient_message(self.env.user.partner_id, f"Hello! You just successfully used the /sample command. You typed {kwargs['body']}.")
Reference: Github where Odoo defines the /lead command
PS: The above is for Odoo V12.0 - V14.0. Odoo V15.0 works a bit different, but you can check this and this for how to do it.

Issue in odoo 8 displaying warning message

I am trying to validate a "capacity" field with onchange decorator but for some reason when I send the warning message the previous line stops working. The template updates the field fine whitout the warning
#api.onchange('capacity')
def check_capacity_values(self):
if self.capacity<0:
self.capacity=0
raise Warning(_('wrong capacity.'))
You can use a dictionary as return value for methods decorated by api.onchange. The key for warning messages will be warning and the value another dictionary with keys title and message. An example:
return {
'warning': {'title': "WARNING!",
'message': "It isn't allowed to have a negative capacity!"}
}
I think that the problem may be that the change you made in self.capacity just before raising the warning is not stored in the database because you are using #api.onchange, so the new value is just shown in the UI, but not stored in the database.
Try it using #api.depends instead, the change will be reflected both in the UI and the database.
#api.onchange('capacity')
def check_capacity_values(self):
if self.capacity<0:
self.capacity=0
return {'warning': {
'title': "Warning",
'message': "message",
}
}

Show a message and assign value to a field in an onchange method

I am trying to write an onchange that returns a message and updates a value at the same time. So far it displays the message but the field remains the same. The code I have is:
#api.onchange('changed_field')
def my_onchange_method(self):
if self.other_field:
self.changed_field=False
raise Warning('Some message.')
I think my mistake is in the way of sending the message, could anyone tell me how to achieve this in odoo 9? Thanks.
I think you're raising the builtin Warning exception, which is probably why the field isn't updated (I think the changes are rolled back when the exception is raised).
Try this instead :
#api.onchange('changed_field')
def my_onchange_method(self):
if self.other_field:
self.changed_field = False
return {
'warning': {
'title': 'TITLE OF THE WARNING MESSAGE BOX',
'message': 'YOUR WARNING MESSAGE',
}
}
I can confirm this works at least for odoo 8. It will probably work for odoo 9.
def onchange_amount_paid(self, cr, uid, ids, amount_paid, context=None):
res = {'value':{}}
if amount_paid:
if fee_type==1 and amount_paid<70:
warning = { 'title': ("Warning"), 'message': ('registration account minimum payment is 70'), }
return {'value': res.get('value',{}), 'warning':warning}
return {'value': res.get('value',{})}

Odoo 10 - Form view opened in edit mode

I'm developing on Odoo 10.
I created a dynamic form view which search and display a product from its barcode, but I've got a problem.
Since the view has no initial record to display it is opened in edit mode, and that's ok, because I want to type the 'barcode' field.
But, after the product is displayed, when I exit from that view the 'can_be_discarded' function is fired, opening the confirm dialog.
Have I to create a new view type inheriting from FormView or is there a way to workaround this problem?
The view is a classic form view, with nothing special.
Here's the server code instead.
class ProductFromBarcode(models.TransientModel):
_name = 'levelprime_product_general_status.product_from_barcode'
_inherits = { 'product.product': 'product_id' }
product_id = fields.Many2one(
comodel_name='product.product',
store=False)
product_barcode = fields.Integer(help='Insert the barcode to search '
'the correspondent product',
store=False)
#api.onchange('product_barcode')
def on_barcode_changed(self):
if self.product_barcode != 0:
self.product_id = self.get_product_from_barcode(self.product_barcode)
#api.model
def get_product_from_barcode(self, barcode):
r = self.env['product.product'].search([('barcode', '=', barcode)])
if r:
return r
Ok, I think I'm in the right way, there are some rendering problem to solve, but the main behavior is what I was searching for.
I created a new type of view which inherit from the 'FormView' one and override the 'can_be_discarded' method to not execute controls about the data changes.
JS
odoo.define('levelprime_product_general_status.readonly_formview', function(require) {
'use strict'
var core = require('web.core')
var FormView = require('web.FormView')
var ReadOnly_FormView = FormView.extend({
init: function() {
this._super.apply(this, arguments)
},
start: function() {
this._super.apply(this, arguments)
},
can_be_discarded: function() {
return $.Deferred().resolve()
}
})
core.view_registry.add('readonly_form', ReadOnly_FormView)
return ReadOnly_FormView
})
PY
class ViewExtension(models.Model):
_inherit = 'ir.ui.view'
type = fields.Selection(selection_add=[
('readonly_form', 'ReadOnly Form Version')])
Then you can simply use the tag in the xml.
You have used _inherits = { 'product.product': 'product_id' } in your wizard.
When using _inherits you will do a kind of polymorphic model in the database way.
For example product.product inherits product.template or res.users inherits res.partner.
This mean we create a model that gets the know how of a Model but adds aditional data/columns in a new database table. So when you create a user, all partner data is stored in res_partner table (and a partner is created) and all user related info is stored in res_users table.
In your code when wizard (levelprime_product_general_status.product_from_barcode) record will create then all product.product/product.template fields are required,due to that reason you are getting this type of error.
You can check difference between _inherit & _inherits from following link.
https://www.odoo.com/forum/how-to/developers-13/the-different-openerp-model-inheritance-mechanisms-what-s-the-difference-between-them-and-when-should-they-be-used-46
You need to follow following code.
class ProductFromBarcode(models.TransientModel):
_name = 'levelprime_product_general_status.product_from_barcode'
product_id= fields.Many2one(comodel_name='product.product',string="Product")
product_barcode = fields.Char(help='Insert the barcode to search '
'the correspondent product',
"Barcode")
#api.onchange('product_barcode')
def on_barcode_changed(self):
if self.product_barcode:
self.product_id = self.get_product_from_barcode(self.product_barcode)
#api.model
def get_product_from_barcode(self,barcode):
r = self.env['product.product'].search([('barcode', '=', barcode)])
if r:
return r
else:
return False
In above code just create wizard & add two fields.
This may help you.
from what you are saying you are using inherits to show the information of the product selected by the barcode remove it and use related field:
add the fields that you are showing on the form view to you model
name = fields.Char(related='product_id.name', readonly=True)
Now you can use name on your view it's like compute field or proxy.
you can make them readonly if you want to display them.

Results SQL results from Grails controller to dropdown list box

I am doing a query on a database and trying to return the list to a dropdown list box. I'm not getting any errors, but I am not getting any results. When I run the SQL I know it should return values, but I probably have something messed up on the controller or view side. Any help appreciated.
Controller -
import groovy.sql.Sql
class V1Controller {
def dataSource
def index() {
def nameList = {
def db = new Sql(dataSource)
def results = db.rows("SELECT NAME FROM MIT_TEST_NAME")
//results.each{row -> log.debug .name}
[results:results]
}
}
}
View -
Test Name: <g:select name="p_project_name" from="${results}" />
Also, is there any good way to see what my controller SQL query is returning?
In addition to the way Joshua showed, I was also able to get it working this way. Now I have two options of doing this! -
Test Name: <g:select name="p_project_name" from="${results?.NAME}" noSelection="${['null':'Select One...']}" />
The reason why your results are not showing up in the select list is because you haven't told the tag how to find the appropriate value. The results is a List<GroovyRowResult>, so you will need tell the tag what column from the results you want to use:
<g:select from="${results}" optionKey="NAME" optionValue="NAME" />
You can read more about the g:select tag in the documentation.