Re: [tryton-dev] set_context to @table_query from Report

2018-01-31 Thread Josias Pérez
This works well for me. Thanks!

El 31/01/2018, 9:01:41 (UTC-6), Sergi Almacellas Abellana escribió:
> El 31/01/18 a les 15:33, Josias Pérez ha escrit:
> > I was thinking in something like the code below: 
> > 
> > In Report
> > 
> > with Transaction().set_context(_check_access=False):
> > date = data['date']
> > department = data['deparment']
> 
> Then you should have to surround the search with the set_context call:
> 
> with Transaction().set_context(
>   date=data['date'], department=data['department']):
>records = Model.search(domain)
> > 
> > In table_query
> > if Transaction().context.get('date'):
> > where &= report.date >= Transaction().context['date']
> > if Transaction().context.get('department'):
> > where &= report_line.department == 
> > Transaction().context['deparment']
> > 
> > But I dont know if it is possible or a good practice.
> 
> Indeed the table query can be directly searched so you be able to
> perform the following search on your get_records method:
> 
> records = Model.search([
>   ('date', '>=', data['date']),
>   ('department', '=', data['department']),
>   ])
> 
> For me this is the cleaner solution.
> 
> -- 
> Sergi Almacellas Abellana
> www.koolpi.com
> Twitter: @pokoli_srk

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/1395ce4d-2284-47b4-84f4-fb6841e35fa0%40googlegroups.com.


Re: [tryton-dev] Adding a sequence on an existing field

2018-01-31 Thread Maxime Richez
> I think the problem is:
> 
> cls.number.states['readonly'] = True
> 
> Try to remove required states that is not True (and/or invisible in case that 
> you like not show it)
> 

Not working because unchanged in the database, but also risky like Cédric 
said... so i just changed my test ;-) Thanks anyway.

> 
> Why not design your feature as the code in party module (from sequence or 
> not)?
> 
> https://bitbucket.org/tryton/party/src/7f54e6742e171187d0e84640f68fd88bc30a8e28/party.py?at=default=file-view-default#party.py-34
>  
> https://bitbucket.org/tryton/party/src/7f54e6742e171187d0e84640f68fd88bc30a8e28/party.py?at=default=file-view-default#party.py-188

Will have a look... 

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/d05f6501-fad6-41ed-bd1d-faf0d81a7c84%40googlegroups.com.


Re: [tryton-dev] set_context to @table_query from Report

2018-01-31 Thread Sergi Almacellas Abellana
El 31/01/18 a les 15:33, Josias Pérez ha escrit:
> I was thinking in something like the code below: 
> 
> In Report
> 
> with Transaction().set_context(_check_access=False):
> date = data['date']
> department = data['deparment']

Then you should have to surround the search with the set_context call:

with Transaction().set_context(
date=data['date'], department=data['department']):
   records = Model.search(domain)
> 
> In table_query
> if Transaction().context.get('date'):
> where &= report.date >= Transaction().context['date']
> if Transaction().context.get('department'):
> where &= report_line.department == Transaction().context['deparment']
> 
> But I dont know if it is possible or a good practice.

Indeed the table query can be directly searched so you be able to
perform the following search on your get_records method:

records = Model.search([
('date', '>=', data['date']),
('department', '=', data['department']),
])

For me this is the cleaner solution.

-- 
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/13a9c499-ad11-02b1-deb0-de0557019797%40koolpi.com.


Re: [tryton-dev] How to update to null a table column with python-sql

2018-01-31 Thread Raimon Esteve
2018-01-31 15:19 GMT+01:00 Cédric Krier :

> On 2018-01-31 13:50, Raimon Esteve wrote:
> > I try with python-sql to update a column to "null".
> >
> > Somebody know how to update to null a column?
> >
> > Example:
> >
> > query = user.update(
> > columns=[user.main_company],
> > values=[None], <- I like here to update with null
> > where=user.main_company != Null)
>
> As you do not explain what is not working, we can not help you.
>

Examples and errors with python-sql 0.9:

*1- values is [None]*

query = user.update(
columns=[user.main_company],
values=[None],
where=user.main_company != Null)

Query is:

('UPDATE "res_user" SET "main_company" = %s WHERE
("res_user"."main_company" IS NOT NULL)', (None,))

Get error:

  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 193, in __iter__
yield str(self)
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 766, in __str__
where = ' WHERE ' + str(self.where)
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/operators.py",
line 224, in __str__
return '(%s IS NOT NULL)' % self.left
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 1300, in __str__
alias = self._from.alias
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 250, in alias
return AliasManager.get(self)
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 155, in get
if getattr(cls.local, 'alias', None) is None:
RuntimeError: maximum recursion depth exceeded in cmp

*2- values is [Null]*

query = user.update(
columns=[user.main_company],
values=[Null],
where=user.main_company != Null)

Query is:

('UPDATE "res_user" SET "main_company" = %s WHERE
("res_user"."main_company" IS NOT NULL)', (None,))

Get error:

  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 193, in __iter__
yield str(self)
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 766, in __str__
where = ' WHERE ' + str(self.where)
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/operators.py",
line 224, in __str__
return '(%s IS NOT NULL)' % self.left
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 1300, in __str__
alias = self._from.alias
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 250, in alias
return AliasManager.get(self)
  File
"/home/resteve/.virtualenvs/nan40/local/lib/python2.7/site-packages/sql/__init__.py",
line 155, in get
if getattr(cls.local, 'alias', None) is None:
RuntimeError: maximum recursion depth exceeded in cmp

*3- values is an empty list: []*

query = user.update(
columns=[user.main_company],
values=[],
where=user.main_company != Null)

Query is:

('UPDATE "res_user" SET  WHERE ("res_user"."main_company" IS NOT NULL)', ())

Get error:

Traceback (most recent call last):
  File "./trytond/bin/trytond-admin", line 21, in 
admin.run(options)
  File "/home/resteve/virtualenv/nan40/trytond/trytond/admin.py", line 48,
in run
Pool(db_name).init(update=options.update, lang=lang)
  File "/home/resteve/virtualenv/nan40/trytond/trytond/pool.py", line 155,
in init
lang=lang)
  File
"/home/resteve/virtualenv/nan40/trytond/trytond/modules/__init__.py", line
429, in load_modules
_load_modules()
  File
"/home/resteve/virtualenv/nan40/trytond/trytond/modules/__init__.py", line
398, in _load_modules
load_module_graph(graph, pool, update, lang)
  File
"/home/resteve/virtualenv/nan40/trytond/trytond/modules/__init__.py", line
237, in load_module_graph
cls.__register__(module)
  File
"/home/resteve/virtualenv/nan40/trytond/trytond/modules/party_company/user.py",
line 51, in __register__
cursor.execute(*query)
psycopg2.ProgrammingError: syntax error at or near "WHERE"
LINE 1: UPDATE "res_user" SET  WHERE ("res_user"."main_company" IS N...

*Annex:*

- I don't found an example a tryton modules or at  tests in python-sql to
set null a column.

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/CAN2HbXU2OFVCmp9OTh96aSaeBtU3Zo9HLGvhW0TQZgSf-522Pg%40mail.gmail.com.


Re: [tryton-dev] Adding a sequence on an existing field

2018-01-31 Thread Sergi Almacellas Abellana
El 31/01/18 a les 14:51, Maxime Richez ha escrit:
> I decide to create a custom module to add a sequence on an existing field. In 
> my case, it concerns stock_lot module. There's a field "number" (type char, 
> required=true) that i need to be "sequenced".

I believe there is a generic need to create lot numbers with a sequence
defined. Indeed nantic has a module which allows to configure the
sequence based on the product or on the category [1].

I will be very happy if we find a generic solution that can be part of
tryton.

> Here's the code: https://pastebin.com/74EXsjT7

We use states to save pyson states which. If you want to make it always
readonly you can do:

@classmethod
def __setup__(cls):
super(Lot, cls).__setup__()
cls.number.readonly = True


[1] https://bitbucket.org/nantic/trytond-stock_lot_sequence/src

-- 
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/a77876ce-9474-9762-eb7c-7d60b7cec913%40koolpi.com.


Re: [tryton-dev] How to update to null a table column with python-sql

2018-01-31 Thread Cédric Krier
On 2018-01-31 13:50, Raimon Esteve wrote:
> I try with python-sql to update a column to "null".
> 
> Somebody know how to update to null a column?
> 
> Example:
> 
> query = user.update(
> columns=[user.main_company],
> values=[None], <- I like here to update with null
> where=user.main_company != Null)

As you do not explain what is not working, we can not help you.

-- 
Cédric Krier - B2CK SPRL
Email/Jabber: cedric.kr...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/20180131141904.GE23162%40kei.


Re: [tryton-dev] Adding a sequence on an existing field

2018-01-31 Thread Cédric Krier
On 2018-01-31 05:51, Maxime Richez wrote:
> So, what's the best way to finish my custom module?
> 
> - Changing my original test "if values.get('number') is None:" by "if 
> values.get('number') == "":" (and keeping the database unchanged) ?

Use a test like this:

if not values.get('number'):
…

In Tryton required means not False for Python so it includes None,
empty string, empty list, empty dictionary etc.

> - Using some sql in the __register__ method to remove the "NOT NULL" on the 
> "number" column ?

The constraint on removing "NOT NULL" only by the original module is
there on purpose because once a field is required all depending code
will not manage the case if it is empty. So we must ensure that there
will be always a value.
Also it is the reason why we must be careful when setting a field
required.

-- 
Cédric Krier - B2CK SPRL
Email/Jabber: cedric.kr...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/20180131141825.GD23162%40kei.


Re: [tryton-dev] Adding a sequence on an existing field

2018-01-31 Thread Raimon Esteve
Hie,

2018-01-31 14:51 GMT+01:00 Maxime Richez :
>
> Hi,
>
> Some questions and explanations of something i thought easy to realize...
> I decide to create a custom module to add a sequence on an existing
field. In my case, it concerns stock_lot module. There's a field "number"
(type char, required=true) that i need to be "sequenced".
>
> So in my module, there are 2 classes: Configuration,
ConfigurationSequence to define my sequence and i update the original class
"Lot" to define "number" field "readonly", and set sequence on this number
field when creating a new lot.
>
> Here's the code: https://pastebin.com/74EXsjT7

I think the problem is:

cls.number.states['readonly'] = True

Try to remove required states that is not True (and/or invisible in case
that you like not show it)

> In the first time, forgot the __register__ method.
>
> When executing my new module and saving a new lot, this was not working,
i got error on "Number" that is required... it seems number field is not
updated by my new sequence...
>
> Checking my create method, there's a test "if values.get('number') is
None:"...
> I discovered this test is not working because value of the field is not
None but "" (checking database, related field is defined as "NOT NULL"
because of the "required = True" in the original model and then couldn't be
None).
>
> Checking many tryton scripts, i saw it was possible to remove the "NOT
NULL" through the register method with "table.not_null_action('number',
action='remove')"... but not allowed to do this with a custom module
because there is a check if the module_name attempting to update fields is
the original module.
>
> So, what's the best way to finish my custom module?
>
> - Changing my original test "if values.get('number') is None:" by "if
values.get('number') == "":" (and keeping the database unchanged) ?
> - Using some sql in the __register__ method to remove the "NOT NULL" on
the "number" column ?

Why not design your feature as the code in party module (from sequence or
not)?

https://bitbucket.org/tryton/party/src/7f54e6742e171187d0e84640f68fd88bc30a8e28/party.py?at=default=file-view-default#party.py-34
https://bitbucket.org/tryton/party/src/7f54e6742e171187d0e84640f68fd88bc30a8e28/party.py?at=default=file-view-default#party.py-188

Regards

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/CAN2HbXWC93NcxJnN1XA2mz_mshzr2KhO-mpS_6Tw2A3QhpNTbA%40mail.gmail.com.


[tryton-dev] Adding a sequence on an existing field

2018-01-31 Thread Maxime Richez
Hi,

Some questions and explanations of something i thought easy to realize... 
I decide to create a custom module to add a sequence on an existing field. In 
my case, it concerns stock_lot module. There's a field "number" (type char, 
required=true) that i need to be "sequenced".

So in my module, there are 2 classes: Configuration, ConfigurationSequence to 
define my sequence and i update the original class "Lot" to define "number" 
field "readonly", and set sequence on this number field when creating a new lot.

Here's the code: https://pastebin.com/74EXsjT7

In the first time, forgot the __register__ method.

When executing my new module and saving a new lot, this was not working, i got 
error on "Number" that is required... it seems number field is not updated by 
my new sequence...

Checking my create method, there's a test "if values.get('number') is None:"...
I discovered this test is not working because value of the field is not None 
but "" (checking database, related field is defined as "NOT NULL" because of 
the "required = True" in the original model and then couldn't be None).

Checking many tryton scripts, i saw it was possible to remove the "NOT NULL" 
through the register method with "table.not_null_action('number', 
action='remove')"... but not allowed to do this with a custom module because 
there is a check if the module_name attempting to update fields is the original 
module.

So, what's the best way to finish my custom module?

- Changing my original test "if values.get('number') is None:" by "if 
values.get('number') == "":" (and keeping the database unchanged) ?
- Using some sql in the __register__ method to remove the "NOT NULL" on the 
"number" column ?

Thanks for suggestions :-)

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/370607dd-b45c-4667-8555-f0e28fa4b449%40googlegroups.com.


[tryton-dev] How to update to null a table column with python-sql

2018-01-31 Thread Raimon Esteve
hie,

I try with python-sql to update a column to "null".

Somebody know how to update to null a column?

Example:

query = user.update(
columns=[user.main_company],
values=[None], <- I like here to update with null
where=user.main_company != Null)

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/CAN2HbXVn8ZJFyW_Oemjj14O%2BxDGw4CWjW24S%3D-tdP76jm1FBZA%40mail.gmail.com.


Re: [tryton-dev] set_context to @table_query from Report

2018-01-31 Thread Sergi Almacellas Abellana
El 31/01/18 a les 13:04, Josias Pérez ha escrit:
> So, I want to group these result to show only 
> 
> Group 1   45 (which is the expected behavior), dates are not required in the 
> report.
> 
> I was thinking in add the method set_context to the report and send it to the 
> table_query. In a table_query, without report, using an aditional class 
> 'deparment.context' it works fine and give the expected results. 
> 
> Any help will be appreciated. 
I think the better option is to create two diferents table_queries with
different group bys. And then on your report just use the one you want.

In order to simplify the code, you can use a Mixin as done in the
oportunity module:

http://hg.tryton.org/modules/sale_opportunity/file/e65684f7ecbd/opportunity.py#l520

Hope it helps.

-- 
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/64abab2b-b669-37fe-3bee-54f8ce5b15a5%40koolpi.com.


[tryton-dev] set_context to @table_query from Report

2018-01-31 Thread Josias Pérez
Hi,

I have a custom module with the follow definition:

DepartmentLine
Department (many2one to Deparment)
Product
Quantity

Department
Customer
Date 
Lines (one2many to DepartmentLine)


I have a custom report that call info from a table_query

class DepartmentReport(Report):

'Report'
__name__ = 'disc.report'

@classmethod
def _get_records(cls, ids, model, data):
Report = Pool().get('disc.report.department.table')

clause = ''
clause = clause[:]
date = data['date']

department = data['department']

if date: 
clause = [clause,   
('date','>=',date)
]
if department:
clause = [clause, 
('department','=',department)
]
query = Report.search(clause,
order=[('total', 'DESC')]) 
return query 

class ReportDepartmentTable(ModelSQL, ModelView):
'Report Department'
__name__ = 'disc.report.department.table'

group = fields.Many2One('disc.group',
'Group')
department = fields.Many2One('disc.department',
'Department')
total = fields.Numeric('Total')
date = fields.Date('Date')
 
@staticmethod
def table_query():
pool = Pool()
Report = pool.get('disc.report')
report = Report.__table__()
ReportLine = pool.get('disc.report.line')
report_line = ReportLine.__table__()

query = (report
.join(report_line,
condition=report_line.report == report.id)
.select(
Max(report_line.id * 1000).as_('id'),
Max(report.create_uid).as_('create_uid'),
Max(report.create_date).as_('create_date'),
Max(report.write_uid).as_('write_uid'),
Max(report.write_date).as_('write_date'),
Max(report.department).as_('department'),
report.date
report_line.group,
Sum(report_line.quantity).as_('total'),
where=where,
group_by=
(report_line.group,
report.date, 
)
)
)
return query 

The report works fine, with some detail, in different dates, I receive the 
following data

Group 1   10#10/01
Group 1   15#15/01
Group 1   20#20/01

So, I want to group these result to show only 

Group 1   45 (which is the expected behavior), dates are not required in the 
report.

I was thinking in add the method set_context to the report and send it to the 
table_query. In a table_query, without report, using an aditional class 
'deparment.context' it works fine and give the expected results. 

Any help will be appreciated. 

-- 
You received this message because you are subscribed to the Google Groups 
"tryton-dev" group.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tryton-dev/89acabf4-2c07-4ce9-aac9-7b77cfe9f266%40googlegroups.com.