1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
# you really want to read the templates.py files before
# hacking this file
from copy import copy
from gtk import *
import string
from specific import *
from entry_classes import *
import joins
import log
import checks
################ row widgets
class select_navigate_widget:
"provides facility for doing a SELECT and for navigating it"
__cursor=None
#button_next button_prev may be accessed from outside
def __init__(self):
hbox=GtkHBox()
self.widget=hbox
self.__widget=hbox
hbox.show()
prev=GtkButton()
a=GtkArrow(ARROW_LEFT, SHADOW_OUT)
a.show()
prev.add(a)
prev.show()
hbox.pack_start(prev, FALSE, FALSE, 2)
#this may be overridden
prev.connect("clicked", self.cursor_prev)
self.button_prev=prev
next=GtkButton()
a=GtkArrow(ARROW_RIGHT, SHADOW_OUT)
a.show()
next.add(a)
next.show()
hbox.pack_start(next, FALSE, FALSE, 2)
#this may be overridden
next.connect("clicked", self.cursor_next)
self.button_next=next
button_=GtkButton("select where")
button_.show()
hbox.pack_start(button_, TRUE, TRUE, 2)
button_.set_flags(CAN_FOCUS)
self.button_select=button_
button_.connect("clicked",self.select)
self.entry_key=GtkEntry()
if self.n_key!=None:
l=GtkLabel(self.fields[self.n_key]+"=")
hbox.pack_start(l,FALSE, FALSE, 2)
l.show()
self.entry_key.connect("changed", self.select)
#FIXME this is not in 'characters':self.entry_key.set_usize(80,10)
hbox.pack_start(self.entry_key,FALSE, FALSE, 2)
self.entry_key.show()
l=GtkLabel("&&")
hbox.pack_start(l,FALSE, FALSE, 2)
l.show()
entry=GtkEntry()
hbox.pack_start(entry, TRUE, TRUE, 2)
entry.show()
entry.set_flags(CAN_FOCUS)
entry.connect("activate",self.select)
self.entry_where=entry
#useful methods to use the cursor in a consistent way
def select(self,button):
self.cursor_del()
#FIXME THIS DOESNT WORK
#sql="SELECT ("+self.keyfield+", id) FROM " + self.table_name
sql="SELECT * FROM " + self.table_name
selkey=self.entry_key.get_text()
selwhere=self.entry_where.get_text()
if selkey or selwhere:
sql=sql+" WHERE "
if selkey != "":
sql=sql + self.fields[self.n_key] + " LIKE '"+selkey+"%' "
if selwhere != "":
if selkey != "":
sql=sql+" && "
sql=sql + selwhere
if self.cursor_query(sql):
r=self.cursor_next(None)
if not r:
self.log(1,"no matches for:" +sql)
self.cursor_del()
def has_cursor(self):
return not not self.__cursor
def cursor_query(self,query):
#prepares the cursor for a SELECT query
if self.__cursor:
del self.__cursor
self.__cursor=self.db.cursor()
if self.__cursor == None:
self.log(3, "E: could not get a cursor (please retry)")
return FALSE
self.log(3, "SQL :"+query)
try:
self.__cursor.execute(query)
except self.dbapi_exceptions.Warning, a:
self.log(1,"WARNING: "+str(a))
self.__cursor=None
return FALSE
except self.dbapi_exceptions.Error, a:
self.log(1,"ERROR: "+str(a))
self.__cursor=None
return FALSE
else:
self.__cursor.backlist=[]
self.__cursor.position=-1
return TRUE
def cursor_next(self,b):
if len(self.__cursor.backlist) > self.__cursor.position+1:
self.__cursor.position = 1 + self.__cursor.position
r=self.__cursor.backlist[self.__cursor.position]
else:
r=self.__cursor.fetchone()
if r:
self.__cursor.backlist.append(r)
self.__cursor.position = 1 + self.__cursor.position
if r:
self.set_row(r)
else:
#if called from a button...
if b:
self.log(1,"no more matches")
self.set_row(None)
#self.cursor=None
self.button_next.set_sensitive(not not r)
self.button_prev.set_sensitive(self.__cursor.position>0)
#print "NEXT"+str(self.__cursor.position)
return r
def cursor_prev(self,b):
if self.__cursor.position>0:
self.__cursor.position = self.__cursor.position - 1
else:
self.log(1,"already on first row")
r=self.__cursor.backlist[self.__cursor.position]
self.set_row(r)
self.button_prev.set_sensitive(self.__cursor.position>0)
self.button_next.set_sensitive(TRUE)
#print "PREV"+str(self.__cursor.position)
return r
def cursor_del(self):
if self.__cursor:
del self.__cursor
self.__cursor=None
self.button_next.set_sensitive(FALSE)
self.button_prev.set_sensitive(FALSE)
class fast_search_widget(table_info,row_widget_template,
select_navigate_widget):
__row=None
__popup_window=None
__primary_value=None
def __init__(self,db,db_name,table_name,
#which field to use to select when doing set() or get()
join_field=None ):
table_info.__init__(self,db,db_name,table_name)
# there is no __init__
#row_widget_template.__init__(self,db,db_name,table_name)
if join_field != None:
self.n_primary=self.fields.index(join_field)
select_navigate_widget.__init__(self)
#save it for future references
self.select_navigate_thewidget=self.widget
self.db_name=db_name
self.table_name=table_name
self.db=db
#main widget
vbox=GtkVBox()
self.label_current=GtkLabel()
self.label_current.show()
b=GtkButton()
b.show()
b.add(self.label_current)
b.connect("pressed",self.__popup)
b.connect("released",self.__popdown)
vbox.pack_start(b, FALSE, FALSE, 2)
vbox.pack_start(self.select_navigate_thewidget, FALSE, FALSE, 2)
vbox.show()
self.widget=vbox
self.__widget=vbox
def __popup(self,b):
if self.__row==None:
return
if not self.__popup_window:
f=form_widget(self.db,self.db_name,self.table_name)
f.set_editable(FALSE)
self.__popup_form=f
window1=GtkWindow(WINDOW_POPUP)
window1.set_policy(FALSE, TRUE, FALSE)
window1.add(f.widget)
window1.set_position(WIN_POS_MOUSE)
self.__popup_window=window1
self.__popup_window.show()
self.__popup_form.set_row(self.__row)
def __popdown(self,b):
if self.__popup_window:
self.__popup_window.hide()
def get(self):
return self.__primary_value
def set(self,data):
self.cursor_del()
if self.n_primary == None:
self.log(3,'INTERNAL ERROR: cant "set" into table ' + self.table_name)
if data != None:
sql="SELECT * FROM " + self.table_name +\
" WHERE " + self.fields[self.n_primary] + " = "+str(data)
cursor=self.db.cursor()
cursor.execute(sql)
self.set_row(cursor.fetchone())
del cursor
else:
self.set_row(None)
#whatever it is... save it!
self.__primary_value=data
def set_row(self,r):
self.__row=r
if r != None:
self.__primary_value=r[self.n_primary]
self.label_current.set_text(str(r[self.n_key:])[:60])
else:
#FIXME: is this the right thing to do?
self.__primary_value=None
self.label_current.set_text("NULL")
def get_row(self):
return self.__row
def set_editable(self,v):
self.select_navigate_thewidget.set_sensitive(v)
#self.select_navigate.set_editable(v)
class form_widget(table_info,row_widget_template):
__current_row=None
def __init__(self,db,db_name,table_name,
#these are used for automatic joins
join_rules=[],table_fields={}):
self.field_entry={}
table_info.__init__(self,db,db_name,table_name)
#row_widget_template.__init__(self,db,db_name,table_name)
self.__table_name=table_name
frame=GtkFrame(table_name)
frame.show()
#export this
self.widget=frame
#and keep a copy
self.__widget=frame
vbox2=GtkVBox()
vbox2.show()
frame.add(vbox2)
########fields
for f in self.fields:
columninfo=self.field_columninfo[f]
description=self.field_description[f]
(Field, Type, Null, Key, Default, Extra) = columninfo
hbox1=GtkHBox()
vbox2.pack_start(hbox1, FALSE, TRUE, 0)
hbox1.set_usize(-1, -1)
hbox1.set_homogeneous(FALSE)
hbox1.set_spacing(0)
eventbox1=GtkEventBox()
hbox1.pack_start(eventbox1, FALSE, FALSE, 0)
eventbox1.set_usize(-1, -1)
label1=GtkLabel()
eventbox1.add(label1)
tooltip = GtkTooltips();
tooltip.set_tip(eventbox1,repr(columninfo) +"\n" + repr(description ))
eventbox1.show()
label1.set_justify(JUSTIFY_CENTER)
label1.set_alignment(0.5, 0.5)
label1.set_padding(0, 0)
label1.show()
join=None
if join_rules != []:
join=joins.find_join(self.table_name, Field,
#these are used for automatic joins
join_rules,table_fields)
if join != None:
(t2, f2)=join
#print "join " + repr(join)
entry1=fast_search_widget(self.db,self.db_name,t2,f2)
entry1.dbapi_exceptions=self.dbapi_exceptions
label1.set_text(Field+'=\n='+t2+'.'+f2)
else:
label1.set_text(Field)
entry1=appropriate_entry_widget(columninfo,description)
#redirect log messages
entry1.log=self.log
hbox1.pack_start(entry1.widget, TRUE, TRUE, 0)
hbox1.show()
self.field_entry[Field]=entry1
#template methods
def set_editable(self,v):
for f in self.fields:
e=self.field_entry[f]
if f == "id":
#e.entry_widget
e.set_editable(FALSE)
else:
#e.entry_widget
e.set_editable(v)
def get_row(self):
r=[]
for i in range(self.n_fields):
entry=self.field_entry[ self.fields[i] ]
r.append(entry.get())
return r
def get_current_row(self):
"""this is the row that was set into this class, not the one that the
user edits"""
return self.__current_row
def set_row(self,row):
if row != None:
for i in range(len(row)):
entry=self.field_entry[ self.fields[i] ]
entry.set(row[i])
self.__current_row=copy(row)
#else:
# self.log(1,"Can't set row to None in form for table "+self.__table_name)
class editor_widget(log.log_widget,
form_widget, select_navigate_widget):
def __init__(self,db,db_name,table_name,
#api
dbapi,dbapi_exceptions,
#these are used for automatic joins
join_rules=[],table_fields={},
#list of functions that will check the row (see checks.py)
checks=[checks.extra_spaces,checks.empty_null,
checks.not_null_not_empty]):
self.dbapi_exceptions=dbapi_exceptions
self.checks=checks
###the form widget
form_widget.__init__(self,db,db_name,table_name,
#these are used for automatic joins
join_rules,table_fields)
#save it for future references
formwidget=self.widget
### the select navigate widget
select_navigate_widget.__init__(self)
#save it for future references
self.select_navigate_thewidget=self.widget
###the log widget (it is in log.py)
log.log_widget.__init__(self)
#self.log=logwidget.log
#use it also for the form widget
#formwidget.log=logwidget.log
#save it for future references
self.log_thewidget=self.widget
vbox1=GtkVBox()
vbox1.set_spacing(2)
#hbox4=GtkHBox()
handlebox=GtkHandleBox()
vbox1.pack_start(handlebox, FALSE, FALSE, 2)
handlebox.show()
vbox4=GtkVBox()
handlebox.add(vbox4)
vbox4.show()
### toolbar of buttons
tb=GtkToolbar()
tb.show()
tb.set_space_style(TOOLBAR_SPACE_LINE)
tb.set_space_size(20)
vbox4.pack_start(tb,FALSE,FALSE,2)
button_=GtkButton("insert")
tb.append_widget(button_, "","")
button_.show()
button_.set_flags(CAN_FOCUS)
self.button_insert=button_
self.button_insert.connect("clicked",self.insert)
button_=GtkButton("edit")
button_.show()
tb.append_widget(button_, "","")
button_.set_flags(CAN_FOCUS)
self.button_edit=button_
button_.show()
self.button_edit.connect("clicked",self.edit)
button_=GtkButton("update")
button_.show()
tb.append_widget(button_, "","")
button_.set_flags(CAN_FOCUS)
self.button_update=button_
self.button_update.connect("clicked",self.update)
button_=GtkButton("cancel")
button_.show()
tb.append_widget(button_, "","")
button_.set_flags(CAN_FOCUS)
#button_.show()
self.button_cancel=button_
self.button_cancel.connect("clicked",self.cancel)
tb.append_space()
button_=GtkButton("default")
button_.show()
tb.append_widget(button_, "sets entry to default values","")
button_.set_flags(CAN_FOCUS)
#button_.show()
self.button_default=button_
self.button_default.connect("clicked",self.default)
tb.append_space()
button_=GtkButton("reset")
tb.append_widget(button_, "returns this program to the starting state","")
button_.set_flags(CAN_FOCUS)
button_.show()
self.button_reset=button_
self.button_reset.connect("clicked",self.reset)
vbox4.pack_start(self.select_navigate_thewidget, FALSE,FALSE, 2)
###select stuff
hbox=GtkHBox()
vbox4.pack_start(hbox, FALSE,FALSE, 2)
hbox.show()
#so we can show/hide
self.widget_select=hbox
self.button_prev.set_usize(20,20)
self.button_next.set_usize(20,20)
#add another callback
self.button_prev.connect("clicked", self.editable)
self.button_next.connect("clicked", self.editable)
vbox1.pack_start(formwidget, FALSE, FALSE, 2)
#the log widget (it is in log.py)
vbox1.pack_start(self.log_thewidget, FALSE, FALSE, 2)
vbox1.show()
#export this
self.widget=vbox1
#and keep a copy
self.__widget=vbox1
#bring to initial state
self.reset(None)
############################
def edit(self,button):
self.set_editable(TRUE)
if self.has_cursor():
self.button_update.set_sensitive(TRUE)
else:
self.button_insert.set_sensitive(TRUE)
self.button_default.set_sensitive(TRUE)
self.button_cancel.set_sensitive(TRUE)
self.button_next.set_sensitive(FALSE)
self.button_prev.set_sensitive(FALSE)
self.button_edit.set_sensitive(FALSE)
#self.button_select.set_sensitive(FALSE)
#self.entry_select.set_sensitive(FALSE)
self.select_navigate_thewidget.set_sensitive(FALSE)
def default(self,button):
self.set_row(self.default_row)
def cancel(self,button):
self.button_next.set_sensitive(self.has_cursor() )
self.button_prev.set_sensitive(self.has_cursor() )
self.select_navigate_thewidget.set_sensitive( self.has_cursor() )
self.button_edit.set_sensitive(TRUE)
self.set_editable(FALSE)
self.set_row(self.get_current_row())
self.button_update.set_sensitive(FALSE)
self.button_insert.set_sensitive(FALSE)
self.button_cancel.set_sensitive(FALSE)
def editable(self,button):
r=self.get_row()
if r:
self.button_edit.set_sensitive(None != r[self.n_primary])
def update(self,b):
r= self.get_row()
(r, msgs,msg) = checks.check_list_of_checks(self.checks,self,r)
if r == None:
self.log(1,msg)
return
sql="UPDATE %s SET " % self.table_name
for i in range(self.n_fields):
sql=sql+self.fields[i]+"=%s, "
sql=sql[:-2]+" WHERE "
cr=self.get_current_row()
#FIXME: horrible fix for strange "cant update" problem
if len(self.primary_key_fields)==1:
sql=sql+self.fields[self.n_primary]+ "=%s && "
longrow=tuple(list(r) + [cr[self.n_primary]] )
else:
# this should work perfectly... but it does not!
for i in range(self.n_fields):
sql=sql+self.fields[i]+ "=%s && "
longrow=tuple(list(r) + list(cr))
sql=sql[:-4]
#WHAT THE HELL IS WRONG
#print "R " + repr (r)
#print "CR " + repr(cr)
#print "SQL " + repr(sql)
self.db.select_db(self.db_name)
self.db.paramstyle='format'
c=self.db.cursor()
try:
try:
c.execute(sql, longrow )
except self.dbapi_exceptions.Warning, b:
msg=msg+"\nSQL WARNING:"+str(b)
except self.dbapi_exceptions.Error, b:
self.log(1,"ERROR: "+str(b)+msg)
else:
self.set_row(r)
#simple hack to show the new line
self.cancel(None)
if c.rowcount == 1:
self.log(3,msg+"\nupdate was succesfull")
else:
self.log(3,msg+"\nWARNING update was done on %s rows" % c.rowcount )
del c
def insert(self,bla):
r= self.get_row()
(r, msgs,msg) = checks.check_list_of_checks(self.checks,self,r)
if r == None:
self.log(1,msg)
return
self.db.select_db(self.db_name)
sql= "INSERT INTO " + self.table_name + " VALUES ( " + " %s, " * (self.n_fields-1) + "%s )"
self.db.select_db(self.db_name)
self.db.paramstyle='format'
c=self.db.cursor()
try:
c.execute(sql , r)
except self.dbapi_exceptions.Warning, b:
self.log(3,"SQL WARNING: " + str(b)+"\n"+msg)
except self.dbapi_exceptions.Error, b:
self.log(3,"SQL ERROR: " + str(b)+"\n"+msg)
else:
self.log(1,"INSERT was succeful\n"+msg)
del c
def reset(self, bla):
self.set_row(self.default_row)
self.select_navigate_thewidget.set_sensitive(TRUE)
self.set_editable(FALSE)
self.cursor_del()
self.button_prev.set_sensitive(FALSE)
self.button_next.set_sensitive(FALSE)
self.button_update.set_sensitive(FALSE)
self.button_edit.set_sensitive(TRUE)
self.button_insert.set_sensitive(FALSE)
self.button_cancel.set_sensitive(FALSE)
self.log(1,"TABLE "+self.table_name)
def editor(db, db_name, table_name,
#api
dbapi,dbapi_exceptions,
#these are used for automatic joins
join_rules=[],table_fields={}):
x=editor_widget(db, db_name, table_name,
#api
dbapi,dbapi_exceptions,
#
join_rules, table_fields)
window1=GtkWindow(WINDOW_TOPLEVEL)
window1.set_title("sql:/"+db_name+"/"+table_name)
#window1.set_usize(-1, -1)
window1.set_policy(FALSE, TRUE, FALSE)
#window1.connect("destroy", close_window, self)
#window1.GTKPY_MAIN = self
window1.add(x.widget)
window1.show()
return window1
|