Re: [tryton-dev] Updating a one2many via on_change_*

2016-05-06 Thread Cédric Krier
On 2016-05-05 17:43, Sergi Almacellas Abellana wrote:
> El 05/05/16 a les 17:35, Clemens Hupka ha escrit:
> >Hello!
> >
> >I'm working on a ticket reservation and selling system based on tryton 3.8.
> >
> >As discussed with pokoli on #tryton, my model is quite special because i
> >have two one2many-relations towards ticket.ticket (one from
> >ticket.reservation and one from ticket.sale).
> 
> Here is the link to the discussion on the irc:
> 
> http://www.tryton.org/~irclog/2016-05-05.log.html#t2016-05-05%2015:09
> >
> >The goal was to get all tickets of a reservation into a sale with the
> >following on_change function (the number of tickets in the system should
> >be kept constant). The problem is, that with this piece of code the
> >number of reserved ticket is newly created, so if i select a reservation
> >with tickets id:162 and id:163 it creates me two new empty tickets
> >instead of updating the reserved ones...
> 
> As shown in the irclog, this Returns:
> 
>   [{'tickets': defaultdict(, {'add': [(0, {}), (1, {})]})}]
> 
> To the client code, because the records have no modified property.
> So I'm wondering if we should at least return the id in order to relate
> already existing records.

The code managed this behaviour is:
http://hg.tryton.org/trytond/file/default/trytond/model/modelview.py#l673
With the active record design, it had to choose the behaviour of the
on_change for the One2Many and the Many2Many. We could not mix both
because they have incompatible design (dict actions vs list of ids).
I think the older design was a little bit problematic because when
overriding we could be know which one will be used.
So it was decided to stick on_change behaviour to the default behaviour
of the client which means for One2Many: create, update and delete and
for ManyMany: add/remove.

> >Here is the code:
> >
> >class Ticketsale(ModelSQL, ModelView):
> > """
> >Ticket sale
> >"""
> > __name__ = 'ticket.sale'
> > receipt_number = fields.Char('Receipt number', readonly=True,
> >select=True)
> > pos_create_date = fields.DateTime('Create date', readonly=True)
> > reservation = fields.Many2One('ticket.reservation', 'Reservation')
> > voucher = fields.Many2One('ticket.voucher', 'Voucher')
> > tickets = fields.One2Many('ticket.ticket', 'sale', "Tickets")
> > @fields.depends('reservation','tickets')
> > def on_change_reservation(self):
> > if self.reservation:
> > tkts = list(self.tickets)
> > for t in self.reservation.tickets:
> > tkts.append(t)
> > self.tickets = tuple(tkts)
> >
> >I already found another solution with the use of a button. Just wanted
> >to know (btw pokoli too) if the behaviour of tryton is correct in that
> >(indeed very special and uncommon) case.

I think the behaviour of Tryton shows you that there is a design issue
in this code. The tickers should be a Many2Many even if you would like
to have ticket linked to only one sale. I said that because nothing
prevent the user to select a reservation that is already selected on
another sale so you will re-link the tickets from one sale to another.
I think it is not what you want.
If you are using a Many2Many (with a unique constraint), this case will
raise an error and so the user could correct it.

-- 
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/20160506085535.GV13326%40tetsuo.


Re: [tryton-dev] Updating a one2many via on_change_*

2016-05-05 Thread Sergi Almacellas Abellana

El 05/05/16 a les 17:35, Clemens Hupka ha escrit:

Hello!

I'm working on a ticket reservation and selling system based on tryton 3.8.

As discussed with pokoli on #tryton, my model is quite special because i
have two one2many-relations towards ticket.ticket (one from
ticket.reservation and one from ticket.sale).


Here is the link to the discussion on the irc:

http://www.tryton.org/~irclog/2016-05-05.log.html#t2016-05-05%2015:09


The goal was to get all tickets of a reservation into a sale with the
following on_change function (the number of tickets in the system should
be kept constant). The problem is, that with this piece of code the
number of reserved ticket is newly created, so if i select a reservation
with tickets id:162 and id:163 it creates me two new empty tickets
instead of updating the reserved ones...


As shown in the irclog, this Returns:

  [{'tickets': defaultdict(, {'add': [(0, {}), (1, {})]})}]

To the client code, because the records have no modified property.
So I'm wondering if we should at least return the id in order to relate 
already existing records.




Here is the code:

class Ticketsale(ModelSQL, ModelView):
 """
Ticket sale
"""
 __name__ = 'ticket.sale'
 receipt_number = fields.Char('Receipt number', readonly=True,
select=True)
 pos_create_date = fields.DateTime('Create date', readonly=True)
 reservation = fields.Many2One('ticket.reservation', 'Reservation')
 voucher = fields.Many2One('ticket.voucher', 'Voucher')
 tickets = fields.One2Many('ticket.ticket', 'sale', "Tickets")
 @fields.depends('reservation','tickets')
 def on_change_reservation(self):
 if self.reservation:
 tkts = list(self.tickets)
 for t in self.reservation.tickets:
 tkts.append(t)
 self.tickets = tuple(tkts)

I already found another solution with the use of a button. Just wanted
to know (btw pokoli too) if the behaviour of tryton is correct in that
(indeed very special and uncommon) case.

Kind regards,
Clemens Hupka.

--
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/675c1ae1-becd-4036-8efb-a704942baf9a%40googlegroups.com
.



--
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/572B6A21.4010508%40koolpi.com.