From e26fcfa48b93010d983a84b4456c95fc84ac134f Mon Sep 17 00:00:00 2001 From: Waleed Mohsen Date: Tue, 5 Nov 2019 16:32:04 +0300 Subject: [PATCH] Fix _read_csv method Fix _read_csv method --- app_store/models/appstore_account.py | 133 ++++++++++++++------------- 1 file changed, 68 insertions(+), 65 deletions(-) diff --git a/app_store/models/appstore_account.py b/app_store/models/appstore_account.py index da125d6fe..74caffd65 100644 --- a/app_store/models/appstore_account.py +++ b/app_store/models/appstore_account.py @@ -176,8 +176,8 @@ def analyse_module(self, module_name, app_directory): for filename in fnmatch.filter(filenames, '*.xml'): self._read_xml(os.path.join(root, filename), module_overview.id) - #for filename in fnmatch.filter(filenames, '*.csv'): - # self._read_csv(filename, open( os.path.join(root, filename) ).read(), module_overview.id) + for filename in fnmatch.filter(filenames, '*.csv'): + self._read_csv(filename, os.path.join(root, filename), module_overview.id) #Create a zip of the module (TODO include dependacies) zf = zipfile.ZipFile(os.path.expanduser('~') + "/apps/" + module_name + ".zip", "w") @@ -193,73 +193,76 @@ def analyse_module(self, module_name, app_directory): exc_type, exc_obj, exc_tb = sys.exc_info() _logger.error("Line: " + str(exc_tb.tb_lineno) ) pass - - def _read_csv(self, file_name, file_content, m_id): - if "ir.model.access": - rownum = 0 - reader = csv.reader(file_content, delimiter=',') - header = [] - for row in reader: - row_dict = {'mo_id': m_id} - # Save header row. - if rownum == 0: - header = row - else: - colnum = 0 - for col in row: - #map the csv header columns to the fields - if header[colnum] == "id": - row_dict['x_id'] = col - elif header[colnum] == "model_id:id": - row_dict['model'] = col - elif header[colnum] == "group_id:id": - row_dict['group'] = col - elif header[colnum] == "perm_read": - row_dict['perm_read'] = bool(int(col)) - elif header[colnum] == "perm_write": - row_dict['perm_write'] = bool(int(col)) - elif header[colnum] == "perm_create": - row_dict['perm_create'] = bool(int(col)) - elif header[colnum] == "perm_unlink": - row_dict['perm_unlink'] = bool(int(col)) - else: - row_dict[header[colnum]] = col - - colnum += 1 + + def _read_csv(self, file_name, file_path, m_id): + if file_name == "ir.model.access.csv": + with open(file_path) as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + row_dict = {'mo_id': m_id} + row_dict['x_id'] = row['id'] + row_dict['model'] = row['model_id:id'] if 'model_id:id' in row else False + row_dict['group'] = row['group_id:id'] if 'group_id:id' in row else False + row_dict['name'] = row['name'] if 'name' in row else False + row_dict['perm_read'] = bool(int(row['perm_read'])) if 'perm_read' in row else False + row_dict['perm_write'] = bool(int(row['perm_write'])) if 'perm_write' in row else False + row_dict['perm_create'] = bool(int(row['perm_create'])) if 'perm_create' in row else False + row_dict['perm_unlink'] = bool(int(row['perm_unlink'])) if 'perm_unlink' in row else False group_name = "" - #Deal with blank or other malformed rows - if ('group' not in row_dict) or ('x_id' not in row_dict): + # Deal with blank or other malformed rows + if not row_dict['group'] or ('x_id' not in row_dict): continue + + # if the group is not empty + if row_dict['group']: + try: + # to check group without module + group_split = row_dict['group'].split(".") + if len(group_split) > 1: + group_name = self.env['ir.model.data'].get_object(row_dict['group'].split(".")[0], + row_dict['group'].split(".")[1]).display_name + else: + self.env['ir.model.data'].get_object(self._ref_to_model(row_dict['model']), + row_dict['group']) + + except ValueError: + #If the group belong to module which not inatlled the above will raise an error, so will set group name same as group + group_name = row_dict['group'] + + # Create the group if it does exist + ex_group = self.env['module.overview.group'].search([('x_id', '=', row_dict['group']),('mo_id','=',m_id)]) + + if len(ex_group) == 0: + my_group = self.env['module.overview.group'].create({'mo_id': m_id, 'name': group_name, 'x_id': row_dict['group']}) + else: + my_group = ex_group[0] + + # If model doesn't exists, create it + if row_dict['model']: + ex_model = self.env['module.overview.model'].search( + [('name', '=', self._ref_to_model(row_dict['model']))]) + + if not ex_model: + # create the model + my_model = self.env['module.overview.model'].create( + {'name': self._ref_to_model(row_dict['model']), 'mo_id': m_id}) + else: + my_model = ex_model[0] + + # Add the access rule to the group + self.env['module.overview.group.access'].create( + {'mog_id': my_group.id if my_group else False, 'model_id': my_model.id, 'perm_read': row_dict['perm_read'], + 'perm_write': row_dict['perm_write'], 'perm_create': row_dict['perm_create'], + 'perm_unlink': row_dict['perm_unlink']}) + + access_dict = {'model_id': my_model.id, 'name': group_name, 'x_id': row_dict['group'], + 'perm_read': row_dict['perm_read'], 'perm_write': row_dict['perm_write'], + 'perm_create': row_dict['perm_create'], 'perm_unlink': row_dict['perm_unlink']} + self.env['module.overview.model.access'].create(access_dict) + row_dict ={} - if row_dict['group'] != "": - group_name = self.env['ir.model.data'].get_object(row_dict['group'].split(".")[0], row_dict['group'].split(".")[1]).display_name - - #Create the group if it does exist - ex_group = self.env['module.overview.group'].search([('x_id','=',row_dict['group'])]) - - if len(ex_group) == 0: - my_group = self.env['module.overview.group'].create({'mo_id': m_id, 'name': group_name, 'x_id': row_dict['group']}) - else: - my_group = ex_group[0] - - #If model diiesn't exists, create it - ex_model = self.env['module.overview.model'].search([('name','=', self._ref_to_model(row_dict['model']) )]) - - if len(ex_model) == 0: - #create the model - my_model = self.env['module.overview.model'].create({'name':self._ref_to_model(row_dict['model']), 'mo_id': m_id}) - else: - my_model = ex_model[0] - - #Add the access rule to the group - self.env['module.overview.group.access'].create({'mog_id':my_group.id, 'model_id': my_model.id, 'perm_read': row_dict['perm_read'], 'perm_write':row_dict['perm_write'], 'perm_create':row_dict['perm_create'], 'perm_unlink':row_dict['perm_unlink']}) - - access_dict = {'model_id':my_model.id, 'name': group_name, 'x_id': row_dict['group'], 'perm_read': row_dict['perm_read'], 'perm_write':row_dict['perm_write'], 'perm_create':row_dict['perm_create'], 'perm_unlink':row_dict['perm_unlink']} - self.env['module.overview.model.access'].create(access_dict) - - rownum += 1 def _ref_to_model(self, ref_string): """Turns 'model_sms_message' into 'sms.message'""" @@ -308,4 +311,4 @@ def _read_xml(self, file_path, m_id): model = model_exist[0] #add this view to this model - self.env['module.overview.model.view'].create({'model_id': model.id, 'name': record_name, 'x_id': record_id}) \ No newline at end of file + self.env['module.overview.model.view'].create({'model_id': model.id, 'name': record_name, 'x_id': record_id})