Changeset 29
- Timestamp:
- 11/20/08 18:48:00 (7 weeks ago)
- Files:
-
- 5 modified
-
gnrjs/gnr_d11/js/genro.js (modified) (1 diff)
-
gnrpy/gnr/sql/gnrsqldata.py (modified) (5 diffs)
-
gnrpy/gnr/sql/gnrsqltable.py (modified) (55 diffs)
-
gnrpy/gnr/web/gnrwebapphandler.py (modified) (4 diffs)
-
gnrpy/gnr/web/gnrwebpage.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
gnrjs/gnr_d11/js/genro.js
r28 r29 743 743 resolver.onSetResolver = function(node){ 744 744 node.newBagRow = function(defaultArgs){ 745 var defaultArgs = defaultArgs || {};745 //var defaultArgs = defaultArgs || {}; 746 746 var resolver = genro.getRelationResolver(objectUpdate(this.attr.childResolverParams,{'sync':true})); 747 objectUpdate(resolver.kwargs, defaultArgs);747 resolver.kwargs.newRecordDefualts = defaultArgs; 748 748 var result = new gnr.GnrBagNode(null,'label',null ,this.attr.childResolverParams,resolver); 749 749 if ((!this._newRowTemplate) || (!objectIsEqual(defaultArgs,this._oldDefaultArgs||{}))){ -
gnrpy/gnr/sql/gnrsqldata.py
r26 r29 1502 1502 ignoreMissing=False, ignoreDuplicate=False, 1503 1503 bagFields=True, for_update=False, 1504 joinConditions=None, sqlContextName=None, newrecord=False,1504 joinConditions=None, sqlContextName=None, 1505 1505 **kwargs): 1506 1506 … … 1521 1521 self.bagFields=bagFields 1522 1522 self.for_update = for_update 1523 self.newrecord=newrecord1524 1523 1525 1524 def setJoinCondition(self, target_fld, from_fld, condition, one_one=False, **kwargs): … … 1551 1550 where = ' AND '.join(['t0.%s=:%s' % (k,k) for k in self.sqlparams.keys()]) 1552 1551 1553 if (not where) and (self.newrecord is False):1554 raise RecordSelectionError("Insufficient parameters for selecting a record: %s" % (self.dbtable.fullname, ))1555 1552 1556 1553 self._compiled = SqlQueryCompiler(self.dbtable.model, sqlparams=self.sqlparams, … … 1565 1562 def _get_result(self): 1566 1563 if self._result is None: 1567 params = self.sqlparams 1564 if not self.compiled.where: 1565 raise RecordSelectionError("Insufficient parameters for selecting a record: %s" % (self.dbtable.fullname, )) 1566 params = self.sqlparams 1568 1567 if self.pkey is not None: 1569 1568 params['pkey'] = self.pkey 1570 if not (self.newrecord is False):1571 result = self.dbtable.getNewRow(self.newrecord)1572 self._result = Bag()1573 for k,v in result.items():1574 self._result['t0_%s' % k]=v # for loaded records all fields startst with t0_1575 return self._result1576 1569 #raise '%s \n\n%s' % (str(params), str(self.compiled.get_sqltext(self.db))) 1577 1570 cursor = self.db.execute(self.compiled.get_sqltext(self.db), params) … … 1593 1586 return self._result 1594 1587 result = property(_get_result) 1588 1589 def out_newrecord(self, resolver_one=True, resolver_many=True, defaults=None): 1590 result = SqlRecordBag(self.db, self.dbtable.fullname) 1591 1592 #defaults = self.dbtable.getNewRow(defaults) 1593 record = Bag() 1594 #for k,v in defaults.items(): 1595 # record['t0_%s' % k]=v # for loaded records all fields startst with t0_ 1596 1597 self._loadRecord(result, record, self.compiled.template, resolver_many=resolver_many, resolver_one=resolver_one) 1598 self.dbtable.onCreating(result, defaults) 1599 return result 1595 1600 1596 1601 def out_bag(self, resolver_one=True, resolver_many=True): -
gnrpy/gnr/sql/gnrsqltable.py
r26 r29 3 3 # package : GenroPy sql - see LICENSE for details 4 4 # module gnrsqltable : Genro sql table object. 5 # Copyright (c) : 2004 - 2007 Softwell sas - Milano 5 # Copyright (c) : 2004 - 2007 Softwell sas - Milano 6 6 # Written by : Giovanni Porcari, Francesco Cavazzana 7 7 # Saverio Porcari, Francesco Porcari … … 45 45 self.name_long = tblobj.name_long 46 46 self.name_plural = tblobj.name_plural 47 47 48 48 def _get_model(self): 49 49 """property model. … … 51 51 return self._model() 52 52 model = property(_get_model) 53 53 54 54 def _get_pkg(self): 55 55 """property pkg. … … 57 57 return self.model.pkg 58 58 pkg = property(_get_pkg) 59 59 60 60 def _get_db(self): 61 61 """property db … … 64 64 db = property(_get_db) 65 65 dbroot = db 66 66 67 67 def applicationError(self, message, code=None): 68 68 return GnrSqlApplicationException(code, message, table=self.fullname) 69 69 70 70 def column(self,name): 71 71 """Returns a column object. … … 74 74 """ 75 75 return self.model.column(name) 76 76 77 77 def getColumnPrintWidth(self, column): 78 78 if column.dtype in ['A','C','T','X','P']: … … 101 101 format=column.attributes.get('print_format',None), 102 102 mask=column.attributes.get('print_mask',None)) 103 103 104 104 namelong=column.attributes.get('name_long','untitled') 105 105 if '\n' in namelong: … … 111 111 return max(result, headerlen) 112 112 113 113 114 114 def _get_attributes(self): 115 115 return self.model.attributes 116 116 attributes = property(_get_attributes) 117 117 118 118 def _get_pkey(self): 119 119 """property db … … 121 121 return self.model.pkey 122 122 pkey = property(_get_pkey) 123 123 124 124 def _get_lastTS(self): 125 125 """property db … … 127 127 return self.model.lastTS 128 128 lastTS = property(_get_lastTS) 129 129 130 130 def _get_noChangeMerge(self): 131 131 """property db … … 140 140 return self.model.rowcaption 141 141 rowcaption = property(_get_rowcaption) 142 142 143 143 def _get_columns(self): 144 144 """property columns 145 145 Returns the DbColumnListObj object""" 146 146 return self.model.columns 147 columns = property(_get_columns) 148 147 columns = property(_get_columns) 148 149 149 def _get_relations(self): 150 150 """property columns … … 157 157 Returns the DbIndexListObj object""" 158 158 return self.model.indexes 159 indexes = property(_get_indexes) 160 159 indexes = property(_get_indexes) 160 161 161 def _get_relations_one(self): 162 162 """property relations_one 163 163 Return a bag of relations that start from the current table""" 164 164 return self.model.relations_one 165 relations_one = property(_get_relations_one) 166 165 relations_one = property(_get_relations_one) 166 167 167 def _get_relations_many(self): 168 168 """property relations_many … … 170 170 return self.model.relations_many 171 171 relations_many = property(_get_relations_many) 172 172 173 173 def recordCoerceTypes(self,record,null='NULL'): 174 """Check and coerce types in record 174 """Check and coerce types in record 175 175 @param record: an object implementing dict interface as colname, colvalue 176 176 """ … … 191 191 if 'rjust' in colattr: 192 192 v=v.rjust(int(colattr['size']),colattr['rjust']) 193 193 194 194 elif 'ljust' in colattr: 195 195 v=v.ljust(int(colattr['size']),colattr['ljust']) 196 196 record[k]=v 197 197 198 198 def buildrecord(self, fields, resolver_one=None, resolver_many=None): 199 199 newrecord = Bag() … … 211 211 info['_target_fld'] = attrs['one_relation'] 212 212 info['mode']=attrs['mode'] 213 213 214 214 if resolver_one is True: 215 215 pass # non posso fare il resolver python, il valore di link non c'Ú ancora … … 233 233 except: 234 234 pass 235 235 236 236 newrecord.setItem(fld, v, info) 237 237 return newrecord 238 238 239 239 def buildrecord_(self, fields): 240 240 newrecord = Bag() … … 248 248 except: 249 249 pass 250 250 251 251 newrecord.setItem(fld, v, info) 252 252 return newrecord 253 253 254 254 def newrecord(self, assignId=False, resolver_one=None, resolver_many=None, **kwargs): 255 255 newrecord = self.buildrecord(kwargs, resolver_one=resolver_one, resolver_many=resolver_many) … … 257 257 newrecord[self.pkey] = self.newPkeyValue() 258 258 return newrecord 259 259 260 260 261 def record(self, pkey=None, where=None, 261 lazy=None, eager=None, mode=None, relationDict=None, ignoreMissing=False, 262 lazy=None, eager=None, mode=None, relationDict=None, ignoreMissing=False, 262 263 ignoreDuplicate=False, bagFields=True, joinConditions=None, sqlContextName=None, 263 for_update=False, newrecord=False,**kwargs):264 for_update=False, **kwargs): 264 265 """ 265 266 This method is used to get a single record of the table. It returns a SqlRecordResolver. … … 277 278 @param relationDict(optional): this is a dictionary that contains couples composed by fieldName and relationPath 278 279 e.g. {'$member_name':'@member_id.name'} 279 280 280 281 """ 281 282 record = SqlRecord(self, pkey=pkey, where=where, … … 283 284 relationDict=relationDict, 284 285 ignoreMissing=ignoreMissing, 285 ignoreDuplicate=ignoreDuplicate, 286 ignoreDuplicate=ignoreDuplicate, 286 287 joinConditions=joinConditions, sqlContextName=sqlContextName, 287 bagFields=bagFields,for_update=for_update, newrecord=newrecord,**kwargs)288 288 bagFields=bagFields,for_update=for_update, **kwargs) 289 289 290 if mode: 290 291 return record.output(mode) 291 292 else: 292 293 return record 293 294 def getNewRow(self, defaultArgs=None): 294 295 def onCreating(self, record, defaults=None): 296 newdefaults = self.defaultValues() 297 if defaults: 298 newdefaults.update(defaults) 299 for k,v in newdefaults.items(): 300 record[k] = v 301 302 def defaultValues (self): 295 303 """Override this to assign defaults to new record. 296 defaultArgs is a dictionary with values arriving 297 from the user. You can use them to define 298 the default values. 299 """ 300 result = {} 301 self.defaultValues(result) 302 if defaultArgs: 303 result.update(defaultArgs) 304 return result 305 306 def defaultValues (self, defaultArgs): 307 pass 308 309 def query(self, columns='*', where=None, order_by=None, 304 return a dictionary: fill it with defaults 305 """ 306 return {} 307 308 def query(self, columns='*', where=None, order_by=None, 310 309 distinct=None, limit=None, offset=None, 311 group_by=None, having=None, for_update=False, 310 group_by=None, having=None, for_update=False, 312 311 relationDict=None, sqlparams=None, 313 312 mode=None, **kwargs): 314 313 """This method return an object SqlQuery object which represents a query that 315 314 can be executed with different modes. 316 315 317 316 @param columns: it represents what stays between 'SELECT' and 'FROM' in the traditional SQL query. 318 317 It is a string of column names and related fields separated by comma. … … 321 320 e.g. "@member_id.name".For selecting all columns use che char '*'. 322 321 columns parameter accepts also special statements such as 'COUNT','DISTINCT' and 'SUM'. 323 322 324 323 @param where (optional):This is the sql "WHERE" clause. We suggest not 325 324 to use hardcoded values into the where clause, but 326 325 refer to variables passed to the query method as kwargs 327 326 e.g. where="$date BETWEEN :mybirthday AND :christmas", mybirthday=mbd, christmas=xmas 328 327 329 328 @param order_by (optional): this param corrisponds to sql ORDER BY operator 330 329 @param distinct (optional): this param corrisponds to sql DISTINCT operator … … 333 332 @param group_by (optional): this param corrisponds to sql GROUP BY operator 334 333 @param having (optional): this param corrisponds to sql HAVING operator 335 @param relationDict (optional):a dictionary which associates relationPath names 334 @param relationDict (optional):a dictionary which associates relationPath names 336 335 with an alias name. eg: {'$member_name':'@member_id.name'} 337 @param sqlparams (optional): an optional dictionary for sql query parameters. 338 336 @param sqlparams (optional): an optional dictionary for sql query parameters. 337 339 338 @param **kwargs : another way to pass sql query parameters 340 339 """ 341 query = SqlQuery(self, columns=columns, where=where, order_by=order_by, 340 query = SqlQuery(self, columns=columns, where=where, order_by=order_by, 342 341 distinct=distinct, limit=limit, offset=offset, 343 342 group_by=group_by, having=having, for_update=for_update, 344 relationDict=relationDict, sqlparams=sqlparams, 343 relationDict=relationDict, sqlparams=sqlparams, 345 344 **kwargs) 346 345 347 346 if mode: 348 347 raise GnrSqlException ("Query_01","Validation error: %s" % str(mode) ) 349 348 350 349 return query 351 350 352 351 def sqlWhereFromBag(self, wherebag, sqlArgs=None): 353 352 """ … … 371 370 selection.dbtable = self 372 371 return selection 373 372 374 373 def checkPkey(self,record): 375 374 pkeyValue = record.get(self.pkey) … … 379 378 record[self.pkey] = self.newPkeyValue() 380 379 return newkey 381 380 382 381 def deleteSelection(self, relation_field, relation_id): 383 382 # if self.trigger_onDeleting: … … 388 387 # if not self.trigger_onDeleting: 389 388 # sql delete where 390 389 391 390 def existsRecord(self, record): 392 391 """This method check if a record already exists in the table""" … … 394 393 record = {self.pkey: record} 395 394 return self.db.adapter.existsRecord(self, record) 396 395 397 396 def insertOrUpdate(self, record): 398 397 """This method inserts or updates a single record. … … 405 404 else: 406 405 return self.insert(record) 407 406 408 407 def insert(self, record): 409 408 """This method inserts a single record. … … 411 410 """ 412 411 self.db.insert(self, record) 413 412 414 413 def delete(self, record): 415 414 """This method deletes a single record. 416 415 @param record_data: a dictionary that represent the record that must be deleted 417 416 """ 418 self.db.delete(self, record) 419 417 self.db.delete(self, record) 418 420 419 def deleteRelated(self, record): 421 420 for rel in self.relations_many: … … 425 424 opkg, otbl, ofld = rel.attr['one_relation'].split('.') 426 425 relatedTable = self.db.table(mtbl,pkg=mpkg) 427 sel = relatedTable.query(columns='*', where='%s = :pid' % mfld, 426 sel = relatedTable.query(columns='*', where='%s = :pid' % mfld, 428 427 pid=record[ofld], for_update=True).fetch() 429 428 if sel: 430 429 if onDelete in ('r' ,'raise'): 431 430 raise GnrSqlSaveChangesException ("applyChanges_10","Cannot delete this record.(deleteRelated violates)") 432 431 433 432 elif onDelete in ('c' ,'cascade') : 434 433 for row in sel: 435 434 relatedTable.delete(relatedTable.record(row['pkey'], mode='bag')) 436 435 437 436 def update(self, record, old_record=None): 438 437 """This method updates a single record. … … 441 440 self.db.update(self, record, old_record=old_record) 442 441 443 442 444 443 def applyChanges(self, changeSet, debugPath=None): 445 444 """This method receives a changeSet and executes insert, delete or update … … 454 453 if isNew and toDelete: 455 454 return # the record doesn't exists in DB, there's no need to delete it 456 455 457 456 if isNew: 458 457 main_record = main_changeSet … … 487 486 to_fld = joiner['one_relation'].split('.')[2] 488 487 main_record[from_fld] = rel_record[to_fld] 489 488 490 489 validationError = self.validateRecord(main_record) 491 490 if validationError: 492 491 raise GnrSqlSaveChangesException ("applyChanges_03","Validation error: %s" % validationError ) 493 492 494 493 if isNew: 495 494 self.insert(main_record) … … 509 508 many_tblobj.applyChanges(sub_changeSet) 510 509 return main_record 511 510 512 511 def xmlDebug(self, data, debugPath, name=None): 513 512 name= name or self.name … … 518 517 debug_data['debug']=data 519 518 debug_data.toXml(filepath,autocreate=True) 520 519 521 520 def _splitChangeSet(self, changeSet, mainRecord=None, debugPath=None): 522 521 relatedOne={} … … 539 538 self.xmlDebug(v, debugPath, k) 540 539 return relatedOne, relatedMany 541 540 542 541 def _doFieldTriggers(self, triggerEvent, record): 543 542 trgFields = self.model._fieldTriggers.get(triggerEvent) … … 545 544 for fldname, trgFunc in trgFields: 546 545 getattr(self, 'trigger_%s' % trgFunc)(record, fldname) 547 546 548 547 def newPkeyValue(self): 549 548 """This method get a new univoque id to use as primary key on the current table""" … … 554 553 else: 555 554 return getUuid() 556 555 557 556 #---------- method to implement via mixin 558 557 def onIniting(self): 559 558 pass 560 559 561 560 def onInited(self): 562 561 pass 563 562 564 563 def validateRecord(self, record): 565 564 pass 566 565 567 566 def trigger_onInserting(self,record): 568 567 self.trigger_onUpdating(record) 569 568 570 569 def trigger_onInserted(self, record): 571 570 self.trigger_onUpdated(record) 572 571 573 572 def trigger_onUpdating(self, record, old_record=None): 574 573 pass 575 574 576 575 def trigger_onUpdated(self, record, old_record=None): 577 576 pass 578 577 579 578 def trigger_onDeleting(self, record): 580 579 pass 581 580 582 581 def trigger_onDeleted(self, record): 583 582 pass 584 583 585 584 def columnsFromString(self, columns): 586 585 result = [] … … 594 593 result.append(col) 595 594 return result 596 595 597 596 def getQueryFields(self, columns=None): 598 597 columns = columns or self.model.queryfields or self.rowcaptionDecode()[0] 599 598 return self.columnsFromString(columns) 600 599 601 600 def rowcaptionDecode(self, rowcaption=None): 602 601 rowcaption = rowcaption or self.rowcaption … … 613 612 mask = ' - '.join(['%s' for k in fields]) 614 613 return fields, mask 615 614 616 615 def recordCaption(self, record, newrecord=False, rowcaption=None): 617 616 if newrecord: … … 630 629 caption = mask % tuple([v for k,v in cols]) 631 630 return caption 632 631 633 632 def colToAs(self, col): 634 633 return self.db.colToAs(col) 635 634 636 635 def relationName(self, relpath): 637 636 relpath = self.model.resolveRelationPath(relpath) … … 642 641 targettbl = '%s.%s' % (relpkg, reltbl) 643 642 result = joiner.get('many_rel_name') or self.db.table(targettbl).name_plural 644 else: 643 else: 645 644 relpkg, reltbl, relfld = joiner['one_relation'].split('.') 646 645 targettbl = '%s.%s' % (relpkg, reltbl) … … 648 647 return result 649 648 650 649
