Merge lp:~syleam/wms/5.0-multi-scanner into lp:wms

Proposed by Christophe CHAUVET
Status: Merged
Merged at revision: 238
Proposed branch: lp:~syleam/wms/5.0-multi-scanner
Merge into: lp:wms
Diff against target: 217 lines (+69/-19)
3 files modified
wms_scanner/object/scanner.py (+62/-18)
wms_scanner/scripts/import_scenario.py (+2/-0)
wms_scanner/view/scanner.xml (+5/-1)
To merge this branch: bzr merge lp:~syleam/wms/5.0-multi-scanner
Reviewer Review Type Date Requested Status
Christophe CHAUVET Approve
Review via email: mp+73397@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Christophe CHAUVET (christophe-chauvet) wrote :

Ok

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'wms_scanner/object/scanner.py'
2--- wms_scanner/object/scanner.py 2011-08-25 09:01:52 +0000
3+++ wms_scanner/object/scanner.py 2011-08-30 15:49:16 +0000
4@@ -28,6 +28,7 @@
5 import uuid
6 import netsvc
7 from tools import config
8+from threading import Semaphore
9
10 logger = netsvc.Logger()
11
12@@ -46,8 +47,11 @@
13 'title': fields.char('Title', size=256, help='Title for this scenario'),
14 'note': fields.text('Note', help='Store different note, date and title for modification, etc...'),
15 'resid': fields.char('Reference id', size=64, readonly=True, help="Use by import/export Scenario"),
16+ 'shared_custom': fields.boolean('Shared Chustom', help="Allows to shared the custom values with a shared scanner in the same warehouse"),
17 }
18
19+ _order = 'sequence'
20+
21 _defaults = {
22 'title': lambda *a: False,
23 }
24@@ -56,6 +60,9 @@
25 ('resid_uniq', 'unique (resid)', 'The resid of the Step must be unique !'),
26 ]
27
28+ ## dict to save current semarophe {scenario: {warehouse: {refdoc: instance of semaphore}}}
29+ _semaphore = {}
30+
31 def create(self, cr, uid, values, context=None):
32 """
33 if the resid not in value, we will create with uuid1
34@@ -64,6 +71,28 @@
35 values['resid'] = uuid.uuid1()
36 return super(ScannerScenario, self).create(cr, uid, values, context=context)
37
38+ def _semaphore_acquire(self, cr, uid, id, warehouse_id, ref_doc, context=None):
39+ """
40+ make an acquire on a semaphore to take one jeton
41+ the semaphore is use like a mutex one on one
42+ """
43+ sem = self._semaphore.get(id, {}).get(warehouse_id, {}).get(ref_doc, None)
44+ if not sem:
45+ sem = Semaphore()
46+ if not self._semaphore.get(id):
47+ self._semaphore[id] = {}
48+ if not self._semaphore[id].get(warehouse_id):
49+ self._semaphore[id][warehouse_id] = {}
50+ self._semaphore[id][warehouse_id][ref_doc] = sem
51+ sem.acquire()
52+
53+ def _semaphore_release(self, cr, uid, id, warehouse_id, ref_doc, context=None):
54+ """
55+ make a release on a semaphore to take one jeton
56+ the semaphore is use like a mutex one on one
57+ """
58+ self._semaphore[id][warehouse_id][ref_doc].release()
59+
60 ScannerScenario()
61
62
63@@ -115,22 +144,29 @@
64 'text_val': lambda *a: '',
65 }
66
67- def _get_values(self, cr, uid, scenario_id, scanner_id, model='', res_id=None, domain=None, context=None):
68+ def _get_domain(self, cr, uid, scenario, scanner, context=None):
69+ """
70+ create a domain to found custom values.
71+ use the fields shared_custom of scenario and scanner
72+ """
73+ if scenario.shared_custom == True:
74+ return [('scenario_id', '=', scenario.id), ('scanner_id.ref_doc', '=', scanner.ref_doc), ('scanner_id.warehouse_id', '=', scanner.warehouse_id.id)]
75+ return [('scenario_id', '=', scenario.id), ('scanner_id', '=', scanner.id)]
76+
77+ def _get_values(self, cr, uid, scenario, scanner, model='', res_id=None, domain=None, context=None):
78 """
79 return read customs line
80 @param domain : list of tuple for search method
81 """
82 if domain is None:
83- domain = [('scenario_id', '=', scenario_id), ('scanner_id', '=', scanner_id)]
84+ domain = self._get_domain(cr, uid, scenario, scanner, context=context)
85 else:
86- domain.extend([('scenario_id', '=', scenario_id), ('scanner_id', '=', scanner_id)])
87+ domain.extend(self._get_domain(cr, uid, scenario, scanner, context=context))
88 if model:
89 domain.append(('model', '=', model))
90 if res_id:
91 domain.append(('res_id', '=', res_id))
92- print domain
93 ids = self.search(cr, uid, domain, context=context)
94- print ids
95 if ids:
96 return self.read(cr, uid, ids, [], context=context)
97 return []
98@@ -151,15 +187,21 @@
99 pass
100 return self.write(cr, uid, ids, vals, context=context)
101
102- def _remove_values(self, cr, uid, scenario_id, scanner_id, context=None):
103+ def _remove_values(self, cr, uid, scenario, scanner, context=None):
104 """
105 Unlink all the line link we one scenarii and one scanner
106 """
107- domain = [('scenario_id', '=', scenario_id), ('scanner_id', '=', scanner_id)]
108+ domain = []
109+ scanner_ids = []
110+ if scenario.shared_custom == True:
111+ scanner_ids = self.pool.get('scanner.hardware').search(cr, uid, [('scenario_id', '=', scenario.id), ('warehouse_id', '=', scanner.warehouse_id.id), ('ref_doc', '=', scanner.ref_doc), ('id', '!=', scanner.id)], context=context)
112+ domain = [('scenario_id', '=', scenario.id), ('scanner_id', '=', scanner.id)]
113 ids = self.search(cr, uid, domain, context=context)
114- if ids:
115+ if scanner_ids:
116+ return self.write(cr, uid, ids, {'scanner_id': scanner_ids[0]}, context=context)
117+ else:
118 return self.unlink(cr, uid, ids, context=context)
119- return True
120+
121
122 ScannerScenarioCustom()
123
124@@ -311,7 +353,6 @@
125 r = self._scenario_save(cr, uid, term.id, message, etype)
126 return r
127 elif action == 'action' and term.scenario_id:
128- logger.notifyChannel('wms_scanner', netsvc.LOG_INFO, '[%s] Action : %s (%s)' % (numterm, message, term.scenario_id.name))
129 r = self._scenario_save(cr, uid, term.id, message, etype, term.scenario_id.id, term.step_id.id, term.ref_doc)
130 return r
131
132@@ -329,9 +370,9 @@
133 because if we want reset term when error we must use sql querry, it is bad in production
134 """
135 custom_obj = self.pool.get('scanner.scenario.custom')
136- for read in self.read(cr, uid, ids, ['scenario_id'], context=context):
137- if read.get('scenario_id'):
138- custom_obj._remove_values(cr, uid, read['scenario_id'][0], read['id'], context=context)
139+ for scanner in self.browse(cr, uid, ids, context=context):
140+ if scanner.scenario_id and scanner.scenario_id.id:
141+ custom_obj._remove_values(cr, uid, scanner.scenario_id, scanner, context=context)
142 self.write(cr, uid, ids, {'scenario_id': False, 'step_id': False, 'ref_doc': 0, 'tmp_val1': '', 'tmp_val2': '', 'tmp_val3': '', 'tmp_val4': '', 'tmp_val5': ''}, context=context)
143 return True
144
145@@ -432,7 +473,9 @@
146 return ('U', ['Please contact', 'your', 'administrator'], 0)
147
148 self._memorize(cr, uid, term_id, scenario_id, step_id, cur_obj, context)
149-
150+ # MUTEXT
151+ t = term.browse(cr, uid, term_id, context=context)
152+ scenario._semaphore_acquire(cr, uid, t.scenario_id.id, t.warehouse_id.id, t.ref_doc, context=context)
153 # execute the step and return the result
154 stp = step.browse(cr, uid, step_id, context=context)
155 logger.notifyChannel('wms_scanner', netsvc.LOG_INFO, 'Model: %s' % stp.scenario_id.model_id.model)
156@@ -445,7 +488,7 @@
157 'term': term,
158 'context': context,
159 'm': message,
160- 't': term.browse(cr, uid, term_id, context=context),
161+ 't': t,
162 'tracer': tracer,
163 'wkf': netsvc.LocalService('workflow'),
164 'scenario': self.pool.get('scanner.scenario').browse(cr, uid, scenario_id, context=context),
165@@ -456,11 +499,12 @@
166 self.empty_scanner_values(cr, uid, [term_id], context=context)
167 except osv.except_osv, e:
168 logger.notifyChannel('wms_scanner', netsvc.LOG_WARNING, 'OSV Exception: %s' % str(e))
169- return ('U', ['Please contact', 'your', 'administrator'], 0)
170+ ld.update({'act': 'U', 'res': ['Please contact', 'your', 'administrator'], 'val': 0})
171 except Exception, e:
172 logger.notifyChannel('wms_scanner', netsvc.LOG_WARNING, 'Exception: %s' % str(e))
173- return ('U', ['Please contact', 'your', 'administrator'], 0)
174-
175+ ld.update({'act': 'U', 'res': ['Please contact', 'your', 'administrator'], 'val': 0})
176+ finally:
177+ scenario._semaphore_release(cr, uid, t.scenario_id.id, t.warehouse_id.id, t.ref_doc, context=context)
178 return (ld.get('act', 'M'), ld.get('res', ['nothing']), ld.get('val', 0))
179
180 def _scenario_list(self, cr, uid, warehouse_id):
181
182=== modified file 'wms_scanner/scripts/import_scenario.py'
183--- wms_scanner/scripts/import_scenario.py 2011-07-28 07:51:17 +0000
184+++ wms_scanner/scripts/import_scenario.py 2011-08-30 15:49:16 +0000
185@@ -100,6 +100,8 @@
186 warehouse_ids = warehouse_obj.search([('name', '=', node.text)], 0, None, None, {'active_test': False})
187 if warehouse_ids:
188 scen_vals['warehouse_ids'].append((4, warehouse_ids[0]))
189+ elif node.tag == 'shared_custom':
190+ scen_vals[node.tag] = eval(node.text) or False
191 else:
192 scen_vals[node.tag] = node.text or False
193 scen_vals['model_id'] = model_obj.search([('model', '=', scen_vals['model_id'])], 0, None, None, {'active_test': False}) or False
194
195=== modified file 'wms_scanner/view/scanner.xml'
196--- wms_scanner/view/scanner.xml 2011-08-23 07:27:37 +0000
197+++ wms_scanner/view/scanner.xml 2011-08-30 15:49:16 +0000
198@@ -111,6 +111,7 @@
199 <field name="name"/>
200 <field name="model_id"/>
201 <field name="title"/>
202+ <field name="shared_custom"/>
203 </tree>
204 </field>
205 </record>
206@@ -126,7 +127,10 @@
207 <field name="active" select="2"/>
208 <field name="sequence"/>
209 <field name="model_id"/>
210- <field name="title" colspan="4"/>
211+ <group colspan="4">
212+ <field name="title" colspan="3"/>
213+ <field name="shared_custom" colspan="1" select="2"/>
214+ </group>
215 <notebook colspan="4">
216 <page string="Steps">
217 <field name="step_ids" nolabel="1" mode="tree,form">

Subscribers

People subscribed via source and target branches