Skip to content

Commit

Permalink
Merge PR #718 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by legalsylvain
  • Loading branch information
OCA-git-bot committed Oct 10, 2023
2 parents ac3a358 + 3476c06 commit ace7828
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 233 deletions.
7 changes: 5 additions & 2 deletions sql_export/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
],
"data": [
"views/sql_export_view.xml",
"views/ir_model_fields_view.xml",
"wizard/wizard_file_view.xml",
"security/sql_export_security.xml",
"security/ir.model.access.csv",
],
"demo": [
"demo/ir_model_fields.xml",
"demo/sql_export.xml",
],
"assets": {
"web.assets_backend": [
"sql_export/static/src/scss/modal_properties.scss",
]
},
"installable": True,
}
37 changes: 0 additions & 37 deletions sql_export/demo/ir_model_fields.xml

This file was deleted.

10 changes: 5 additions & 5 deletions sql_export/demo/sql_export.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ SELECT p.id
FROM res_partner p
LEFT JOIN res_partner_res_partner_category_rel rel
ON rel.partner_id = p.id
WHERE create_date < %(x_date)s
AND id = %(x_id)s
AND rel.category_id in %(x_partner_categ_ids)s
WHERE create_date > %(Date)s
AND id = %(ID)s
AND rel.category_id in %(Categories)s
]]>
</field>
<field
eval="[(6, 0, [ref('date_field_variable_sql'), ref('integer_field_variable_sql'), ref('m2m_field_variable_sql')])]"
name="field_ids"
name="query_properties_definition"
eval="[{'name': '630eca383bc142e6', 'type': 'date', 'string': 'Date'}, {'name': '907ac618eccbab74', 'type': 'integer', 'string': 'ID'}, {'name': 'ec0556e22932334b', 'string': 'Categories', 'type': 'many2many', 'default': False, 'comodel': 'res.partner.category', 'domain': False}]"
/>
</record>

Expand Down
50 changes: 26 additions & 24 deletions sql_export/models/sql_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,8 @@ class SqlExport(models.Model):

file_format = fields.Selection([("csv", "CSV")], default="csv", required=True)

field_ids = fields.Many2many(
"ir.model.fields",
"fields_sqlquery_rel",
"sql_id",
"field_id",
"Parameters",
domain=[("model", "=", "sql.file.wizard"), ("state", "=", "manual")],
help="Before adding parameters, make sure you have created one that fill your "
"need in the dedicated menu with the right type and label. \n"
"Then, when you add a parameter here, you have to include it in the SQL "
"query in order to have dynamic values depending on the user choice.\n"
"The format of the parameters in the SQL query must be like this :"
" %(parameter_field_name)s. \n"
"Example : from the variable menu, create an variable with type 'char', "
"having field name 'x_name' and field label : 'Name' \n"
"Then, you can create a SQL query like this : "
"SELECT * FROM res_partner WHERE name = %(x_name)s the variable "
"can be used in any number of different SQL queries. \n"
"In the SQL query, you can also include these 2 special parameters "
"%(user_id)s and %(company_id)s which will be replaced respectively by "
"the user executing the query and the company of the user executing the"
" query.",
)
use_properties = fields.Boolean(compute="_compute_use_properties")
query_properties_definition = fields.PropertiesDefinition("Query Properties")

encoding = fields.Selection(
[
Expand All @@ -58,11 +37,27 @@ class SqlExport(models.Model):
default="utf-8",
)

def _compute_use_properties(self):
for rec in self:
rec.use_properties = bool(rec.query_properties_definition)

def configure_properties(self):
# we need a full window in order for property configuration to work, not a modal
wiz = self.env["sql.file.wizard"].create({"sql_export_id": self.id})
return {
"view_mode": "form",
"res_model": "sql.file.wizard",
"res_id": wiz.id,
"type": "ir.actions.act_window",
"context": self.env.context,
"nodestroy": True,
}

def export_sql_query(self):
self.ensure_one()
wiz = self.env["sql.file.wizard"].create({"sql_export_id": self.id})
# no variable input, we can return the file directly
if not self.field_ids:
if not self.query_properties_definition:
return wiz.export_sql()
else:
return {
Expand All @@ -88,3 +83,10 @@ def csv_get_data_from_query(self, variable_dict):
if self.encoding:
res = res.decode(self.encoding)
return res

def _check_execution(self):
self.ensure_one()
# only check execution if query does not contains variable
if self.query_properties_definition:
return True
return super()._check_execution()
2 changes: 1 addition & 1 deletion sql_export/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Allow to export data in csv files FROM sql requests.
There are some restrictions in the sql query, you can only read datas.
No update, deletion or creation are possible.
A new menu named Export is created.
A new sub menu named Sql Export is available in the Dashboard main menu.
2 changes: 0 additions & 2 deletions sql_export/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ See sql_request_abstract module to fix this issue.
* checking SQL request by execution and rollback is disabled in this module
since variables features has been introduced. This can be fixed by
overloading _prepare_request_check_execution() function.

* V16 : Restore export with parameters features.
3 changes: 1 addition & 2 deletions sql_export/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ Dashboards > Sql Export

- `%(company_id)s` allows to set in the query the company id of the user
- `%(user_id)s` allows to set in the query the user id
- for any created field with `Sql Export Variables` menu, you can use it with `%(x_field_example)s` syntax
(Limitation for relational fields)
- for any created property, you can use it with `%(Property String)s` syntax
3 changes: 3 additions & 0 deletions sql_export/static/src/scss/modal_properties.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.modal-body .o_field_property_add {
display: none;
}
36 changes: 31 additions & 5 deletions sql_export/tests/test_sql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import base64

from odoo import fields
from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase, tagged

Expand Down Expand Up @@ -61,15 +60,42 @@ def test_authorized_queries(self):
sql_export.state, "sql_valid", "%s is a valid request" % (query)
)

def _test_sql_query_with_params(self):
def test_sql_query_with_params(self):
query = self.env.ref("sql_export.sql_export_partner_with_variables")
query.write({"state": "sql_valid"})
categ_id = self.env.ref("base.res_partner_category_0").id
wizard = self.wizard_obj.create(
{
"sql_export_id": query.id,
"x_date": fields.Date.today(),
"x_id": 1,
"x_partner_categ_ids": [(6, 0, [categ_id])],
}
)
wizard.write(
{
"query_properties": [
{
"name": "630eca383bc142e6",
"string": "Date",
"type": "date",
"default": "",
"value": "2023-02-03",
},
{
"name": "ec0556e22932334b",
"string": "Categories",
"type": "many2many",
"default": False,
"comodel": "res.partner.category",
"domain": False,
"value": [[categ_id, "Consulting Services"]],
},
{
"name": "907ac618eccbab74",
"string": "ID",
"type": "integer",
"default": False,
"value": 1,
},
]
}
)
wizard.export_sql()
Expand Down
76 changes: 0 additions & 76 deletions sql_export/views/ir_model_fields_view.xml

This file was deleted.

34 changes: 20 additions & 14 deletions sql_export/views/sql_export_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
/>
<field name="mode">primary</field>
<field name="arch" type="xml">
<field name="query" position="after">
<field name="use_properties" invisible="1" />
</field>
<button name="button_preview_sql_expression" position="attributes">
<attribute name="states" />
<attribute
name="attrs"
>{'invisible': [('field_ids', '!=', False)]}</attribute>
>{'invisible': [('use_properties', '=', True)]}</attribute>
</button>

<xpath expr="//header" position="inside">
<button
name="export_sql_query"
Expand All @@ -24,6 +27,14 @@
class="oe_highlight"
icon="fa-arrow-right text-success"
/>
<button
name="configure_properties"
states="draft"
string="Configure Properties"
type="object"
class="oe_highlight"
icon="fa-arrow-right text-success"
/>
</xpath>
<group name="group_main_info" position="inside">
<group name="option">
Expand All @@ -41,19 +52,14 @@
/>
</group>
</group>
<group name="group_query" position="after">
<group string="Parameters">
<field
name="field_ids"
nolabel="1"
colspan="2"
options="{'no_create': True}"
context="{'tree_view_ref': 'sql_export.sql_parameter_view_tree', 'form_view_ref': 'sql_export.sql_parameter_view_form'}"
attrs="{'readonly': [('state', '!=', 'draft')]}"
groups="sql_request_abstract.group_sql_request_user"
<field name="query" position="before">
<p
colspan="2"
attrs="{'invisible': [('use_properties', '=', False)]}"
> In case of use of properties in the query, use this syntax : &#37;&#37;(Property String)s. <br
/>
</group>
</group>
Example : SELECT id FROM sale_order WHERE create_date > &#37;&#37;(Start Date)s</p>
</field>
</field>
</record>

Expand Down
Loading

0 comments on commit ace7828

Please sign in to comment.