File: wizard.py

package info (click to toggle)
tinyerp-client 3.4.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,832 kB
  • ctags: 1,024
  • sloc: python: 7,566; sh: 2,253; makefile: 81
file content (124 lines) | stat: -rw-r--r-- 4,068 bytes parent folder | download
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
##############################################################################
#
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
#                    Fabien Pinckaers <fp@tiny.Be>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import gtk
from gtk import glade
import gettext
import copy

import service
import rpc
import common

from widget.screen import Screen

class dialog(object):
	def __init__(self, arch, fields, state, name):
		buttons = []
		self.states=[]
		for x in state:
			buttons.append(x[1])
			buttons.append(len(self.states))
			self.states.append(x[0])
		self.dia = gtk.Dialog('Tiny ERP', None,
			gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
			tuple(buttons))

		val = {}
		for f in fields:
			if 'value' in fields[f]:
				val[f] = fields[f]['value']

		self.screen = Screen('wizard.'+name, view_type=[])
		self.screen.new(default=False)
		self.screen.add_view_custom(arch, fields, display=True)
		self.screen.current_model.set(val)

		x,y = self.screen.screen_container.size_get()
		self.screen.widget.set_size_request(x,y)

		self.dia.vbox.pack_start(self.screen.widget)
		self.dia.set_title(self.screen.current_view.title)
		self.dia.show_all()

	def run(self, datas={}):
		while True:
			res = self.dia.run()
			self.screen.current_view.set_value()
			if self.screen.current_model.validate() or (res<0) or (self.states[res]=='end'):
				break
			self.screen.display()
		if res<len(self.states) and res>=0:
			datas.update(self.screen.get())
			self.dia.destroy()
			return (self.states[res], datas)
		else:
			self.dia.destroy()
			return False
	

def execute(action, datas, state='init'):
	if not 'form' in datas:
		datas['form'] = {}
	wiz_id = rpc.session.rpc_exec_auth('/wizard', 'create', action)
	while state!='end':
		res = rpc.session.rpc_exec_auth('/wizard', 'execute', wiz_id, datas, state, rpc.session.context)
		if 'datas' in res:
			datas['form'].update( res['datas'] )
		if res['type']=='form':
			dia = dialog(res['arch'], res['fields'], res['state'], action)
			dia.screen.current_model.set( datas['form'] )
			res = dia.run(datas['form'])
			if not res:
				break
			(state, new_data) = res
			for d in new_data:
				if new_data[d]==None:
					del new_data[d]
			datas['form'].update(new_data)
			del new_data
		elif res['type']=='action':
			obj = service.LocalService('action.main')
			obj._exec_action(res['action'],datas)
			state = res['state']
		elif res['type']=='print':
			obj = service.LocalService('action.main')
			datas['report_id'] = res.get('report_id', False)
			if res.get('get_id_from_action', False):
				backup_ids = datas['ids']
				datas['ids'] = datas['form']['ids']
				win = obj.exec_report(res['report'], datas)
				datas['ids'] = backup_ids
			else:
				win = obj.exec_report(res['report'], datas)
			state = res['state']
		elif res['type']=='state':
			state = res['state']
		#common.error('Wizard Error:'+ str(e.type), e.message, e.data)
		#state = 'end'