In the book collection db I'm coding (found on the developers wiki under 
BookBase), I'm get approach standard application needs. Stuff like:
1. updating the UI with a related list of grand-children records (one-2-many 
relation).
2. getting a field from a related table via a fk to populate a list or field 
with a meaningful entry for the user.
3. ask the user to confirm record saving if it tries to move from the current 
record.
4. create a record depending on the visible Page frame and update the ui 
(Thanks to Ed for the help on this)

For the solutions I've came up with, I would like specifically to hear comments 
about:
1. I'm achieving this via a SQL call in the bookAuthorBizobj that returns a 
dataset and then getting the values and keys form the dataset to the listbox. 
Sugestions on improving this or is it dandy.
2. Is this virtual field well used? (like in line # 46 in the class definition 
of BookBizobj)
3. I've used the form's beforePointerMove method (line # 21). Looks fine from 
where I'm standing.
4. I'm just always suspicious that I maybe messing up. Not the case here I hope 
:-) form's onNew event line # 137

In general i'm interested in anything you find just plain wrong or improvable. 
I'm aware that's quite a piece of code though very straight forward. Please 
keep in mind that I'm aiming to get approval put the stuff in the wiki so that 
may serve others learning.

I've numbered the lines for convenience, though you'll have to maximize the 
message reader :-(

Txs for all the help,
Miguel


def afterInitAll(self):                                                         
             # 1
        self.requery()                                                          
             # 2
        self.requery('author')                                                  
             # 3

        # start refactor
        # Put this in it's on method: 
self.populateLbBookAuthors(self.PrimaryBizobj.getPK())
        authors, keys = 
self.bookAuthorBizobj.getAuthorsAndKeys(self.PrimaryBizobj.getPK())  # 6
        self.lbBookAuthors.Choices, self.lbBookAuthors.Keys = authors, keys     
             # 7
        # end refactor
        
        # Setup Authors ListBox
        self.lbBookAuthors.MultipleSelect = False                               
             # 11

def afterPointerMove(self):                                                     
             # 13
        # Put this in it's on method: 
self.populateLbBookAuthors(self.PrimaryBizobj.getPK())
        authors, keys = 
self.bookAuthorBizobj.getAuthorsAndKeys(self.PrimaryBizobj.getPK())  # 15
        self.lbBookAuthors.Choices, self.lbBookAuthors.Keys = authors, keys     
             # 16

def beforeInit(self):                                                           
             # 18
        self.ShowMenuBar = False                                                
             # 19

def beforePointerMove(self):                                                    
             # 21
        if self.PrimaryBizobj.isChanged():                                      
             # 22
                result = dabo.ui.areYouSure('Save record?', 'Save')             
             # 23
                if result == True:                                              
             # 24
                        self.PrimaryBizobj.save()                               
             # 25
                else:                                                           
             # 26
                        self.PrimaryBizobj.cancel()                             
             # 27

def createBizobjs(self):                                                        
             # 28
        class BookBizobj(dabo.biz.dBizobj):                                     
             # 29
                def afterInit(self):                                            
             # 30
                        self.DataSource = "book"                                
             # 31
                        self.KeyField = "book_id"                               
             # 32
                        self.addFrom("book")                                    
             # 33
                        self.addField("book_id")                                
             # 34
                        self.addField("title")                                  
             # 35
                        self.addField("author_id")                              
             # 36
                        self.addField("editor")                                 
             # 37
                        self.addField("isbn")                                   
             # 38
                        self.addField("category")                               
             # 39
                        self.addField("subcategory")                            
             # 40
                        self.addField("edition")                                
             # 41
                        self.addField("year_pub")                               
             # 42
                        self.addField("collection")                             
             # 43
                        self.addField("language")                               
             # 44
                        
                        self.VirtualFields = {'authorname_v': 
self.retAuthorName}            # 46
                
                def retAuthorName(self):                                        
             # 48
                        bookAuthorBizobj = 
self.getChildByDataSource('bookauthor')           # 49
                        return bookAuthorBizobj.getAuthorName(self.getPK())     
             # 50
                        
                        
                def validateRecord(self):                                       
             # 53
                        """Returning anything other than an empty string from
                        this method will prevent the data from being saved.
                        """
                        ret = ""                                                
             # 57
                        # Add your business rules here. 
                        return ret                                              
             # 59
        
        
        class BookAuthorBizobj(dabo.biz.dBizobj):                               
             # 62
                def afterInit(self):                                            
             # 63
                        self.DataSource = "bookauthor"                          
             # 64
                        self.KeyField = "bookauthor_id"                         
             # 65
                        self.LinkField = "book_id"                              
             # 66
                
                def getAuthorName(self, bookId):                                
             # 68
                        ds = self.getRelatedAuthorsDataSet(bookId)              
             # 69
                        for rec in ds:                                          
             # 70
                                if rec['book_id'] == bookId:                    
             # 71
                                        return rec['authorname']                
             # 72
                                else:                                           
             # 73
                                        return ''                               
             # 74
                
                def getRelatedAuthorsDataSet(self, bookId):                     
             # 76
                        cr = self.getTempCursor()                               
             # 77
                        cr.execute("""                                          
             # 78
                        select bookauthor.bookauthor_id as bookauthor_id,       
             # 79
                                                bookauthor.book_id as book_id,  
             # 80
                                                author.authorname as authorname 
             # 81
                        from bookauthor                                         
             # 82
                        left join author                                        
             # 83
                                on bookauthor.author_id = author.author_id      
             # 84
                        where book_id = %d;                                     
             # 85
                        """ % bookId)                                           
             # 86
                        
                        return cr.getDataSet()                                  
             # 88

                
                def getAuthorsAndKeys(self, bookId):                            
             # 91
                        ds = self.getRelatedAuthorsDataSet(bookId)              
             # 92
                        authors = [rec['authorname'] for rec in ds]             
             # 93
                        keys = [rec['bookauthor_id'] for rec in ds]             
             # 94
                        
                        return (authors, keys)                                  
             # 96
                
                def validateRecord(self):                                       
             # 98
                        """Returning anything other than an empty string from
                        this method will prevent the data from being saved.
                        """
                        ret = ""                                                
             # 102
                        # Add your business rules here.
                        return ret                                              
             # 104
                
                
        
                        
        class AuthorBizobj(dabo.biz.dBizobj):                                   
             # 109
                def afterInit(self):                                            
             # 110
                        self.DataSource = "author"                              
             # 111
                        self.KeyField = "author_id"                             
             # 112
                        self.addFrom("author")                                  
             # 113
                        self.addField("author_id")                              
             # 114
                        self.addField("authorname")                             
             # 115
                
                def validateRecord(self):                                       
             # 117
                        """Returning anything other than an empty string from
                        this method will prevent the data from being saved.
                        """
                        ret = ""                                                
             # 121
                        # Add your business rules here. 
                        return ret                                              
             # 123
        
        
        # The primary bizobj to populate the record view fields and the Books 
Grid
        bookBizobj = BookBizobj(self.Connection)                                
             # 127
        self.addBizobj(bookBizobj)                                              
             # 128
        
        self.bookAuthorBizobj = BookAuthorBizobj(self.Connection)               
             # 130
        bookBizobj.addChild(self.bookAuthorBizobj)                              
             # 131
        
        # The bizobj to populate the Authors Grid
        authorBizobj = AuthorBizobj(self.Connection)                            
             # 134
        self.addBizobj(authorBizobj)                                            
             # 135

def onNew(self, evt):                                                           
             # 137
        pg = self.pgFrame.SelectedPage                                          
             # 138
        
        if pg == self.pgAuthorLst:                                              
             # 140
                self.getBizobj('author').new()                                  
             # 141
                self.grdAuthorLst.refresh()                                     
             # 142
        elif pg == self.pgBookLst:                                              
             # 143
                self.getBizobj('book').new()                                    
             # 144
                self.grdBookLst.refresh()                                       
             # 145


_______________________________________________________________________________________
Sabe qual e o credito pessoal MAIS FACIL DE PAGAR no futuro?
Aquele em que as PRESTACOES DESCEM ao longo do emprestimo?
Saiba mais em: http://www.iol.pt/correio/rodape.php?dst=0704171



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]

Reply via email to