File: form.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 (382 lines) | stat: -rw-r--r-- 11,869 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
##############################################################################
#
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# $Id: form.py 4017 2006-09-04 08:44:06Z pinky $
#
# 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 types

import gtk
import gobject
from gtk import glade
import gettext

import rpc
import win_selection
import win_preference
import win_search
import win_export
import win_import
import win_list

import common
import service
import options
import copy

import gc

from observator import oregistry
from widget.screen import Screen

class form(object):
	def __init__(self, model, res_id=False, domain=[], view_type=None, view_ids=[], window=None, context={}):
		if not view_type:
			view_type = ['form','tree']
		else:
			if view_type[0]=='tree' and not res_id:
				res_id = rpc.session.rpc_exec_auth('/object', 'execute', model, 'search', domain)

		fields = {}
		self.model = model
		self.previous_action = None
		self.glade = glade.XML(common.terp_path("terp.glade"),'win_form_container')
		self.widget = self.glade.get_widget('win_form_container')
		self.widget.show_all()
		self.fields = fields
		self.domain = domain
		self.context = context

		self.screen = Screen(self.model, view_type=view_type, context=self.context, view_ids=view_ids, domain=domain)
		self.screen.signal_connect(self, 'record-message', self._record_message)
		oregistry.add_receiver('misc-message', self._misc_message)

		self.name = self.screen.current_view.title
		self.sw = gtk.ScrolledWindow()
		self.sw.set_shadow_type(gtk.SHADOW_NONE)
		self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		self.sw.add_with_viewport(self.screen.widget)
		vp = self.sw.get_child()
		vp.set_shadow_type(gtk.SHADOW_NONE)
		self.sw.show_all()
		
		self.has_backup = False
		self.backup = {}

		self.widget.pack_start(self.sw)
		self.handlers = {
			'but_new': ('New',self.sig_new),
			'but_copy': ('Copy',self.sig_copy),
			'but_save': ('Save',self.sig_save),
			'but_save_as': ('Save As',self.sig_save_as),
			'but_import': ('Import',self.sig_import),
			'but_print_repeat': ('Repeat',self.sig_print_repeat),
			'but_remove': ('Remove',self.sig_remove),
			'but_search': ('Search',self.sig_search),
			'but_previous': ('Previous',self.sig_previous),
			'but_next': ('Next', self.sig_next),
			#'but_preference': ('Preferences', self.sig_preference),
			'but_goto_id': ('Go To ID...', self.sig_goto),
			'but_log': ('Access log', self.sig_logs),
			'but_print': ('Print', self.sig_print),
			'but_reload': (_('Reload'),self.sig_reload),
			'but_print_html': ('Print', self.sig_print_html),
			'but_action': ('Print', self.sig_action),
			'but_switch': ('Switch', self.sig_switch),
			'but_attach': ('Attach', self.sig_attach),
			'but_close': ('Close',self.sig_close)
		}
		if res_id:
			if isinstance(res_id, int):
				res_id = [res_id]
			self.screen.load(res_id)
		else:
			if len(view_type) and view_type[0]=='form':
				self.sig_new(autosave=False)

	def sig_goto(self, *args):
		if not self.modified_save():
			return
		glade2 = glade.XML(common.terp_path("terp.glade"),'dia_goto_id')
		win = glade2.get_widget('dia_goto_id')
		widget = glade2.get_widget('goto_spinbutton')
		response = win.run()
		win.destroy()
		if response == gtk.RESPONSE_OK:
			self.screen.load( [int(widget.get_value())] )

	def destroy(self):
		oregistry.remove_receiver('misc-message', self._misc_message)
		self.screen.signal_unconnect(self)
		self.screen.destroy()
		del self.screen
		del self.glade
		del self.widget
		self.sw.destroy()
		del self.sw
		gc.collect()

	def ids_get(self):
		return self.screen.ids_get()

	def id_get(self):
		return self.screen.id_get()

	def _form_action_action(self, name, value):
		id = self.sig_save(sig_new=False)
		if id:
			id2 = value[2] or id
			obj = service.LocalService('action.main')
			action_id = int(value[0])
			obj.execute(action_id, {'model':value[1], 'id': id2, 'ids': [id2]})
			self.sig_reload()

	def _form_action_object(self, name, value):
		id = self.sig_save(sig_new=False, auto_continue=False)
		if id:
			id2 = value[2] or id
			rpc.session.rpc_exec_auth('/object', 'execute', value[1], value[0], [id2], rpc.session.context)
			self.sig_reload()

	def _form_action_workflow(self, name, value):
		id = self.sig_save(sig_new=False, auto_continue=False)
		if id:
			rpc.session.rpc_exec_auth('/object', 'exec_workflow', value[1], value[0], id)
			self.sig_reload()

	def sig_attach(self, widget=None):
		id = self.screen.id_get()
		if id:
			import win_attach
			win = win_attach.win_attach(self.model, id)
			win.go()
		else:
			self.message_state(_('No resource selected !'))
		return True

	def sig_switch(self, widget=None, mode=None):
		#
		# Verifier pourquoi:
		#	1. Modif
		#	2. Switch & Cancel
		#	3. Re-Switch -> Question car modified
		#
		if not self.modified_save():
			return
		self.screen.switch_view()

	def _id_get(self):
		return self.screen.id_get()

	def sig_logs(self, widget=None):
		id = self._id_get()
		if not id:
			self.message_state(_('You have to select one resource!'))
			return False
		res = rpc.session.rpc_exec_auth('/object', 'execute', self.model, 'perm_read', [id])
		message = ''
		for line in res:
			todo = [
				('id', _('ID')),
				('create_uid', _('Creation User')),
				('create_date', _('Creation Date')),
				('write_uid', _('Latest Modification by')),
				('write_date', _('Latest Modification Date')),
				('uid', _('Owner')),
				('gid', _('Group Owner')),
				('level', _('Access Level'))
			]
			for (key,val) in todo:
				if line[key] and key in ('create_uid','write_uid','uid'):
					line[key] = line[key][1]
				message+=val+': '+str(line[key] or '/')+'\n'
		common.message(message)
		return True

	def sig_remove(self, widget=None):
		if common.sur(_('Are you sure to remove this record ?')):
			id = self.screen.remove(unlink=True)
			if not id:
				self.message_state(_('Resource not removed !'))
			else:
				self.message_state(_('Resource removed.'))

	def sig_import(self, widget=None):
		fields = []
		win = win_import.win_import(self.model, self.screen.fields, fields)
		res = win.go()

	def sig_save_as(self, widget=None):
		fields = []
		win = win_export.win_export(self.model, self.screen.ids_get(), self.screen.fields, fields)
		res = win.go()

	def sig_new(self, widget=None, autosave=True):
		if autosave:
			if not self.modified_save():
				return
		self.screen.new()
	
	def sig_copy(self, *args):
		if not self.modified_save():
			return
		res_id = self._id_get()
		new_id = rpc.session.rpc_exec_auth('/object', 'execute', self.model, 'copy', res_id, {}, rpc.session.context)
		self.screen.load([new_id])
		self.message_state(_('Working now on the duplicated document !'))

	def _form_save(self, auto_continue=True):
		pass

	def sig_save(self, widget=None, sig_new=True, auto_continue=True):
		modification = self.screen.current_model.id
		id = self.screen.save_current()
		if id:
			self.message_state(_('Document saved !'))
			if not modification:
				self.screen.new()
		else:
			self.message_state(_('Invalid form, correct red fields !'))
		return bool(id)

	def sig_previous(self, widget=None):
		if not self.modified_save():
			return
		self.screen.display_prev()
		self.message_state('')

	def sig_next(self, widget=None):
		if not self.modified_save():
			return
		self.screen.display_next()
		self.message_state('')

	def sig_reload(self):
		if self.screen.current_view.view_type == 'form':
			self.screen.cancel_current()
			self.screen.display()
		else:
			id = self.screen.id_get()
			ids = self.screen.ids_get()
			self.screen.clear()
			self.screen.load(ids)
			for model in self.screen.models:
				if model.id == id:
					self.screen.current_model = model
					self.screen.display()
					break
		self.message_state('')

	def sig_action(self, keyword='client_action_multi', previous=False, report_type='pdf', adds={}):
		ids = self.screen.ids_get()
		if self.screen.current_model:
			id = self.screen.current_model.id
		else:
			id = False
		if self.screen.current_view.view_type == 'form':
			id = self.screen.save_current()
			if not id:
				return False
			ids = [id]
		if len(ids):
			obj = service.LocalService('action.main')
			if previous and self.previous_action:
				obj._exec_action(self.previous_action[1], {'model':self.screen.resource, 'id': id or False, 'ids':ids, 'report_type': report_type})
			else:
				res = obj.exec_keyword(keyword, {'model':self.screen.resource, 'id': id or False, 'ids':ids, 'report_type': report_type}, adds=adds)
				if res:
					self.previous_action = res
			self.sig_reload()
		else:
			self.message_state(_('No record selected!'))

	def sig_print_repeat(self):
		self.sig_action('client_print_multi', True)

	def sig_print_html(self):
		self.sig_action('client_print_multi', report_type='html')

	def sig_print(self):
		self.sig_action('client_print_multi', adds={'Print Screen': {'report_name':'printscreen.list', 'name':'Print Screen', 'type':'ir.actions.report.xml'}})

	def sig_search(self, widget=None):
		if not self.modified_save():
			return
		dom = self.domain
		win = win_search.win_search(self.model, domain=self.domain, context=self.context)
		res = win.go()
		if res:
			self.screen.clear()
			self.screen.load(res)

	def message_state(self, message, context='message'):
		sb = self.glade.get_widget('stat_state')
		cid = sb.get_context_id(context)
		sb.push(cid, message)

	def _record_message(self, screen, signal_data):
		if not signal_data[1]:
			msg = _('No record selected')
		else:
			name = '_'
			if signal_data[0]>=0:
				name = str(signal_data[0]+1)
			name2 = _('New document')
			if signal_data[2]:
				name2 = _('Editing document (id: ')+str(signal_data[2])+')'
			msg = _('Record: ')+name+' / '+str(signal_data[1])+' - '+name2
		sb = self.glade.get_widget('stat_form')
		cid = sb.get_context_id('message')
		sb.push(cid, msg)

	def _misc_message(self, obj, message):
		self.message_state(message)

	def sig_preference(self, widget=None):
		actions = rpc.session.rpc_exec_auth('/object', 'execute', 'ir.values', 'get', 'meta', False, [(self.model,False)], True, rpc.session.context, True)
		id = self.screen.id_get()
		if id and len(actions):
			win = win_preference.win_preference(self.model, id, actions)
			win.run()
		elif id:
			self.message_state(_('No preference available for this resource !'))
		else:
			self.message_state(_('No resource selected !'))

	def modified_save(self):
		if self.screen.is_modified():
			value = common.sur_3b(_('This record has been modified\ndo you want to save it ?'))
			if value == 'ok':
				return self.sig_save()
			elif value == 'ko':
				self.sig_reload()
				return True
			else:
				return False
		return True

	def sig_close(self, urgent=False):
		return self.modified_save()