Is it possible generate html in .py file and render in qweb?
<openerp>
<data>
<record id="paperformat_time" model="report.paperformat">
<field name="name">Time</field>
<field name="font_size">10</field>
</record>
<report id="time_qweb" model="hr_timesheet_sheet.sheet" string="Time"
report_type="qweb-pdf" name="time.report_time" file="time.report_time" />
<record id="time_qweb" model="ir.actions.report.xml">
<field name="paperformat_id" ref="time.paperformat_time" />
</record>
</data>
</openerp>
qweb
<template id="report_time">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="t">
<span t-esc="t.__compute_html()" />
<div class="page">
<span t-field="t.html_text " />
</div>
</t>
</t>
</template>
.py file
class Time(models.Model):
_inherit = 'hr_timesheet_sheet.sheet'
html_text = fields.Html(string = 'Html')
#api.one
def _compute_html(self):
html_value = "<h1>TEST</h1>"
html_value += "<h1>TEST 2</h1>"
self.html_text = html_value
eg.
html_value = "<h1> + employee_id.name + "</h1>"
html_value += "<h1> + employee_id.phone + "</h1>"
now I need html_value render in qweb in put in <div class="page"> put here html_value </div>
Now I save text in database, any better solution?................................
Yes you can if you have a variable that have html code if you use t-esc or t-field odoo will print it as text.
If you want to render it use. t-raw
<div t-raw="doc.some_attribute" > </div>
Or
<t t-raw="doc.some_attribute" > </t>
You can try this
<span t-raw="my_html_field"/>
Here my_html_field is your html formated data
Since Odoo version 15, t-raw is deprecated. You need to render the HTML in a safe way using Python, then parse that into XML. Reference
import markupsafe
...
class YourController(http.Controller):
...
#http.route(...)
def your_rendering_method(self):
...
return request.render("YOUR_TEMPLATE", {"YOUR_FIELD": markupsafe.Markup("<a href='https://www.stackoverflow.com'>Stack Overflow</a>")})
<t t-esc="YOUR_FIELD" />
Related
i added a checkbox on product page
When (checkbox_customtext) field is TRUE a (customtext) field should appear on the site
When an False field should disappear
I wrote the code but does not work
Please help me solve the problem
py xml files
<odoo>
<template id="website_insert_customtext" inherit_id="website_sale.address">
<xpath expr="//div[#id='div_phone']" position="after">
<!--hier is not working-->
<figure t-foreach="product_template" t-as="o" t-if="o.checkbox_customtext == True" class="snip1533">
<div t-attf-class="form-group #{error.get('customtext') and 'has-error' or ''} col-md-6"
id="div_custom_text">
<label class="control-label" for="customtext">Custom Text ( Only For Tactical vest )</label>
<input name="customtext" class="form-control"
t-att-value="'customtext' in checkout and checkout['customtext']"/>
</div>
</figure>
</xpath>
</template>
by .py
from odoo import models, fields
class ProductTemplate(models.Model):
_inherit = 'product.template'
checkbox_customtext = fields.Boolean('Add Custom field', default=True, )
class ResPartner(models.Model):
_inherit = 'sale.order'
customtext = fields.Char('Custom Text')
You can do it without using template. Just add to your customtext field in xml attrs="{'invisible': #your_domain}" and it will do the job.
<field name="customtext" attrs="{'invisible':[('checkbox_customtext', '=', False)]}"/>
I am trying to create a custom settings page using res.config.settings
I'm using res_config_settings_views.xml in the hr addons folder as a reference for the view.
And I'm using res_config_settings.py also in the hr addons folder as a reference for the model.
It worked I can see my custom settings page. I can save the values. But after I saved, the settings page back to its unset state.
When I look at the database, the values were saved. And I can call the value to use in my model.
Just confused why the settings page cannot display the values I saved?
Am I missing something?
# -*- coding: utf-8 -*-
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
president_director_id = fields.Many2one(comodel_name='hr.employee', string="President Director", readonly=False)
head_of_hr_id = fields.Many2one(comodel_name='hr.employee', string="Head of Human Resources", readonly=False)
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.fhid_recruitment</field>
<field name="model">res.config.settings</field>
<field name="priority" eval="65"/>
<field name="inherit_id" ref="base.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[hasclass('settings')]" position="inside">
<div class="app_settings_block" data-string="FHID Settings" string="FHID Settings" data-key="fhid_setting">
<h2>FHID Settings</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="president_director_id"/>
<!-- <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/> -->
<div class="row">
<div class="text-muted col-lg-8">
Set default director, ...
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="president_director_id" class="o_light_label"/>
</div>
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="head_of_hr_id"/>
<!-- <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/> -->
<div class="row">
<div class="text-muted col-lg-8">
Set default manager, ...
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="head_of_hr_id"/>
</div>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
<record id="fhid_recruitment_config_settings_action" model="ir.actions.act_window">
<field name="name">Settings Manager</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.config.settings</field>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'fhid_recruitment'}</field>
</record>
<menuitem id="fhid_recruitment_menu_configuration"
name="Settings"
parent="hr.menu_human_resources_configuration"
sequence="0"
action="fhid_recruitment_config_settings_action"
groups="base.group_system"/>
</odoo>
You will have to use set_values method to save the field values and then to get the value again you will have to you get_values method.
For example you have the following field in res.config.settings model:
head_of_hr_id = fields.Many2one('hr.employee', string="Head")
Now in order to save this field value after clicking the Save button you will have to use the following code:
def set_values(self):
super(ResConfigSettings, self).set_values()
set_param = self.env['ir.config_parameter'].sudo().set_param
set_param('module_name.head_of_hr_id', int(self.head_of_hr_id.id))
To get the value again in the field, use the following code:
#api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
get_param = self.env['ir.config_parameter'].sudo().get_param
res['head_of_hr_id'] = int(get_param('module_name.head_of_hr_id'))
return res
Hope this code help you.
I am try to disable the clear button in attachment form.
I think this is js code. I search some js code in but nothing find.
Any hint for this problem??
This is the image of clear button
Another approach for only extending this one form view is to set the field dynamically to readonly when filled.
<record id="view_attachment_form" model="ir.ui.view">
<field name="name">disable remove button if filled</field>
<field name="model">ir.attachment</field>
<field name="inherit_id" ref="base.view_attachment_form" />
<field name="arch" type="xml">
<field name="datas" position="attributes">
<attribute name="attrs">{'invisible':[('type','=','url')], 'readonly':[('datas', '!=', False)]}</attribute>
</field>
</field>
</record>
And a part of the original view:
<sheet>
<label for="name" class="oe_edit_only"/>
<h1>
<field name="name"/>
</h1>
<group>
<group>
<field name="type"/>
<field name="datas" filename="datas_fname" attrs="{'invisible':[('type','=','url')]}"/>
<field name="datas_fname" invisible="1" attrs="{'invisible':[('type','=','url')]}" class="oe_inline oe_right"/>
<!-- and so on -->
If you want to remove the Button for the whole Binary field widget in Odoo, you could just "extend" the QWeb template for that widget, which is:
<t t-name="FieldBinaryFile">
<a t-if="widget.mode === 'readonly'" href="javascript:void(0)" class="o_form_uri"/>
<div t-if="widget.mode !== 'readonly'" class="o_field_binary_file">
<input type="text" class="o_input"
readonly="readonly"
t-att-name="widget.name"
t-att-tabindex="widget.attrs.tabindex"
t-att-autofocus="widget.attrs.autofocus"/>
<button type="button" class="btn btn-sm btn-primary o_select_file_button" title="Select">Upload your file</button>
<button type="button" class="btn btn-sm btn-default fa fa-pencil o_select_file_button" title="Select"/>
<button type="button" class="btn btn-sm btn-default fa fa-trash-o o_clear_file_button" title="Clear"/>
<span class="o_form_binary_progress">Uploading...</span>
<t t-call="HiddenInputFile">
<t t-set="fileupload_id" t-value="widget.fileupload_id"/>
<t t-set="fileupload_style" t-translation="off">overflow-x: hidden</t>
</t>
</div>
</t>
You can extend QWeb templates but have to load them in the manifest file under key qweb.
xml file normally in module at /static/src/xml
<templates>
<t t-name="web.FieldBinaryFile" t-extend="base.FieldBinaryFile">
<t t-jquery="button[title='Clear']"
t-operation="replace" />
</t>
</templates>
And the part of the manifest
{
'name': 'remove button in binary widget',
# and so on
'depends': [
'base',
],
'qweb': [
'static/src/xml/remove_button.xml'
],
# and so on
}
I am using odoo 8.0 and I'm making a report for asset list. So far I have created the asset list report:
How do I add a button with function to export the report as either .pdf,.xls or .csv formats?
Here is a snippet code I have for the report view.
<record model="ir.actions.act_window" id="action_fleet_reporting_asset_listing">
<field name="name">Asset Listing</field>
<field name="res_model">fleet.asset</field>
<field name="view_id" ref="fleet_asset_listing_report"></field>
<field name="view_type">tree</field>
<field name="view_mode">tree</field>
<field name="context">{"search_default_parent_false" : True,}</field>
<field name="help" type="html">
<p>
Odoo helps you managing the costs for your different vehicles
Costs are generally created from services and contract and appears here.
</p>
<p>
Thanks to the different filters, Odoo can only print the effective
costs, sort them by type and by vehicle.
</p>
</field>
</record>
<report id="report_fleet_asset_list"
name="fleet.qweb_fleet_asset_list"
model="fleet.asset"
string="Assets"
report_type="qweb-pdf" />
Then created the Template:
<?xml version="1.0" encoding="utf-8"?>
<!--Custom report.-->
<openerp>
<data>
<template id="qweb_fleet_asset_list">
<t t-call="report.html_container">
<t t-call="report.internal_layout">
<div class="page">
<h2>Aseet List</h2>
<div class="row mt4 mb4" t-as="o" t-foreach="docs">
<div class="col-md-6">
<t t-esc="o.name"/>
</div>
<div class="col-md-6">
<t t-esc="o.location" t-if="o.location"/>
<t t-if="not o.location">-</t>
</div>
</div>
</div>
</t>
</t>
</template>
</data>
</openerp>
I am new in odoo I want to make a report pdf to my model, I have try all tuto I have find in net youtube, google but no one work for me please give me an advice.
there is my model :
# modelx.py file
from openerp import models, fields, api
class omega(models.Model):
_name = 'omega.model'
_description = 'No Description for now !!'
#api.model
def render_html(self, docids, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('report.external_layout')
docargs = {
'doc_ids': docids,
'doc_model': report.model,
'docs': self,
}
return report_obj.render('report.external_layout', docargs)
state = fields.Selection([
('Nouveau', 'Nouveau'),
('valid', 'Validation Responsable'),
('Termine', 'Termine'),
],default='Nouveau')
#api.one
def confirmer(self):
self.write({
'state': 'valid',
})
employe = fields.Many2one(comodel_name="res.users", string="Employe", required=True, delegate=True)
date = fields.Datetime(string="Date", required=True)
date2 = fields.Date(string="Date2", required=True)
day_number = fields.Integer(string="Nombre de jour", required=True)
transport = fields.Selection(string="Transport", selection=[('1', 'Train'), ('2', 'Voiture de Service'), ('3', 'Avion')])
sujet = fields.Char(string="Sujet", required=True)
lieu = fields.Char(string="Lieu", required=False)
I have also this two file XML:
<!-- report.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<openerp>
<data>
<report
id="action_report_omega"
model="omega.model"
string="Report"
report_type="qweb-pdf"
file="report.external_layout"
name="report.external_layout"
/>
</data>
</openerp>
and this file for template view as I find in net and odoo documentation
<!-- report_template.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<openerp>
<data>
<template id="report_omega_document">
<t t-call="report.html_container">
<t t-call="report.external_layout">
<div class="page">
<div class="oe_structure">
<div class="row">
<H1>Hi there hello</H1>
</div>
</div>
</div>
</t>
</t>
</template>
<template id="report_omega">
<t t-call="report.html_container">
<t t-call="report.external_layout">
<t t-foreach="doc_ids" t-as="doc_id">
<div class="page">
<div class="oe_structure">
<div class="row">
<H3>Hi hello </H3>
</div>
</div>
</div>
</t>
</t>
</t>
</template>
</data>
</openerp>
when I execute the programme for printing the report I get an empty file, give me any advice please
You need to change file and name attribute of report tag. It always represent module_name.report_template_name
<report
id="action_report_omega"
model="omega.model"
string="Report"
report_type="qweb-pdf"
file="your_module_name.report_omega"
name="your_module_name.report_omega"
/>
Afterwards, upgrade your module and try it. It should work fine.
For more details, you may refer Qweb Reports - Odoo10 Document.