Skip to content

Commit

Permalink
WIP: Add extra_info when CSV is loaded in Lot Enviament
Browse files Browse the repository at this point in the history
  • Loading branch information
pauboixsom committed Feb 7, 2022
1 parent fcde972 commit a643b96
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
11 changes: 10 additions & 1 deletion som_infoenergia/som_infoenergia_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ def create_enviaments_from_object_list(self, cursor, uid, ids, object_ids, conte
lot_info = self.read(cursor, uid, ids, ['name','tipus'])
context['tipus'] = lot_info['tipus']
job_ids = []

for obj_id in object_ids:
job = self.create_single_enviament_from_object_async(cursor, uid, ids, obj_id, context=context)
if context.get('extra_text', False):
env_obj = self.pool.get('som.enviament.massiu')
env_data = env_obj.read(cursor, uid, obj_id, ['name'])
contexte = context
contexte['extra_text'] = context['extra_text'][env_data['name']]

job = self.create_single_enviament_from_object_async(cursor, uid, ids, obj_id, context=contexte)
job_ids.append(job.id)
# Create a jobs_group to see the status of the operation
create_jobs_group(
Expand Down Expand Up @@ -141,6 +148,8 @@ def create_single_enviament_from_object(self, cursor, uid, ids, object_id, conte
env_obj = self.pool.get('som.enviament.massiu')

env_values.update({context['from_model']: object_id})
if 'extra_text' in context:
env_values.update({'extra_text': context['extra_text']})

env_id = env_obj.search(cursor, uid, [('lot_enviament','=', ids), (context['from_model'], '=', object_id)])
if not env_id:
Expand Down
38 changes: 38 additions & 0 deletions som_infoenergia/tests/test_wizards.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,41 @@ def test_create_enviaments_from_csv__infoenergia_many(self, mock_create):
mock_create.assert_called_with(self.cursor, self.uid, lot_enviament_id, [pol_id_2, pol_id_3], {'from_model': 'polissa_id'})
wiz_info = wiz_obj.read(self.cursor, self.uid, [wiz_id], ['info'])[0]['info']
self.assertEqual(wiz_info, "Es crearan els enviaments de 2 pòlisses en segon pla")



@mock.patch('som_infoenergia.som_infoenergia_lot.SomInfoenergiaLotEnviament.create_enviaments_from_object_list')
def test_create_enviaments_from_csv__massive_extra_info(self, mock_create):
wiz_obj = self.openerp.pool.get('wizard.create.enviaments.from.csv')
imd_obj = self.openerp.pool.get('ir.model.data')
pol_obj = self.openerp.pool.get('giscedata.polissa')
lot_env_obj = self.openerp.pool.get('som.infoenergia.lot.enviament')
env_obj = self.openerp.pool.get('som.enviament.massiu')
lot_enviament_id = imd_obj.get_object_reference(
self.cursor, self.uid, 'som_infoenergia', 'lot_enviament_0002'
)[1]
pol_id_2 = imd_obj.get_object_reference(
self.cursor, self.uid, 'giscedata_polissa', 'polissa_0002'
)[1]
pol_id_3 = imd_obj.get_object_reference(
self.cursor, self.uid, 'giscedata_polissa', 'polissa_0003'
)[1]
pol_name_2 = pol_obj.read(self.cursor, self.uid, pol_id_2, ['name'])['name']
pol_name_3 = pol_obj.read(self.cursor, self.uid, pol_id_3, ['name'])['name']
csv_content = "polissa,extra_info,k\n{},Info 1,234\n{},Info 2,213".format(pol_name_2, pol_name_3)
encoded_csv = base64.b64encode(csv_content)
vals = {
'csv_file': encoded_csv,
}

ctx = {
'active_id': lot_enviament_id, 'active_ids': [lot_enviament_id],
}

wiz_id = wiz_obj.create(self.cursor, self.uid, vals, context=ctx)
#import pudb;pu.db
wiz_obj.create_from_file(self.cursor, self.uid, [wiz_id], context=ctx)

mock_create.assert_called_with(self.cursor, self.uid, lot_enviament_id, [pol_id_2, pol_id_3], {'from_model': 'polissa_id', 'extra_text': {'0002': {'k': '234', 'extra_info': 'Info 1'}, '0003': {'k': '213', 'extra_info': 'Info 2'}}})
wiz_info = wiz_obj.read(self.cursor, self.uid, [wiz_id], ['info'])[0]['info']
self.assertEqual(wiz_info, "Es crearan els enviaments de 2 pòlisses en segon pla")
26 changes: 23 additions & 3 deletions som_infoenergia/wizard/wizard_create_enviaments_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,34 @@ def create_from_file(self, cursor, uid, ids, context=None):
lot_obj = self.pool.get('som.infoenergia.lot.enviament')
pol_obj = self.pool.get('giscedata.polissa')
wiz = self.browse(cursor, uid, ids[0], context=context)

vals = {'from_model': 'polissa_id'}
csv_file = StringIO(base64.b64decode(wiz.csv_file))
reader = csv.reader(csv_file)
pol_list = [line[0] for line in list(reader)]
linies= list(reader)
n_linies = len(linies)
start = 0
if n_linies>0 and not linies[0][0].isdigit():
header = linies[0]
start=1

pol_list= []
result = {}
for line in linies[start:]:
i = 1
result_extra_info = {}
for column in line[1:]:
result_extra_info[header[i]] = column #TODO header without value
i += 1
if result_extra_info:
result[line[0]] = result_extra_info
pol_list.append(line[0])
if result:
vals['extra_text'] = result
#pol_list = [pol for pol in result.keys()]

lot_id = context.get('active_id', [])
pol_ids = pol_obj.search(cursor, uid, [('name','in', pol_list)])
lot_obj.create_enviaments_from_object_list(cursor, uid, lot_id, pol_ids, {'from_model': 'polissa_id'})
lot_obj.create_enviaments_from_object_list(cursor, uid, lot_id, pol_ids, vals)
msg = "Es crearan els enviaments de {} pòlisses en segon pla".format(len(pol_ids))
wiz.write({'state': "finished", 'info': msg})
return True
Expand Down

0 comments on commit a643b96

Please sign in to comment.