Merge lp:~fabien-morin/unifield-server/fm-us-1167 into lp:~mallorymarcot/unifield-server/us-1167
- fm-us-1167
- Merge into us-1167
Proposed by
Fabien MORIN
Status: | Needs review |
---|---|
Proposed branch: | lp:~fabien-morin/unifield-server/fm-us-1167 |
Merge into: | lp:~mallorymarcot/unifield-server/us-1167 |
Diff against target: |
263 lines (+161/-56) 2 files modified
bin/addons/msf_tools/report/report_inconsistencies.py (+143/-38) bin/addons/msf_tools/report/report_inconsistencies_xls.mako (+18/-18) |
To merge this branch: | bzr merge lp:~fabien-morin/unifield-server/fm-us-1167 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Mallory MARCOT | Pending | ||
Review via email: mp+314427@code.launchpad.net |
Commit message
Description of the change
I have improved the perf of your branch, once you test it, please merge it with your.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'bin/addons/msf_tools/report/report_inconsistencies.py' | |||
2 | --- bin/addons/msf_tools/report/report_inconsistencies.py 2016-12-01 10:09:35 +0000 | |||
3 | +++ bin/addons/msf_tools/report/report_inconsistencies.py 2017-01-12 10:58:50 +0000 | |||
4 | @@ -178,7 +178,7 @@ | |||
5 | 178 | # cached data | 178 | # cached data |
6 | 179 | self.uf_status_cache = {} | 179 | self.uf_status_cache = {} |
7 | 180 | self.prod_creator_cache = {} | 180 | self.prod_creator_cache = {} |
9 | 181 | self.inconsistent = [] | 181 | self.inconsistent = {} |
10 | 182 | 182 | ||
11 | 183 | 183 | ||
12 | 184 | def get_inconsistent_lines(self, prod_id=None): | 184 | def get_inconsistent_lines(self, prod_id=None): |
13 | @@ -189,44 +189,146 @@ | |||
14 | 189 | prod_id: You can get only SMRL with product_id, if you don't give this information you | 189 | prod_id: You can get only SMRL with product_id, if you don't give this information you |
15 | 190 | will get all inconsistents SMRL | 190 | will get all inconsistents SMRL |
16 | 191 | ''' | 191 | ''' |
17 | 192 | smrl_obj = self.pool.get('stock.mission.report.line') | ||
18 | 193 | inconsistent_id_list = [] | ||
19 | 192 | if not self.inconsistent: | 194 | if not self.inconsistent: |
20 | 193 | prod_obj = self.pool.get('product.product') | 195 | prod_obj = self.pool.get('product.product') |
22 | 194 | smrl_obj = self.pool.get('stock.mission.report.line') | 196 | prod_status_obj = self.pool.get('product.status') |
23 | 195 | 197 | ||
25 | 196 | smrl_ids = smrl_obj.search(self.cr, self.uid, [('full_view', '=', False)], context=self.localcontext) | 198 | smrl_ids = smrl_obj.search(self.cr, self.uid, [('full_view', '=', False)], order='NO_ORDER', context=self.localcontext) |
26 | 197 | 199 | ||
27 | 198 | # check stock mission report line for inconsistencies with HQ (our instance): | 200 | # check stock mission report line for inconsistencies with HQ (our instance): |
31 | 199 | self.inconsistent = [] | 201 | self.inconsistent = {} |
32 | 200 | for smrl in smrl_obj.browse(self.cr, self.uid, smrl_ids, context=self.localcontext): | 202 | read_smrl_result = smrl_obj.read(self.cr, self.uid, smrl_ids, |
33 | 201 | for prod in prod_obj.browse(self.cr, self.uid, [smrl.product_id.id], context=self.localcontext): | 203 | ['product_id', 'product_state', 'state_ud', |
34 | 204 | 'product_active', 'mission_report_id', 'default_code', | ||
35 | 205 | 'international_status_code'], | ||
36 | 206 | context=self.localcontext) | ||
37 | 207 | |||
38 | 208 | read_smrl_result_dict = {} | ||
39 | 209 | all_product_ids = [] | ||
40 | 210 | for read_smrl in read_smrl_result: | ||
41 | 211 | read_smrl_result_dict[read_smrl['id']] = read_smrl | ||
42 | 212 | all_product_ids.append(read_smrl['product_id'][0]) | ||
43 | 213 | del read_smrl_result | ||
44 | 214 | |||
45 | 215 | # read all product informations | ||
46 | 216 | product_result = prod_obj.read(self.cr, self.uid, all_product_ids, | ||
47 | 217 | ['state', 'state_ud', 'active', 'name_template', | ||
48 | 218 | 'international_status', 'default_code'], | ||
49 | 219 | context=self.localcontext) | ||
50 | 220 | product_dict = dict((x['id'], x) for x in product_result) | ||
51 | 221 | |||
52 | 222 | # get all product_status | ||
53 | 223 | prod_status_ids = prod_status_obj.search(self.cr, self.uid, [], context=self.localcontext) | ||
54 | 224 | prod_status_result = prod_status_obj.read(self.cr, self.uid, prod_status_ids, ['code'], context=self.localcontext) | ||
55 | 225 | prod_status_dict = dict((x['id'], x['code']) for x in prod_status_result) | ||
56 | 226 | |||
57 | 227 | # get all instance id and build a level dict | ||
58 | 228 | instance_obj = self.pool.get('msf.instance') | ||
59 | 229 | instance_ids = instance_obj.search(self.cr, self.uid, []) | ||
60 | 230 | instance_read_result = instance_obj.read(self.cr, self.uid, instance_ids, | ||
61 | 231 | ['level', 'name'], context=self.localcontext) | ||
62 | 232 | instance_level_dict = dict((x['id'], x) for x in | ||
63 | 233 | instance_read_result) | ||
64 | 234 | |||
65 | 235 | # get all report id and build a instance dict | ||
66 | 236 | smr_obj = self.pool.get('stock.mission.report') | ||
67 | 237 | smr_ids = smr_obj.search(self.cr, self.uid, []) | ||
68 | 238 | smr_read_result = smr_obj.read(self.cr, self.uid, smr_ids, | ||
69 | 239 | ['instance_id'], context=self.localcontext) | ||
70 | 240 | smr_instance_dict = dict((x['id'], x) for x in | ||
71 | 241 | smr_read_result) | ||
72 | 242 | |||
73 | 243 | # get all uf_status codes | ||
74 | 244 | uf_status_obj = self.pool.get('product.status') | ||
75 | 245 | uf_status_code_ids = uf_status_obj.search(self.cr, self.uid, [], context=self.localcontext) | ||
76 | 246 | uf_status_code_read_result = uf_status_obj.read(self.cr, self.uid, | ||
77 | 247 | uf_status_code_ids, ['code', 'name'], context=self.localcontext) | ||
78 | 248 | uf_status_code_dict = dict((x['code'], x) for x in | ||
79 | 249 | uf_status_code_read_result) | ||
80 | 250 | |||
81 | 251 | product_result_dict = {} | ||
82 | 252 | state_ud_dict = {} | ||
83 | 253 | for product in product_result: | ||
84 | 254 | inconsistent_id_list = [] | ||
85 | 255 | # get the lines matching this product | ||
86 | 256 | smrl_ids = smrl_obj.search(self.cr, self.uid, | ||
87 | 257 | [('full_view', '=', False), | ||
88 | 258 | ('product_id', '=', product['id'])], | ||
89 | 259 | order='NO_ORDER', context=self.localcontext) | ||
90 | 260 | |||
91 | 261 | # get the inconsistent related lines | ||
92 | 262 | for smrl_id in smrl_ids: | ||
93 | 263 | smrl = read_smrl_result_dict[smrl_id] | ||
94 | 264 | product = product_dict[smrl['product_id'][0]] | ||
95 | 202 | # in product_product state is False when empty | 265 | # in product_product state is False when empty |
96 | 203 | # in smrl product_state is '' when empty: | 266 | # in smrl product_state is '' when empty: |
120 | 204 | if not prod.state and not smrl.product_state: | 267 | if not product['state'] and not smrl['product_state']: |
121 | 205 | pass | 268 | pass |
122 | 206 | elif not prod.state and smrl.product_state: | 269 | elif not product['state'] and smrl['product_state']: |
123 | 207 | self.inconsistent.append(smrl) | 270 | inconsistent_id_list.append(smrl['id']) |
124 | 208 | continue | 271 | continue |
125 | 209 | elif prod.state and not smrl.product_state: | 272 | elif product['state'] and not smrl['product_state']: |
126 | 210 | self.inconsistent.append(smrl) | 273 | inconsistent_id_list.append(smrl['id']) |
127 | 211 | continue | 274 | continue |
128 | 212 | elif prod.state.code != smrl.product_state: | 275 | elif product['state'] and product['state'] in prod_status_dict: |
129 | 213 | self.inconsistent.append(smrl) | 276 | state_code = prod_status_dict[product['state'][0]] |
130 | 214 | continue | 277 | if state_code != smrl['product_state']: |
131 | 215 | 278 | inconsistent_id_list.append(smrl['id']) | |
132 | 216 | if not prod.state_ud and not smrl.state_ud: | 279 | continue |
133 | 217 | pass | 280 | |
134 | 218 | elif prod.state_ud != smrl.state_ud: | 281 | if not product['state_ud'] and not smrl['state_ud']: |
135 | 219 | self.inconsistent.append(smrl) | 282 | pass |
136 | 220 | continue | 283 | elif product['state_ud'] != smrl['state_ud']: |
137 | 221 | 284 | inconsistent_id_list.append(smrl['id']) | |
138 | 222 | if prod.active != smrl.product_active: # if null in DB smrl.product_active = False .... | 285 | continue |
139 | 223 | self.inconsistent.append(smrl) | 286 | |
140 | 224 | continue | 287 | if product['active'] != smrl['product_active']: # if null in DB smrl.product_active = False .... |
141 | 225 | 288 | inconsistent_id_list.append(smrl['id']) | |
142 | 226 | self.inconsistent.sort(key=lambda smrl: smrl.mission_report_id.instance_id.level) | 289 | continue |
143 | 290 | |||
144 | 291 | if inconsistent_id_list: | ||
145 | 292 | product_result_dict[product['id']] = {} | ||
146 | 293 | current_prod = product_result_dict[product['id']] | ||
147 | 294 | current_prod['smrl_list'] = [] | ||
148 | 295 | prod_state_ud = '' | ||
149 | 296 | if product['state_ud']: | ||
150 | 297 | if product['state_ud'] not in state_ud_dict: | ||
151 | 298 | product_browse_obj = prod_obj.browse(self.cr, self.uid, product['id'], context=self.localcontext) | ||
152 | 299 | state_ud_name = self.pool.get('ir.model.fields').get_browse_selection(self.cr, self.uid, product_browse_obj, 'state_ud', self.localcontext) | ||
153 | 300 | state_ud_dict[product['state_ud']] = state_ud_name | ||
154 | 301 | prod_state_ud = state_ud_dict[product['state_ud']] | ||
155 | 302 | current_prod.update({ | ||
156 | 303 | 'prod_default_code': product['default_code'], | ||
157 | 304 | 'prod_name_template': product['name_template'], | ||
158 | 305 | 'prod_international_status': product['international_status'] and product['international_status'][1] or '', | ||
159 | 306 | 'prod_state': product['state'] and product['state'][1] or '', | ||
160 | 307 | 'prod_state_ud': prod_state_ud, | ||
161 | 308 | 'prod_active': product['active'], | ||
162 | 309 | }) | ||
163 | 310 | |||
164 | 311 | # build the result list dict | ||
165 | 312 | for smrl_id in inconsistent_id_list: | ||
166 | 313 | smrl = read_smrl_result_dict[smrl_id] | ||
167 | 314 | instance_id = smr_instance_dict[smrl['mission_report_id'][0]]['instance_id'][0] | ||
168 | 315 | |||
169 | 316 | smrl_dict = { | ||
170 | 317 | 'instance_name': instance_level_dict[instance_id]['name'], | ||
171 | 318 | 'smrl_default_code': smrl['default_code'], | ||
172 | 319 | 'smrl_name_template': product['name_template'], | ||
173 | 320 | 'internationnal_status_code_name': smrl['international_status_code'] and smrl['international_status_code'][1] or '', | ||
174 | 321 | 'uf_status_code': smrl['product_state'] and uf_status_code_dict[smrl['product_state']]['name'] or '', | ||
175 | 322 | 'ud_status_code': self.get_ud_status(smrl['state_ud']), | ||
176 | 323 | 'active': smrl['product_active'], | ||
177 | 324 | 'instance_level': instance_level_dict[instance_id]['level'], | ||
178 | 325 | } | ||
179 | 326 | product_result_dict[product['id']]['smrl_list'].append(smrl_dict) | ||
180 | 327 | product_result_dict[product['id']]['smrl_list'].sort(key=lambda smrl: smrl['instance_level']) | ||
181 | 328 | self.inconsistent[product['id']] = product_result_dict[product['id']] | ||
182 | 227 | 329 | ||
183 | 228 | if prod_id: | 330 | if prod_id: |
185 | 229 | return [smrl for smrl in self.inconsistent if smrl.product_id.id == prod_id] | 331 | return self.inconsistent[prod_id] |
186 | 230 | else: | 332 | else: |
187 | 231 | return self.inconsistent | 333 | return self.inconsistent |
188 | 232 | 334 | ||
189 | @@ -238,14 +340,17 @@ | |||
190 | 238 | if not self.inconsistent: | 340 | if not self.inconsistent: |
191 | 239 | self.inconsistent = self.get_inconsistent_lines() | 341 | self.inconsistent = self.get_inconsistent_lines() |
192 | 240 | 342 | ||
201 | 241 | prod_ids = set() | 343 | prod_ids = self.inconsistent.keys() |
202 | 242 | for smrl in self.inconsistent: | 344 | |
203 | 243 | prod_ids.add(smrl.product_id.id) | 345 | # order product id by default code |
204 | 244 | 346 | prod_obj = self.pool.get('product.product') | |
205 | 245 | prod_list = self.pool.get('product.product').browse(self.cr, self.uid, list(prod_ids), context=self.localcontext) | 347 | prod_result = prod_obj.read(self.cr, self.uid, prod_ids, ['default_code'], self.localcontext) |
206 | 246 | prod_list.sort(key=lambda prod: prod.default_code) | 348 | prod_result.sort(key=lambda prod: prod['default_code']) |
207 | 247 | 349 | ordered_ids = [x['id'] for x in prod_result] | |
208 | 248 | return prod_list | 350 | |
209 | 351 | # order self.inconsistent according with this ids | ||
210 | 352 | final_result = [self.inconsistent[x] for x in ordered_ids] | ||
211 | 353 | return final_result | ||
212 | 249 | 354 | ||
213 | 250 | 355 | ||
214 | 251 | def get_uf_status(self, code): | 356 | def get_uf_status(self, code): |
215 | 252 | 357 | ||
216 | === modified file 'bin/addons/msf_tools/report/report_inconsistencies_xls.mako' | |||
217 | --- bin/addons/msf_tools/report/report_inconsistencies_xls.mako 2016-12-01 10:09:35 +0000 | |||
218 | +++ bin/addons/msf_tools/report/report_inconsistencies_xls.mako 2017-01-12 10:58:50 +0000 | |||
219 | @@ -85,26 +85,26 @@ | |||
220 | 85 | <Cell ss:StyleID="tab_header_orange"><Data ss:Type="String">Active/Inactive</Data></Cell> | 85 | <Cell ss:StyleID="tab_header_orange"><Data ss:Type="String">Active/Inactive</Data></Cell> |
221 | 86 | </Row> | 86 | </Row> |
222 | 87 | % for prod in get_products_with_inconsistencies(): | 87 | % for prod in get_products_with_inconsistencies(): |
234 | 88 | # HQ data: | 88 | # HQ data: |
224 | 89 | <Row ss:AutoFitHeight="1"> | ||
225 | 90 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${('HQ')|x}</Data></Cell> | ||
226 | 91 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod.default_code)|x}</Data></Cell> | ||
227 | 92 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod.name_template)|x}</Data></Cell> | ||
228 | 93 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod.international_status and prod.international_status.name or '')|x}</Data></Cell> | ||
229 | 94 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod.state and prod.state.name or '')|x}</Data></Cell> | ||
230 | 95 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod.state_ud and getSel(prod, 'state_ud') or '')|x}</Data></Cell> | ||
231 | 96 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${('Active' if prod.active else 'Inactive')|x}</Data></Cell> | ||
232 | 97 | </Row> | ||
233 | 98 | % for smrl in get_inconsistent_lines(prod.id): | ||
235 | 99 | <Row ss:AutoFitHeight="1"> | 89 | <Row ss:AutoFitHeight="1"> |
243 | 100 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl.mission_report_id.instance_id.name)|x}</Data></Cell> | 90 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${('HQ')|x}</Data></Cell> |
244 | 101 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl.default_code)|x}</Data></Cell> | 91 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod['prod_default_code'])|x}</Data></Cell> |
245 | 102 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl.product_id.name_template)|x}</Data></Cell> | 92 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod['prod_name_template'])|x}</Data></Cell> |
246 | 103 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(get_product_creator_name_from_code(smrl.international_status_code) or '')|x}</Data></Cell> | 93 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod['prod_international_status'] or '')|x}</Data></Cell> |
247 | 104 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(get_uf_status(smrl.product_state))|x}</Data></Cell> | 94 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod['prod_state'] or '')|x}</Data></Cell> |
248 | 105 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(get_ud_status(smrl.state_ud) or '')|x}</Data></Cell> | 95 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${(prod['prod_state_ud'] or '')|x}</Data></Cell> |
249 | 106 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${('Active' if smrl.product_active else 'Inactive')|x}</Data></Cell> | 96 | <Cell ss:StyleID="tab_header_gray"><Data ss:Type="String">${('Active' if prod['prod_active'] else 'Inactive')|x}</Data></Cell> |
250 | 107 | </Row> | 97 | </Row> |
251 | 98 | % for smrl in prod['smrl_list']: | ||
252 | 99 | <Row ss:AutoFitHeight="1"> | ||
253 | 100 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl['instance_name'])|x}</Data></Cell> | ||
254 | 101 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl['smrl_default_code'])|x}</Data></Cell> | ||
255 | 102 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl['smrl_name_template'])|x}</Data></Cell> | ||
256 | 103 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl['internationnal_status_code_name'] or '')|x}</Data></Cell> | ||
257 | 104 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl['uf_status_code'])|x}</Data></Cell> | ||
258 | 105 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${(smrl['ud_status_code'] or '')|x}</Data></Cell> | ||
259 | 106 | <Cell ss:StyleID="tab_content"><Data ss:Type="String">${('Active' if smrl['active'] else 'Inactive')|x}</Data></Cell> | ||
260 | 107 | </Row> | ||
261 | 108 | % endfor | 108 | % endfor |
262 | 109 | % endfor | 109 | % endfor |
263 | 110 | % endif | 110 | % endif |