annotate frontends/wix/form.py @ 91:39c672544593

Tarot: bidding phase - quick_app: command line is now parsed, "profile" option allow to select it - xml_tools: list-single is now managed - plugin tarot: method and signal to manage contract (contrat): tarotChooseContrat & tarotGameContratChoosed - wix: Q&D Form hack to manage more generic form (not only registration), used to show contract selection form
author Goffi <goffi@goffi.org>
date Thu, 27 May 2010 19:26:19 +0930
parents 8f2ed279784b
children 2503de7fb4c7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
5 wix: a SAT frontend
57
a5b5fb5fc9fd updated README and copyright note
Goffi <goffi@goffi.org>
parents: 37
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
7
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
12
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
17
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
21
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
22
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
23
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import wx
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import pdb
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from xml.dom import minidom
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
27 from logging import debug, info, warning, error
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from tools.jid import JID
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
29
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
30
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
31 class Form(wx.Frame):
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
32 """Create a form from a SàT xml"""
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
33
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
34 def __init__(self, host, xml_data='', title="Form", options=[], misc={}):
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 super(Form, self).__init__(None, title=title)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
36
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.host = host
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
38 self.options = options
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
39 self.misc = misc
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
40 self.ctl_list = [] # usefull to access ctrl
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
41
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.sizer = wx.BoxSizer(wx.VERTICAL)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.SetSizer(self.sizer)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.SetAutoLayout(True)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
45
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 #events
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
47 if not 'NO_CANCEL' in self.options:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
48 self.Bind(wx.EVT_CLOSE, self.onClose, self)
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
49
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.MakeModal()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
51
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.constructForm(xml_data)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
53
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.Show()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
55
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def constructForm(self, xml_data):
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 panel=wx.Panel(self)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
58 panel.sizer = wx.BoxSizer(wx.VERTICAL)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
59
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
60 cat_dom = minidom.parseString(xml_data.encode('utf-8'))
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
61
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
62 for elem in cat_dom.documentElement.getElementsByTagName("elem"):
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
63 name = elem.getAttribute("name")
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
64 label = elem.getAttribute("label") if elem.hasAttribute('label') else name
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
65 type = elem.getAttribute("type")
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
66 value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 sizer = wx.BoxSizer(wx.HORIZONTAL)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 if type=="text":
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
69 ctrl = wx.StaticText(panel, -1, value)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
70 elif type=="string":
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
71 label=wx.StaticText(panel, -1, label+": ")
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 ctrl = wx.TextCtrl(panel, -1, value)
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
73 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 sizer.Add(label)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 elif type=="password":
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 label=wx.StaticText(panel, -1, label+": ")
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 ctrl = wx.TextCtrl(panel, -1, value, style=wx.TE_PASSWORD)
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
78 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 sizer.Add(label)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
80 elif type=="list":
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
81 label=wx.StaticText(panel, -1, label+": ")
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
82 ctrl = wx.ListBox(panel, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
83 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 else:
70
Goffi <goffi@goffi.org>
parents: 57
diff changeset
85 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 raise NotImplementedError
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 sizer.Add(ctrl, 1, flag=wx.EXPAND)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 #self.ctl_list[(name, category)] = ctrl
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 panel.sizer.Add(sizer, flag=wx.EXPAND)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
90
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
91 dialogButtons = wx.StdDialogButtonSizer()
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
92 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit"))
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
93 dialogButtons.AddButton(submitButton)
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
94 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
95 if not 'NO_CANCEL' in self.options:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
96 cancelButton = wx.Button(panel,wx.ID_CANCEL)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
97 dialogButtons.AddButton(cancelButton)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
98 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
99 dialogButtons.Realize()
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
100 panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
101
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 panel.SetSizer(panel.sizer)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
103 panel.SetAutoLayout(True)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 panel.sizer.Fit(self)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.sizer.Add(panel, 1, flag=wx.EXPAND)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 cat_dom.unlink()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
107
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
108 def onFormSubmitted(self, event):
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
109 """Called when submit button is clicked"""
70
Goffi <goffi@goffi.org>
parents: 57
diff changeset
110 debug(_("Submitting form"))
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
111 data = []
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
112 for ctrl in self.ctl_list:
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
113 if isinstance(ctrl['control'], wx.ListBox):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
114 data.append((ctrl['name'], ctrl['control'].GetStringSelection()))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
115 else:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
116 data.append((ctrl["name"], ctrl["control"].GetValue()))
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
117 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
118 id = self.misc['action_back']("SUBMIT",self.misc['target'], data)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
119 self.host.current_action_ids.add(id)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
120 elif self.misc.has_key('callback'):
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
121 self.misc['callback'](data)
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
122 else:
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 70
diff changeset
123 warning (_("The form data is not sent back, the type is not managed properly"))
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 36
diff changeset
124 self.MakeModal(False)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 36
diff changeset
125 self.Destroy()
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
126
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
127 def onFormCancelled(self, event):
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
128 """Called when cancel button is clicked"""
70
Goffi <goffi@goffi.org>
parents: 57
diff changeset
129 debug(_("Cancelling form"))
37
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 36
diff changeset
130 self.MakeModal(False)
a61beb21d16d Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents: 36
diff changeset
131 self.Close()
36
6491b7956c80 wix: Form submitting, first draft
Goffi <goffi@goffi.org>
parents: 35
diff changeset
132
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def onClose(self, event):
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
134 """Close event: we have to send the form."""
70
Goffi <goffi@goffi.org>
parents: 57
diff changeset
135 debug(_("close"))
35
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.MakeModal(False)
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
137 event.Skip()
c45deebb40a5 Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff changeset
138