Mercurial > libervia-backend
annotate frontends/wix/form.py @ 101:783e9d6980ec
Couchsurfing plugin: first draft
SàT core: adding additionnal menu method
bridge: new methods getMenus, getMenuHelp and callMenu
wix: new menu are added on startup
CS plugin: login on CS
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Jun 2010 17:15:30 +0800 |
parents | 2503de7fb4c7 |
children |
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 | 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 | 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 | 32 """Create a form from a SàT xml""" |
33 | |
34 def __init__(self, host, xml_data='', title="Form", options=[], misc={}): | |
92 | 35 style = wx.DEFAULT_FRAME_STYLE & ~wx.CLOSE_BOX if 'NO_CANCEL' in options else wx.DEFAULT_FRAME_STYLE #FIXME: gof: Q&D tmp hack |
36 super(Form, self).__init__(None, title=title, style=style) | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 self.host = host |
91 | 39 self.options = options |
40 self.misc = misc | |
36 | 41 self.ctl_list = [] # usefull to access ctrl |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 self.sizer = wx.BoxSizer(wx.VERTICAL) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 self.SetSizer(self.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 self.SetAutoLayout(True) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 #events |
91 | 48 if not 'NO_CANCEL' in self.options: |
49 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.MakeModal() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.constructForm(xml_data) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 self.Show() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def constructForm(self, xml_data): |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 panel=wx.Panel(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 panel.sizer = wx.BoxSizer(wx.VERTICAL) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 cat_dom = minidom.parseString(xml_data.encode('utf-8')) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 for elem in cat_dom.documentElement.getElementsByTagName("elem"): |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 name = elem.getAttribute("name") |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 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
|
66 type = elem.getAttribute("type") |
91 | 67 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
|
68 sizer = wx.BoxSizer(wx.HORIZONTAL) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 if type=="text": |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 ctrl = wx.StaticText(panel, -1, value) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 elif type=="string": |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 label=wx.StaticText(panel, -1, label+": ") |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 ctrl = wx.TextCtrl(panel, -1, value) |
36 | 74 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
|
75 sizer.Add(label) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 elif type=="password": |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 label=wx.StaticText(panel, -1, label+": ") |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 ctrl = wx.TextCtrl(panel, -1, value, style=wx.TE_PASSWORD) |
36 | 79 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
|
80 sizer.Add(label) |
91 | 81 elif type=="list": |
82 label=wx.StaticText(panel, -1, label+": ") | |
83 ctrl = wx.ListBox(panel, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE) | |
84 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
|
85 else: |
70 | 86 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
|
87 raise NotImplementedError |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 sizer.Add(ctrl, 1, flag=wx.EXPAND) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 #self.ctl_list[(name, category)] = ctrl |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 panel.sizer.Add(sizer, flag=wx.EXPAND) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 |
36 | 92 dialogButtons = wx.StdDialogButtonSizer() |
91 | 93 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit")) |
36 | 94 dialogButtons.AddButton(submitButton) |
91 | 95 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton) |
96 if not 'NO_CANCEL' in self.options: | |
97 cancelButton = wx.Button(panel,wx.ID_CANCEL) | |
98 dialogButtons.AddButton(cancelButton) | |
99 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton) | |
36 | 100 dialogButtons.Realize() |
101 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
|
102 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 panel.SetSizer(panel.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 panel.SetAutoLayout(True) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 panel.sizer.Fit(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self.sizer.Add(panel, 1, flag=wx.EXPAND) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 cat_dom.unlink() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
36 | 109 def onFormSubmitted(self, event): |
110 """Called when submit button is clicked""" | |
70 | 111 debug(_("Submitting form")) |
36 | 112 data = [] |
113 for ctrl in self.ctl_list: | |
91 | 114 if isinstance(ctrl['control'], wx.ListBox): |
115 data.append((ctrl['name'], ctrl['control'].GetStringSelection())) | |
116 else: | |
117 data.append((ctrl["name"], ctrl["control"].GetValue())) | |
118 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned | |
119 id = self.misc['action_back']("SUBMIT",self.misc['target'], data) | |
120 self.host.current_action_ids.add(id) | |
121 elif self.misc.has_key('callback'): | |
122 self.misc['callback'](data) | |
123 else: | |
124 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
|
125 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
126 self.Destroy() |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
36 | 128 def onFormCancelled(self, event): |
129 """Called when cancel button is clicked""" | |
70 | 130 debug(_("Cancelling form")) |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
131 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
132 self.Close() |
36 | 133 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 def onClose(self, event): |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 """Close event: we have to send the form.""" |
70 | 136 debug(_("close")) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 self.MakeModal(False) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 event.Skip() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |