Skip to content

Instantly share code, notes, and snippets.

@florentx
Last active April 26, 2017 07:22
Show Gist options
  • Save florentx/85abf16b8a814bbba10b to your computer and use it in GitHub Desktop.
Save florentx/85abf16b8a814bbba10b to your computer and use it in GitHub Desktop.
CSV import alternative - (partial implementation) - Odoo 6.1
# -*- coding: utf-8 -*-
# Example of CSV import usage
from openerp import SUPERUSER_ID
from openerp.osv.orm import Model, fields
from openerp.addons.my_base.openerp_tools import convert_csv_import
class BetterZip(Model):
"""Zip object."""
_name = "res.better.zip"
_description = __doc__
_order = "name"
_columns = {
'name': fields.char('ZIP', size=64, required=True),
'city': fields.char('City', size=64, required=True),
}
def init(self, cr):
# Initialize the table of Zip codes, or add missing ones
# But do not update existing entries
convert_csv_import(cr, 'better_zip', 'res.better.zip.csv')
def name_get(self, cr, uid, ids, context=None):
res = []
for r in self.browse(cr, uid, ids):
res.append((r.id, u"%s %s" % (r.name, r.city)))
return res
def reload_from_filesystem(self, cr, uid, context=None):
"""Allow administrator to refresh the Zip codes table."""
assert uid == SUPERUSER_ID
convert_csv_import(cr, 'better_zip', 'res.better.zip.csv', noupdate=False)
# -*- coding: utf-8 -*-
"""Generic tools for OpenERP."""
import csv
import os.path
from openerp import SUPERUSER_ID, pooler
from openerp.conf import addons_paths
__all__ = ['get_resource', 'convert_csv_import']
def get_resource(path):
"""Return the absolute path, if it exists."""
# Inspired by get_module_resource in openerp/modules/module.py
for addons_path in addons_paths:
abspath = os.path.join(addons_path, path)
if os.path.exists(abspath):
return abspath
def convert_csv_import(cr, module, filename, mode='init', noupdate=True):
"""Import records from a CSV file.
This is the same as standard OpenERP loading except that:
* by default noupdate=True is applied (i.e. records are not updated)
* entries in ir.model.data are marked "noupdate=True" to avoid deletion
Related issue:
* https://bugs.launchpad.net/openobject-server/+bug/1024114
"""
# Extract model name from the filename (e.g. "res.partner-blah.csv")
model = os.path.splitext(filename)[0].split('-')[0]
# File is located inside "data/" subfolder
filepath = get_resource(os.path.join(module, 'data', filename))
assert filepath
pool = pooler.get_pool(cr.dbname)
# Parse and decode the CSV file
with open(filepath, 'rb') as csvfile:
reader = csv.reader(csvfile, quotechar='"', delimiter=',')
fields = reader.next()
data = []
for line in reader:
if line and any(line):
record = {}
for (f, cell) in zip(fields, line):
record[f] = cell.decode('utf-8')
data.append(record)
try:
return pool[model].import_records(
cr, SUPERUSER_ID, fields, data, mode, module, noupdate)
except Exception as exc:
raise RuntimeError('CSV import failed: file %s could not be processed:\n %s' %
(filepath, exc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment