File: group.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 (278 lines) | stat: -rw-r--r-- 7,845 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
##############################################################################
#
# Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# $Id: group.py 3931 2006-08-28 15:39:04Z 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.
#
##############################################################################

from rpc import RPCProxy
import rpc
from record import ModelRecord

import signal_event

try:
	a = set()
except NameError:
	from sets import Set as set

class ModelList(list):
	def __init__(self, screen):
		super(ModelList, self).__init__()
		self.__screen = screen

	def insert(self, pos, obj):
		super(ModelList, self).insert(pos, obj)
		self.__screen.signal('record-changed', ('record-added', pos))

	def append(self, obj):
		super(ModelList, self).append(obj)
		self.__screen.signal('record-changed', ('record-added', -1))
	
	def remove(self, obj):
		idx = self.index(obj)
		super(ModelList, self).remove(obj)
		self.__screen.signal('record-changed', ('record-removed', idx))
	
	def clear(self):
		for obj in range(len(self)):
			self.pop()
			self.__screen.signal('record-changed', ('record-removed', len(self)))

	def __setitem__(self, key, value):
		super(ModelList, self).__setitem__(key, value)
		self.__screen.signal('record-changed', ('record-changed', key))

class ModelRecordGroup(signal_event.signal_event):
	def __init__(self, resource, fields, ids=[], parent=None, context={}):
		super(ModelRecordGroup, self).__init__()
		self.parent = parent
		self._context = context
		self._context.update(rpc.session.context)
		self.resource = resource
		self.rpc = RPCProxy(resource)
		self.fields = fields
		self.models = ModelList(self)
		self.current_idx = None
		self.load(ids)
		self.model_removed = []
		self.on_write = ''

	def save(self):
		for model in self.models:
			saved = model.save()
			self.writen(saved)

	def writen(self, edited_id):
		if not self.on_write:
			return
		new_ids = getattr(self.rpc, self.on_write)(edited_id, self.context)
		model_idx = self.models.index(self[edited_id])
		result = False
		for id in new_ids:
			cont = False
			for m in self.models:
				if m.id == id:
					cont = True
					m.reload()
			if cont:
				continue
			newmod = ModelRecord(self.resource, id, self.fields,
								 parent=self.parent, group=self)
			newmod.reload()
			if not result:
				result = newmod
			new_index = min(model_idx, len(self.models)-1)
			self.model_add(newmod, new_index)
		return result
	
	def pre_load(self, ids, display=True):
		if not ids:
			return True
		for id in ids:
			newmod = ModelRecord(self.resource, id, self.fields,
						parent=self.parent, group=self)
			self.model_add(newmod)
			if display:
				self.signal('model-changed', newmod)
		return True

	def load(self, ids, display=True):
		if not ids:
			return True
		if not self.fields:
			return self.pre_load(ids, display)
		c = rpc.session.context.copy()
		c.update(self.context)
		values = self.rpc.read(ids, self.fields.keys(), c)
		newmod = False
		for value in values:
			newmod = ModelRecord(self.resource, value['id'], self.fields,
						parent=self.parent, group=self)
			newmod.set(value)
			self.models.append(newmod)
			newmod.signal_connect(self, 'record-changed', self._record_changed)
		if newmod and display:
			self.signal('model-changed', newmod)
		self.current_idx = 0
		return True
	
	def clear(self):
		self.models.clear()
		self.model_removed = []
	
	def getContext(self):
		ctx = {}
		ctx.update(self._context)
		#ctx['active_ids'] = [model.id for model in self.models if model.id]
		#if self.current_idx is not None:
		#	ctx['active_id'] = self.models[self.current_idx].id or False
		#else:
		#	ctx['active_id'] = False
		return ctx
	context = property(getContext)

	def model_add(self, model, position=-1):
		#assert model.mgroup is self
		fields = {}
		for mf in model.fields:
			fields[model.fields[mf].attrs['name']] = model.fields[mf].attrs
			self.fields[model.fields[mf].attrs['name']] = model.fields[mf].attrs

		self.add_fields(fields, self.models)
		self.add_fields(self.fields, [model])

		if position==-1:
			self.models.append(model)
		else:
			self.models.insert(position, model)
		self.current_idx = position
		model.parent = self.parent
		model.signal_connect(self, 'record-changed', self._record_changed)
		return model

	def model_new(self, default=True, domain=[], context={}):
		newmod = ModelRecord(self.resource, None, self.fields, group=self, 
					   parent=self.parent, new=True)
		newmod.signal_connect(self, 'record-changed', self._record_changed)
		if default:
			newmod.default_get(domain, context)
		return newmod
	
	def model_remove(self, model):
		idx = self.models.index(model)
		self.models.remove(model)
		if self.models:
			self.current_idx = min(idx, len(self.models)-1)
		else:
			self.current_idx = None

	def _record_changed(self, model, signal_data):
		self.signal('model-changed', model)

	def prev(self):
		if self.models and self.current_idx is not None:
			self.current_idx = (self.current_idx - 1) % len(self.models)
		elif self.models:
			self.current_idx = 0
		else:
			return None
		return self.models[self.current_idx]
	
	def next(self):
		if self.models and self.current_idx is not None:
			self.current_idx = (self.current_idx + 1) % len(self.models)
		elif self.models:
			self.current_idx = 0
		else:
			return None
		return self.models[self.current_idx]

	def remove(self, model):
		try:
			idx = self.models.index(model)
			if self.models[idx].id:
				self.model_removed.append(self.models[idx].id)
			self.models.remove(self.models[idx])
		except:
			pass

	def add_fields_custom(self, fields, models):
		for f in fields.keys():
			if not f in self.fields:
				self.fields[f] = fields[f]
				self.fields[f]['name'] = f
		if not len(models):
			return True

		to_add = set() 
		base = set(fields.keys())
		for model in models:
			to_add.update(base - set(model.fields_get().keys()))

		for fname in to_add:
			for model in models:
				fields[fname]['name'] = fname
				model.add_field(fields[fname])
		return list(to_add)

	def add_fields(self, fields, models):
		to_add = self.add_fields_custom(fields, models)
		if not len(models):
			return True

		old = []
		new = []
		for model in models:
			if model.id:
				old.append(model.id)
			else:
				new.append(model)
		if len(old) and len(to_add):
			c = rpc.session.context.copy()
			c.update(self.context)
			values = self.rpc.read(old, to_add, c)
			for v in values:
				id = v['id']
				del v['id']
				self[id].set(v)
		if len(new) and len(to_add):
			values = self.rpc.default_get(to_add, self.context)
			for mod in new:
				mod.set_default(values)

	def __iter__(self):
		return iter(self.models)

	def get_by_id(self, id):
		for model in self.models:
			if model.id == id:
				return model

	__getitem__ = get_by_id



# vim:noexpandtab: