Skip to content
Snippets Groups Projects
Commit cfcaae23 authored by ebuzey's avatar ebuzey
Browse files

[ADD] ReportWizard Model, View and Controller

1. Introduce the ReportWizard model: A transient model, "ReportWizard", has been implemented in
   report_wizard.py. This template represents the wizard popup window and contains
   the content of the report, the name of the file.

2. The wizard view in report_wizard_view.xml: Shows the report content and file name,
   with a button that, when clicked, triggers the "download_report" method
   on the magician model.

3.controllers.py - when the user clicks "Download report"
   of the wizard, go to the URL /self-consumption_energy/report_download.
   This URL is handled by the controller, which takes the helper registration ID from the URL,
   retrieves the report content from the log and responds with content you request
   the browser to download the report as a file.

[MIG] ReportWizard model, wizard view, controller
parent 94a1e84f
No related branches found
No related tags found
2 merge requests!195[REL] 14.0.2.0.0,!192[IMP] self_consumption: adding reports and documents
Pipeline #46660 passed
from . import models from . import models
from . import wizards from . import wizards
from . import controllers
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
"views/res_partner_views.xml", "views/res_partner_views.xml",
"views/distribution_table_views.xml", "views/distribution_table_views.xml",
"views/supply_point_assignation_views.xml", "views/supply_point_assignation_views.xml",
"views/report_wizard_view.xml",
"wizards/selfconsumption_import_wizard_views.xml", "wizards/selfconsumption_import_wizard_views.xml",
"wizards/distribution_table_import_wizard_views.xml", "wizards/distribution_table_import_wizard_views.xml",
"reports/selfconsumption_reports.xml", "reports/selfconsumption_reports.xml",
......
from . import controllers
from odoo import http
from odoo.http import request
class ReportController(http.Controller):
@http.route("/energy_selfconsumption/download_report", type="http", auth="user")
def download_report(self, **kw):
wizard = request.env["energy_selfconsumption.report_wizard"].browse(
int(kw.get("id"))
)
content = wizard.report_data
return request.make_response(
content,
[
("Content-Type", "application/text"),
("Content-Disposition", "attachment; filename=" + wizard.file_name),
],
)
...@@ -45,8 +45,6 @@ class Selfconsumption(models.Model): ...@@ -45,8 +45,6 @@ class Selfconsumption(models.Model):
"energy_project.inscription", "project_id", readonly=True "energy_project.inscription", "project_id", readonly=True
) )
inscription_count = fields.Integer(compute=_compute_inscription_count) inscription_count = fields.Integer(compute=_compute_inscription_count)
report_file = fields.Binary(string="Report File", attachment=True)
report_file_name = fields.Char(string="Report File Name")
def get_distribution_tables(self): def get_distribution_tables(self):
self.ensure_one() self.ensure_one()
...@@ -141,20 +139,24 @@ class Selfconsumption(models.Model): ...@@ -141,20 +139,24 @@ class Selfconsumption(models.Model):
"coefficient": assignation.coefficient, "coefficient": assignation.coefficient,
} }
) )
file_content = "" file_content = ""
for data in report_data: for data in report_data:
line = f"{data['code']};{str(data['coefficient']).replace('.', ',')}\n" line = f"{data['code']};{str(data['coefficient']).replace('.', ',')}\n"
file_content += line file_content += line
txt_file = io.BytesIO(file_content.encode())
file_name = "Coeficiente de reparto.txt" wizard = self.env["energy_selfconsumption.report_wizard"].create(
self.write( {"report_data": file_content, "file_name": "Coeficiente_de_reparto.txt"}
{
"report_file": base64.b64encode(txt_file.getvalue()),
"report_file_name": file_name,
}
) )
return { return {
"type": "ir.actions.act_url", "name": _("Download Partition Coefficient"),
"url": f"web/content/{self._name}/{self.id}/report_file/{file_name}?download=true", "type": "ir.actions.act_window",
"res_model": "energy_selfconsumption.report_wizard",
"view_mode": "form",
"view_id": self.env.ref(
"energy_selfconsumption.view_energy_selfconsumption_report_wizard_form"
).id,
"res_id": wizard.id,
"target": "new", "target": "new",
} }
...@@ -9,3 +9,5 @@ access_energy_selfconsumption_distribution_table_admin,energy_selfconsumption.di ...@@ -9,3 +9,5 @@ access_energy_selfconsumption_distribution_table_admin,energy_selfconsumption.di
access_energy_selfconsumption_supply_point_assignation_admin,energy_selfconsumption.supply_point_assignation.admin,model_energy_selfconsumption_supply_point_assignation,energy_project.group_admin,1,1,1,1 access_energy_selfconsumption_supply_point_assignation_admin,energy_selfconsumption.supply_point_assignation.admin,model_energy_selfconsumption_supply_point_assignation,energy_project.group_admin,1,1,1,1
access_energy_selfconsumption_selfconsumption_import_wizard_admin,energy_selfconsumption.selfconsumption_import.wizard.admin,model_energy_selfconsumption_selfconsumption_import_wizard,energy_project.group_admin,1,1,1,1 access_energy_selfconsumption_selfconsumption_import_wizard_admin,energy_selfconsumption.selfconsumption_import.wizard.admin,model_energy_selfconsumption_selfconsumption_import_wizard,energy_project.group_admin,1,1,1,1
access_energy_selfconsumption_distribution_table_import_wizard_admin,energy_selfconsumption.distribution_table_import.wizard.admin,model_energy_selfconsumption_distribution_table_import_wizard,energy_project.group_admin,1,1,1,1 access_energy_selfconsumption_distribution_table_import_wizard_admin,energy_selfconsumption.distribution_table_import.wizard.admin,model_energy_selfconsumption_distribution_table_import_wizard,energy_project.group_admin,1,1,1,1
access_energy_selfconsumption_report_wizard_user,energy_selfconsumption.report_wizard.user,model_energy_selfconsumption_report_wizard,energy_project.group_user,1,0,0,0
access_energy_selfconsumption_report_wizard_admin,energy_selfconsumption.report_wizard.admin,model_energy_selfconsumption_report_wizard,energy_project.group_admin,1,1,1,1
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_energy_selfconsumption_report_wizard_form" model="ir.ui.view">
<field name="name">energy_selfconsumption.report_wizard.form</field>
<field name="model">energy_selfconsumption.report_wizard</field>
<field name="arch" type="xml">
<form string="Generate Partition Coefficient Report">
<group>
<field name="report_data" readonly="1" />
<field name="file_name" readonly="1" />
</group>
<footer>
<button
string="Download Report"
class="btn-primary"
type="object"
name="download_report"
/>
<button string="Close" class="btn-secondary" special="cancel" />
</footer>
</form>
</field>
</record>
<record
id="action_energy_selfconsumption_report_wizard"
model="ir.actions.act_window"
>
<field name="name">Generate Partition Coefficient Report</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">energy_selfconsumption.report_wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_energy_selfconsumption_report_wizard_form" />
</record>
<menuitem
name="Generate Partition Coefficient Report"
id="menu_energy_selfconsumption_report_wizard"
action="action_energy_selfconsumption_report_wizard"
/>
</odoo>
from . import selfconsumption_import_wizard from . import selfconsumption_import_wizard
from . import distribution_table_import_wizard from . import distribution_table_import_wizard
from . import report_wizard
from odoo import _, api, fields, models
class ReportWizard(models.TransientModel):
_name = "energy_selfconsumption.report_wizard"
_description = "Generate Partition Coefficient Report"
report_data = fields.Text("Report Data", readonly=True)
file_name = fields.Char("File Name", readonly=True)
file_data = fields.Binary("File", readonly=True)
def download_report(self):
url = "/energy_selfconsumption/download_report?id=%s" % self.id
return {
"type": "ir.actions.act_url",
"url": url,
"target": "self",
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment