wiz_local.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_local.py
#Boa:FramePanel:pnl
# -*- coding: utf-8 -*-

import os, string

import wx, wx.lib.buttons, wx.grid

import db_lib, wx_util

[wxID_PNL, wxID_PNLBR_SAVE, wxID_PNLBT_DELETE, wxID_PNLBT_DOWN,
 wxID_PNLBT_INSERT, wxID_PNLBT_UP, wxID_PNLGR_FILED,
 wxID_PNLLBL_FAX_BOOK_NAME, wxID_PNLLBL_FILEDS, wxID_PNLTXT_FB_NAME,
] = [wx.NewId() for _init_ctrls in range(10)]

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(40, 56), size=wx.Size(328, 176),
              style=0)
        self.gr_filed.SetToolTipString('')
        self.gr_filed.SetColLabelSize(20)
        self.gr_filed.SetColMinimalAcceptableWidth(25)
        self.gr_filed.SetRowLabelSize(24)

        self.bt_insert = wx.Button(id=wxID_PNLBT_INSERT, label='Insert field',
              name='bt_insert', parent=self, pos=wx.Point(376, 64),
              size=wx.Size(75, 23), style=0)
        self.bt_insert.SetToolTipString('')
        self.bt_insert.Bind(wx.EVT_BUTTON, self.OnBt_insertButton,
              id=wxID_PNLBT_INSERT)

        self.bt_delete = wx.Button(id=wxID_PNLBT_DELETE, label='Delete field',
              name='bt_delete', parent=self, pos=wx.Point(376, 96),
              size=wx.Size(75, 23), style=0)
        self.bt_delete.SetToolTipString('')
        self.bt_delete.Bind(wx.EVT_BUTTON, self.OnBt_deleteButton,
              id=wxID_PNLBT_DELETE)

        self.br_save = wx.Button(id=wxID_PNLBR_SAVE, label='Save',
              name='br_save', parent=self, pos=wx.Point(376, 208),
              size=wx.Size(75, 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(40, 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(144, 16),
              size=wx.Size(144, 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(24, 16), size=wx.Size(70, 13), style=0)
        self.lbl_fax_book_name.SetToolTipString('')

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

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

    def __init__(self, parent, paths, db_inst, modify):
        self._init_ctrls(parent)
        self.parent = parent
        self.db_inst = db_inst
        self.paths = paths
        #For i18n
        self.ren = wx_util.rename_ctrls(self, 'wiz_local', \
            lang = self.paths.language)
        self.ren.rename()

        #modify phonebook
        self.lstModify = list()
        self.modify = modify[0]
        self.name = modify[1]
        if modify[0]:
            self.txt_fb_name.Enable(False)
            self.gr_filed.EnableEditing(False)
            self.txt_fb_name.SetValue(self.name)
            min_fields = self._getFileds()
        else:
            self.gr_filed.EnableEditing(True)
            min_fields = self.ren.get_list('_min_fields')

        self.gr_filed.CreateGrid(len(min_fields), 1)
        self.gr_filed.SetColSize(0,290)

        # Create a list that simulate a table with 4 rows and 1 col
        self.gr_filed.setData([(i, ) for i in min_fields])

        # Rename (tranlate)
        self.ren.SetGridFieldNames('gr_filed')

        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)

    def OnBt_insertButton(self, event):
        if self.modify: return
        self.gr_filed.InsertRows()
        filed_name = None
        dlg = wx.TextEntryDialog(
                self, self.ren.get("_db_exist"),
                self.ren.get("_db_exist") )

        if dlg.ShowModal() == wx.ID_OK:
            filed_name = dlg.GetValue()
        dlg.Destroy()
        
        if not filed_name: 
            return
            
        self.gr_filed.SetCellValue(0,0,filed_name)
        self.lstModify.append((filed_name, 'add'))

    def OnBt_deleteButton(self, event):
        if self.modify: return
        row = self.gr_filed.GetSelectedRows()
        if len(row) == 0: return
        row = row[0]

        #4 modify
        field_name = self.gr_filed.GetCellValue(row, 0)
        self.lstModify.append((field_name, 'del'))
        self.gr_filed.DeleteRows(row)

    def err_exist_db(self):
        wx.MessageBox(self.ren.get("_db_exist"), self.ren.get("error", 1), wx.ICON_ERROR)

    def err_generic(self, err):
        wx.MessageBox(str(err), self.ren.get("error", 1), wx.ICON_ERROR)

    def OnBr_saveButton(self, event):
        if self.modify:
            self._saveModify()
            return
        txt_fb_name = self.txt_fb_name.GetValue()
        if txt_fb_name == '':
            wx.MessageBox(self.ren.get("_insert_db_name"),
                self.ren.get("error", 1), wx.ICON_ERROR)
            return
        field_lst = list(); field_lst4db = list()
        for x in range(self.gr_filed.GetNumberRows()):
            val = self.gr_filed.GetCellValue(x,0)
            if '-' in val:
                wx.MessageBox(self.ren.get("_value_no_permit"),
                    self.ren.get("error", 1), wx.ICON_ERROR)
                return
            field_lst.append(val + ' text')
            field_lst4db.append(val)
        db_name = os.path.join(self.paths.conf_dir, 'local_' + txt_fb_name + '.db')
        status, ret = self.db_inst.queryReturn('SELECT name FROM fb WHERE name = ?',  (txt_fb_name, ))

        if os.path.exists(db_name) or ret:
            wx.MessageBox(self.ren.get("_db_exist"), self.ren.get("error", 1), wx.ICON_ERROR)
            self.paths.log4write.write("db exists, %s" % db_name)
            self.paths.log4write.write("db exists query: %s, %s" % (status, ret))
            return

        db = db_lib.Sqlite(db_name)
        db.conn()
        q = "CREATE TABLE local (%s , id integer primary key unique)" % (string.join(field_lst, ','))
        status, ret = db.queryReturn(q)
        del db
        if status:
            self.err_generic(ret)
            return
        status, ret_order = self.db_inst.queryReturn('SELECT fb_order FROM fb ORDER BY fb_order')
        if status:
            self.err_generic(ret)
            return
        if ret_order:
            last_number = ret_order[len(ret_order) -1][0]
        else:
            last_number = 0
        self.db_inst.queryReturn('INSERT INTO fb (name, fb_type, fb_order, fb_fields, fb_db_name) VALUES \
                ("%s", "local", %s, "%s", "local")' %\
                (txt_fb_name, last_number +1 , string.join(field_lst4db, ','))
                )
        self.parent.update_parent(txt_fb_name)

    def _saveModify(self):

        """ # Now this don't work yet on sqlite
        db_name = os.path.join(self.paths.conf_dir, 'local_' + txt_fb_name + '.db')
        db = db_lib.Sqlite(db_name)
        db.conn()
        for field, type in self.lstModify:
            if type == 'add':
                q = 'ALTER TABLE local ADD %s text' % field
            else:
                q = 'ALTER TABLE local DROP COLUMN %s' % field
            ret, val = db.queryReturn(q)
            if ret:
                self.err_generic(val)
            print q"""

        txt_fb_name = self.txt_fb_name.GetValue()

        fields = [ self.gr_filed.GetCellValue(row, 0)
                    for row in xrange(self.gr_filed.GetNumberRows()) ]

        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)

    def _getFileds(self):
        """ Get fields
            Used in modify
        """
        ret, val = self.db_inst.queryReturn('SELECT fb_fields FROM fb WHERE name = "%s"'
            % self.name)
        if ret == 0:
            return [ x.strip() for x in val[0][0].split(',') ]
        else:
            self.err_generic(val)
            return list()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.