wiz_mysql.py :  » Business-Application » hylaPEx » hylapex » library » wizard » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » hylaPEx 
hylaPEx » hylapex » library » wizard » wiz_mysql.py
#Boa:FramePanel:pnl
# -*- coding: utf-8 -*-
#
# hylaPEx, an hylafax client written in python
#
# www.unipex.it/hylapex/
#
# License:
#         GNU General Public License (GPL)
#         For more info see LICENSE.txt and LICENSE-GPL.txt
#
# Copyright (C) 2004-2006  Unipex s.r.l.,  All Rights Reserved.
# Via Vittorio Veneto 83/A
# 33050 Gonars (UD) - Italy
# phone 0039 0432 931511 - fax 0039 0432 931378
# www.unipex.it - michele.petrazzo@unipex.it

import wx
import wx.grid
import os, string, db_lib
import wx_util

[wxID_PNL, wxID_PNLBR_SAVE, wxID_PNLBT_CONNECT, wxID_PNLBT_DOWN, 
 wxID_PNLBT_SHOW_ALL, wxID_PNLBT_UP, wxID_PNLCHK_USE_QUERY_ODBC, 
 wxID_PNLGR_FILED, wxID_PNLLBL_DB, wxID_PNLLBL_FAX_BOOK_NAME, 
 wxID_PNLLBL_FILEDS, wxID_PNLLBL_ONE_RECORD, wxID_PNLLBL_PASS, 
 wxID_PNLLBL_SERVER, wxID_PNLLBL_TABLE, wxID_PNLLBL_USER, 
 wxID_PNLLBL_USE_QUERY_ODBC, wxID_PNLTXT_DB, wxID_PNLTXT_FB_NAME, 
 wxID_PNLTXT_PASS, wxID_PNLTXT_SERVER, wxID_PNLTXT_TABLE, wxID_PNLTXT_USER, 
] = [wx.NewId() for _init_ctrls in range(23)]

class editQuery(wx.MiniFrame):
    def __init__(self, *args, **kargs):
        super(editQuery, self).__init__(*args, **kargs)
        self.SetBackgroundColour(wx.Colour(245, 245, 245))
        sz   = wx.BoxSizer(wx.VERTICAL)
        szBt = wx.BoxSizer(wx.HORIZONTAL)
        self.txt = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        btSave = wx.Button(self, label="Save and close")
        btSave.Bind(wx.EVT_BUTTON, self.onBtSave)
        btQuery = wx.Button(self, label="Test Query")
        btQuery.Bind(wx.EVT_BUTTON, self.onBtQuery)
        
        szBt.Add(btQuery, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
        szBt.Add(btSave, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
        
        sz.Add(self.txt, 1, wx.ALL | wx.EXPAND, 5)
        sz.Add(szBt, 0, wx.EXPAND)
        sz.SetMinSize(wx.Size(200,100))
        
        self.SetSizerAndFit(sz)
        self.Layout()
        self.CenterOnParent()
        
        self.Show()
        
    def setValues(self, functReturn, text=''):
        """
        """
        self.txt.SetValue(text)
        self.functReturn = functReturn
    
    def onBtQuery(self, event):
        """
        """
        self.functReturn(self.txt.GetValue())
    
    def onBtSave(self, event):
        """
        """
        self.functReturn(self.txt.GetValue())
        self.Destroy()
        
class pnl(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PNL, name='pnl_1', parent=prnt,
              pos=wx.Point(10, 55), size=wx.Size(468, 277),
              style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(460, 250))
        self.SetToolTipString('')
        self.SetBackgroundColour(wx.Colour(245, 245, 245))

        self.gr_filed = wx_util.sortGrid(id=wxID_PNLGR_FILED, name='gr_filed',
              parent=self, pos=wx.Point(232, 56), size=wx.Size(152, 176),
              style=0)
        self.gr_filed.SetToolTipString('')
        self.gr_filed.SetColLabelSize(20)
        self.gr_filed.SetColMinimalAcceptableWidth(25)
        self.gr_filed.SetRowLabelSize(24)

        self.br_save = wx.Button(id=wxID_PNLBR_SAVE, label='Save',
              name='br_save', parent=self, pos=wx.Point(392, 208),
              size=wx.Size(59, 23), style=0)
        self.br_save.SetToolTipString('')
        self.br_save.Bind(wx.EVT_BUTTON, self.OnBr_saveButton,
              id=wxID_PNLBR_SAVE)

        self.lbl_fileds = wx.StaticText(id=wxID_PNLLBL_FILEDS,
              label='List of fields', name='lbl_fileds', parent=self,
              pos=wx.Point(224, 40), size=wx.Size(55, 13), style=0)

        self.txt_fb_name = wx.TextCtrl(id=wxID_PNLTXT_FB_NAME,
              name='txt_fb_name', parent=self, pos=wx.Point(104, 16),
              size=wx.Size(96, 21), style=0, value='')
        self.txt_fb_name.SetToolTipString('')

        self.lbl_fax_book_name = wx.StaticText(id=wxID_PNLLBL_FAX_BOOK_NAME,
              label='Faxbook name', name='lbl_fax_book_name', parent=self,
              pos=wx.Point(16, 16), size=wx.Size(70, 13), style=0)
        self.lbl_fax_book_name.SetToolTipString('')

        self.txt_server = wx.TextCtrl(id=wxID_PNLTXT_SERVER, name='txt_server',
              parent=self, pos=wx.Point(104, 64), size=wx.Size(100, 21),
              style=0, value='')
        self.txt_server.SetToolTipString('')

        self.txt_user = wx.TextCtrl(id=wxID_PNLTXT_USER, name='txt_user',
              parent=self, pos=wx.Point(104, 88), size=wx.Size(100, 21),
              style=0, value='')
        self.txt_user.SetToolTipString('')

        self.txt_pass = wx.TextCtrl(id=wxID_PNLTXT_PASS, name='txt_pass',
              parent=self, pos=wx.Point(104, 112), size=wx.Size(100, 21),
              style=wx.TE_PASSWORD, value='')
        self.txt_pass.SetToolTipString('')

        self.txt_db = wx.TextCtrl(id=wxID_PNLTXT_DB, name='txt_db', parent=self,
              pos=wx.Point(104, 136), size=wx.Size(100, 21), style=0, value='')
        self.txt_db.SetToolTipString('')

        self.txt_table = wx.TextCtrl(id=wxID_PNLTXT_TABLE, name='txt_table',
              parent=self, pos=wx.Point(104, 160), size=wx.Size(100, 21),
              style=0, value='')
        self.txt_table.SetToolTipString('')

        self.bt_connect = wx.Button(id=wxID_PNLBT_CONNECT, label='Connect',
              name='bt_connect', parent=self, pos=wx.Point(72, 208),
              size=wx.Size(75, 23), style=0)
        self.bt_connect.SetToolTipString('')
        self.bt_connect.Bind(wx.EVT_BUTTON, self.OnBt_connectButton,
              id=wxID_PNLBT_CONNECT)

        self.lbl_server = wx.StaticText(id=wxID_PNLLBL_SERVER, label='Server',
              name='lbl_server', parent=self, pos=wx.Point(16, 64),
              size=wx.Size(31, 13), style=0)

        self.lbl_pass = wx.StaticText(id=wxID_PNLLBL_PASS, label='Password',
              name='lbl_pass', parent=self, pos=wx.Point(16, 112),
              size=wx.Size(46, 13), style=0)
        self.lbl_pass.SetToolTipString('')

        self.lbl_user = wx.StaticText(id=wxID_PNLLBL_USER, label='Username',
              name='lbl_user', parent=self, pos=wx.Point(16, 88),
              size=wx.Size(48, 13), style=0)
        self.lbl_user.SetToolTipString('')

        self.lbl_db = wx.StaticText(id=wxID_PNLLBL_DB, label='Db',
              name='lbl_db', parent=self, pos=wx.Point(16, 136),
              size=wx.Size(14, 13), style=0)
        self.lbl_db.SetToolTipString('')

        self.lbl_table = wx.StaticText(id=wxID_PNLLBL_TABLE, label='Table',
              name='lbl_table', parent=self, pos=wx.Point(16, 160),
              size=wx.Size(27, 13), style=0)
        self.lbl_table.SetToolTipString('')

        self.lbl_one_record = wx.StaticText(id=wxID_PNLLBL_ONE_RECORD,
              label='Table MUST have at least\none record before connect',
              name='lbl_one_record', parent=self, pos=wx.Point(40, 180),
              size=wx.Size(128, 24), style=0)
        self.lbl_one_record.SetToolTipString('')

        self.bt_up = wx.BitmapButton(bitmap=wx.NullBitmap, id=wxID_PNLBT_UP,
              name='bt_up', parent=self, pos=wx.Point(392, 104),
              size=wx.Size(16, 16), style=wx.BU_AUTODRAW)
        self.bt_up.SetToolTipString('')

        self.bt_down = wx.BitmapButton(bitmap=wx.NullBitmap, id=wxID_PNLBT_DOWN,
              name='bt_down', parent=self, pos=wx.Point(392, 136),
              size=wx.Size(16, 16), style=wx.BU_AUTODRAW)
        self.bt_down.SetToolTipString('')

        self.chk_use_query_odbc = wx.CheckBox(id=wxID_PNLCHK_USE_QUERY_ODBC,
              label='', name='chk_use_query_odbc', parent=self,
              pos=wx.Point(232, 16), size=wx.Size(16, 13), style=0)
        self.chk_use_query_odbc.SetValue(False)
        self.chk_use_query_odbc.SetToolTipString('')
        self.chk_use_query_odbc.Bind(wx.EVT_CHECKBOX,
              self.OnChk_use_query_odbcCheckbox, id=wxID_PNLCHK_USE_QUERY_ODBC)

        self.lbl_use_query_odbc = wx.StaticText(id=wxID_PNLLBL_USE_QUERY_ODBC,
              label='Use the string into Table text like query',
              name='lbl_use_query_odbc', parent=self, pos=wx.Point(248, 16),
              size=wx.Size(208, 13), style=0)
        self.lbl_use_query_odbc.Bind(wx.EVT_LEFT_UP,
              self.OnLbl_use_query_odbcLeftUp)

        self.bt_show_all = wx.Button(id=wxID_PNLBT_SHOW_ALL, label='Go',
              name='bt_show_all', parent=self, pos=wx.Point(208, 160),
              size=wx.Size(17, 21), style=0)
        self.bt_show_all.SetToolTipString('')
        self.bt_show_all.Show(False)
        self.bt_show_all.Bind(wx.EVT_BUTTON, self.OnBt_show_allButton,
              id=wxID_PNLBT_SHOW_ALL)

    def __init__(self, parent, paths, db_inst, modify):
        self._init_ctrls(parent)
        self.parent = parent
        self.db_inst = db_inst
        self.paths = paths

        self.gr_filed.CreateGrid(0, 2)
        self.gr_filed.SetColSize(0, 75)
        self.gr_filed.SetColSize(1, 45)

        #For i18n
        self.ren = wx_util.rename_ctrls(self, 'wiz_mysql', \
            lang = self.paths.language)
        self.ren.rename()
        self.ren.SetGridFieldNames('gr_filed')
        
        self.modify = modify[0]
        self.name = modify[1]
        if self.modify:
            self.txt_fb_name.Enable(False)
            self.txt_fb_name.SetValue(self.name)
            self.__setPar()
        
        self.gr_filed.EnableEditing(True)
            
        self.my_path_imgs = os.path.join(self.paths.my_path, 'imgs')
        dict_img = {self.bt_up: ('up.png', (15,15)), self.bt_down: ('down.png',(15,15)), }
        
        for inst_but in dict_img:
            f = os.path.join(self.my_path_imgs, dict_img[inst_but][0])
            i = wx_util.del_mask2img(f,dict_img[inst_but][1] )
            inst_but.SetBitmapLabel(i)
        
        self.gr_filed.enableBtUpDown(self.bt_up, self.bt_down)
        
        self.useTableLikeQuery = 0
        
    def OnBr_saveButton(self, event):
        txt_fb_name = self.txt_fb_name.GetValue()
        if txt_fb_name == '':
            wx.MessageBox(self.ren.get("insert_db_name", 1), 
                    self.ren.get("error", 1), wx.ICON_ERROR)
            return
        if self.gr_filed.GetNumberRows() < 2:
            wx.MessageBox(self.ren.get("no_enough_fields", 1), 
                    self.ren.get("error", 1), wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        if not self.modify:
            status, ret_order = self.db_inst.queryReturn('SELECT fb_order FROM fb ORDER BY fb_order')
            if ret_order:
                last_number = ret_order[len(ret_order) -1][0]
            else:
                last_number = 0

        field_lst4db = [ self.gr_filed.GetCellValue(row,0) 
                        for row in xrange(self.gr_filed.GetNumberRows())
                        if self.gr_filed.GetCellValue(row, 1) in ('Y','y','1', 'S', 's')]
        
        val_k = ['db','pass','server','user']
        val_v = [self.txt_db, self.txt_pass, self.txt_server, self.txt_user, str(self.useTableLikeQuery)]
        val_other = [ "%s=%s" % ( val, val_v[num].GetValue() ) 
                      for num, val in enumerate(val_k) ]
        
        val_other.append("use_table_like_query=%s" % self.useTableLikeQuery)
        
        if not self.modify:
            q = 'INSERT INTO fb (name, fb_type, fb_order, fb_fields, fb_db_name, fb_other) \
                VALUES ("%s","mysql", %s, "%s", "%s", "%s")' %\
                (txt_fb_name, last_number +1 , ','.join(field_lst4db), self.txt_table.GetValue(),\
                ','.join(val_other) ) 
        else:
            q = 'UPDATE fb SET fb_fields="%s", fb_other="%s" WHERE name="%s"' % \
                (','.join(field_lst4db), ','.join(val_other), txt_fb_name )
        
        self.db_inst.queryReturn(q)
        
        self.parent.update_parent(txt_fb_name)

    def OnBt_connectButton(self, event, fields=None):
        vals = {'server': self.txt_server.GetValue(),'user': self.txt_user.GetValue(),\
            'pass': self.txt_pass.GetValue(), 'db': self.txt_db.GetValue()}
        
        if not (vals['db'] and self.txt_table.GetValue().strip()):
            wx.MessageBox(self.ren.get("insert_db_table", 1), 
                    self.ren.get("error", 1), wx.ICON_ERROR)
            return
        
        db = db_lib.Mysql()
        val, err = db.conn(vals)
        if val:
            wx.MessageBox(str(err), self.ren.get("error", 1), wx.ICON_ERROR)
            return
        
        self.useTableLikeQuery = self.chk_use_query_odbc.GetValue()
        if self.useTableLikeQuery:
            q = self.txt_table.GetValue()
        else:
            q = 'SELECT * FROM ' + self.txt_table.GetValue()
        
        val, err = db.queryReturn(q)
        if not val == 0:
            wx.MessageBox(str(err), self.ren.get("error", 1), wx.ICON_ERROR)
            return
        vals = err
        tmp = 0
        
        nr = self.gr_filed.GetNumberRows()
        if nr > 0: self.gr_filed.DeleteRows(0, nr )
        
        #select ragsoc.ragsoc, gruppo.descr from ragsoc inner join gruppo on ragsoc.id_gruppo = gruppo.id where ragsoc = "a"
        if not vals: return
        for field in vals[0].keys():
            if field == 'id': continue
            self.gr_filed.AppendRows()
            self.gr_filed.SetCellValue(tmp, 0, field)
            if (fields and field in fields) or not fields:
                self.gr_filed.SetCellValue(tmp, 1, 'y')
            
            tmp += 1
    def OnBt_show_allButton(self, event):
        frame = editQuery(self.parent, title="Edit query")
        frame.setValues(self.__setFromModifyFrame, self.txt_table.GetValue())
    
    def OnChk_use_query_odbcCheckbox(self, event):
        
        self.__showBtString(not event.IsChecked())

    def OnLbl_use_query_odbcLeftUp(self, event):
        
        self.__showBtString(self.chk_use_query_odbc.GetValue(), 1)
    
    def __showBtString(self, value, set=0):
        if value:
            self.lbl_table.SetLabel(self.ren.get("lbl_table"))
            self.bt_show_all.Hide()
            if set: self.chk_use_query_odbc.SetValue(0)
            
        else:
            self.lbl_table.SetLabel("Query")
            self.bt_show_all.Show()
            if set: self.chk_use_query_odbc.SetValue(1)
    
    def __setFromModifyFrame(self, text):
        """
        """
        self.txt_table.SetValue(text.replace('\n', ' '))
        self.OnBt_connectButton(None)
        
    def __setPar(self):
        """ Used for set parameters on modify
        """ 
        ret, vals = self.db_inst.queryReturn('SELECT fb_other, fb_db_name, fb_fields from fb WHERE name = "%s"' % self.name)
        if ret: return
        db_args, table, fields = vals[0]
        args = {}
        for val in db_args.split(','):
            tmp_val = string.split(val, '=')
            if len(tmp_val) > 1:
                args[tmp_val[0]] = tmp_val[1]
            else:
                args[tmp_val[0]] = ""
        
        self.txt_db.SetValue(     args['db'])
        self.txt_server.SetValue( args['server'])
        self.txt_user.SetValue(   args['user'])
        self.txt_pass.SetValue(   args['pass'])
        
        self.txt_table.SetValue(  table  )
        
        ULQ = 'use_table_like_query'
        if args.has_key(ULQ) and args[ULQ] == 'True':
            self.chk_use_query_odbc.SetValue(True)
            self.bt_show_all.Show()
            self.lbl_table.SetLabel("Query")
        
        self.OnBt_connectButton(None, fields.split(','))
        
    def _saveModify(self):
        txt_fb_name = self.txt_fb_name.GetValue()
        
        fields = [ self.gr_filed.GetCellValue(row, 0)
                    for row in xrange(self.gr_filed.GetNumberRows()) 
                    if self.gr_filed.GetCellValue(row, 0)]
        
        q = 'UPDATE fb SET fb_fields = "%s" WHERE name="%s"' % (', '.join(fields), txt_fb_name)
        ret, val = self.db_inst.queryReturn(q)
        if ret:
            self.err_generic(val)
        self.parent.update_parent(txt_fb_name) 

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.