add rows to OneToMany

35 views
Skip to first unread message

yousef...@gmail.com

unread,
Nov 22, 2015, 1:50:38 AM11/22/15
to Project Camelot
Hello all

Does someone knows why this code only add 3 rows to OneToMany dd in Entity aaa?

Thanks

# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import datetime

from sqlalchemy.ext import hybrid
from sqlalchemy.types import Date, Unicode, Integer, Boolean, Numeric

from sqlalchemy import orm, schema, sql, ForeignKey,Column,ForeignKeyConstraint,PrimaryKeyConstraint

from camelot.admin.entity_admin import EntityAdmin
from camelot.core.document import documented_entity
from camelot.core.orm import ( Entity, using_options, Field, ManyToMany,
                               ManyToOne, OneToMany, ColumnProperty, OneToOne )
from camelot.core.utils import ugettext_lazy as _

class aaa( Entity ):
    using_options( tablename = 'aa' )

    invoice_date = Field(Unicode(250),required = True)
    message = Field(Unicode(256))
    description = Field(Unicode(60))

    dd = OneToMany( 'bbb',
                    cascade='all, delete, delete-orphan')

    def _get_address_field( self, name ):
        for party_address in self.dd:
            #print(party_address, name)
            return getattr( party_address, name )

    def _set_address_field( self, name, value ):
        print( name ,value)
        rec = bbb.query.filter_by( street1 = value ).first()
        #if not self.dd:
        if not [j for j in self.dd if j.street1 == value]:
            if not rec and name == 'street1' :
                address = bbb()
                self.dd.append( address )

        k = (len(self.dd)-1)
        if k > 2:
            self.dd.remove(address)
        for i,r in enumerate(self.dd):
            if i == k:
                address = self.dd[i]
                setattr( address, name, value )
                if address.street1==None:
                    session = orm.object_session( address )
                    if address in session.new:
                        session.expunge( address )

                    else:
                        session.delete( address )
                        self.dd.remove( address )

    @hybrid.hybrid_property
    def street1( self ):
        return self._get_address_field( u'street1' )

    @street1.setter
    def street1_setter( self, value ):
        return self._set_address_field( u'street1', value )

    @hybrid.hybrid_property
    def amount( self ):
        return self._get_address_field( u'amount' )

    @amount.setter
    def amount_setter( self, value ):
        return self._set_address_field( u'amount', value )

aaa = documented_entity()( aaa )

class bbb(Entity):
    using_options( tablename = 'bb' )
    order_item_seq_id = Field(Integer(),primary_key=True,required=True)
    o = Field(Integer(), colname='invoice_item_id',primary_key=True)
    ww = ManyToOne(aaa, ondelete = 'cascade',
                                  onupdate = 'cascade',
                                  field = o,
                                  )

    street1 = Field(Unicode(),required=False)
    amount = Field(Unicode())
    description = Field(Unicode(60))
    taxable = Field(Boolean)
    unit_price = Field(Integer)

    def get_compounding_objects( self, aaa ):
            if aaa.dd:
                yield aaa.dd

bbb = documented_entity()( bbb )

class InvoiceItemAdmin( EntityAdmin ):
    verbose_name = _('line')
    verbose_name_plural = _('line')
    list_display = ['description','taxable','amount','street1']
    list_search = ['description']
    #form_display = CustomForm()
    form_display = ['description','taxable','amount','street1']

    form_size = (400,200)
bbb.Admin = InvoiceItemAdmin

class InvoiceAdmin( EntityAdmin ):
    verbose_name = _('invoice')
    verbose_name_plural = _('invoice')
    list_display = ['invoice_date', 'description']
    list_search = ['description']
    form_display = ['invoice_date', 'description','street1','amount','dd']
    form_actions = []
    field_attributes = dict(dd = dict(create_inline = False),
                                street1 = dict( editable = True,
                                        minimal_column_width = 20,
                                        address_type = 'street1',
                                        from_string = lambda s:('street1', s),
                                        #delegate = delegates.VirtualAddressDelegate,
                                        name = 'street1'),
                                amount = dict( editable = True,
                                        minimal_column_width = 20,
                                        address_type = 'amount',
                                        from_string = lambda s:('amount', s),
                                        #delegate = delegates.VirtualAddressDelegate,
                                        name = 'amount'),)
 

aaa.Admin = InvoiceAdmin

 
t.py
Reply all
Reply to author
Forward
0 new messages