how to get odoo binary field download link in website - odoo

I'm trying to get the download and filename of a file from the website.
model
class Files(models.Model):
_name = 'website_downloads.files'
name = fields.Char()
file = fields.Binary('File')
controler
class website_downloads(http.Controller):
#http.route('/downloads/', auth='public', website=True)
def index(self, **kw):
files = http.request.env['website_downloads.files']
return http.request.render('website_downloads.index', {
'files': files.search([]),
})
template
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="index" name="Website Downloads Index">
<t t-call="website.layout">
<div id="wrap" style="margin-top:50px;margin-bottom:50px">
<div class="container text-center">
<table class="table table-striped">
<t t-foreach="files" t-as="f">
<tr>
<td><t t-esc="f.name"/></td>
**<td>Download</td>**
</tr>
</t>
</table>
</div>
</div>
</t>
</template>
</data>
</openerp>
How can I get the download link, and when saving the file in the db save de original filename

Odoo comes with a built-in /web/binary/saveas controller, that can be used for exactly this purpose:
<t t-foreach="files" t-as="f">
<tr>
<td><t t-esc="f.name"/></td>
<td><a t-attf-href="/web/binary/saveas?model=website_downloads.files&field=file&filename_field=name&id={{ f.id }}">Download</a></td>
</tr>
</t>
The controller takes four arguments:
model - the name of the model with the Binary field
field - the name of the Binary field
id - id of the record containing particular file.
filename_field - name of a Char field containing file's name (optional).

Related

Generate html and render in qweb

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" />

QWeb pdf report odoo 10

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.

Render data on home (index) page odoo 9

I want render data on home (index) page, below is my example:
controller:
import openerp.http as http
from openerp.http import request
class TestController(http.Controller):
#http.route('/index',auth='public',website=True)
def list(self,**kw):
Test9 = http.request.env['test.9']
arr = Test9.search([])
print arr
return http.request.website.render('website.layout',
{'test9':Test9.search([])
})
xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="test9" name="Test9" page="True">
<t t-call="website.layout">
<div class="oe_structure">
<div class="container">
<center><h3>Details</h3></center>
<t t-foreach="test9" t-as="company">
<h4><span t-esc="company.name"/></h4>
<table class="table-striped table">
<tr>
<td>Name:</td>
<td><span t-esc="company.name"/></td>
</tr>
<tr>
<td>City:</td>
<td><span t-esc="company.city"/></td>
</tr>
<tr>
<td>Place:</td>
<td>
<td><span t-esc="company.place"/></td>
</td>
</tr>
</table>
</t>
</div>
</div>
</t>
</template>
</data>
</openerp>
After run http://localhost:8069/index in console get two record
Where is problem in my code?
................................................................................................................................................
Try This.
import openerp.http as http
from openerp.http import request
class List(openerp.addons.web.controllers.main.Home):
#http.route('/', type='http', auth='none', website=True)
def index(self):
Test9 = request.env['test.9']
arr = Test9.search([])
print arr
return request.render('YOUR_MODULE.test9',
{'test9':Test9.search([])
})

QWebException: "'NoneType' object has no attribute 'with_context'" while evaluating "doc.with_context({'lang':doc.partner_id.lang})"

I am new to odoo, I created a module by inheriting sales_order to create a custom report. I am getting the above error when I am printing the report. Need help please?
Here is the code snippets:
test/my_module.py:
class sale_order(models.Model):
_name = 'sale.order'
_description = 'Inheritance'
_inherit = 'sale.order'
test/views/report_template_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="test_report">
<t t-call="report.html_container">
<t t-call="report.internal_layout">
<t t-set="doc" t-value="doc.with_context({'lang':doc.partner_id.lang})"/>
<div class="page">
<div class="oe_structure"/>
<div> <strong><left>User</left></strong>
<p t-field="doc.user_id"/>
</div>
</div>
</t>
</t>
</template>
</data>
How can I access records in custom_report_template?
i know i'm late but the name of the variable containing the recordSet passed to the template is docs not doc.
and you are trying to get value from a variable that don't have anything.
doc.with_context({'lang':doc.partner_id.lang})
i think you need to do docs not doc because doc is None
This why all template in loop docs :
<t t-foreach="docs" t-as="o">

Which is Odoo website user group?

I'm new in odoo so sorry if this is a noob question.
How can I assing unlogged website user read permission to the model?
which is the website user group?
And why odoo 8.0 documentation so poor?
I've made a module website_downloads:
models:
class Files(models.Model):
_name = 'website_downloads.files'
name = fields.Char()
file = fields.Binary('File')
filename = fields.Char()
controler:
class website_downloads(http.Controller):
#http.route('/downloads/', auth='public', website=True)
def index(self, **kw):
files = http.request.env['website_downloads.files']
return http.request.render('website_downloads.index', {
'files': files.search([]),
})
website template:
<template id="index" name="Website Downloads Index">
<t t-call="website.layout">
<div id="wrap" style="margin-top:50px;margin-bottom:50px">
<div class="container text-center">
<table class="table table-striped">
<t t-foreach="files" t-as="f">
<tr>
<td align="left"><t t-esc="f.name"/></td>
<td><t t-esc="f.filename"/></td>
<td><a t-attf-href="/web/binary/saveas?model=website_downloads.files&field=file&filename_field=filename&id={{ f.id }}"> <i class="fa fa-download"></i> Download</a></td>
</tr>
</t>
</table>
</div>
</div>
</t>
</template>
security
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_website_downloads_user,website_downloads.files,model_website_downloads_files,base.group_user,1,0,0,0
access_website_downloads_manager,website_downloads.files,model_website_downloads_files,base.group_sale_manager,1,1,1,1
"base.group_public"
I solved adding this to the security file
access_website_downloads_public,website_downloads.files,model_website_downloads_files,base.group_public,1,0,0,0