|
| 1 | +================================= |
| 2 | +Export to / import from csv files |
| 3 | +================================= |
| 4 | + |
| 5 | +First, in the client module of the item we create two buttons that execute the |
| 6 | +corresponding functions when you click on them: |
| 7 | + |
| 8 | +.. code-block:: js |
| 9 | + |
| 10 | + function on_view_form_created(item) { |
| 11 | + var csv_import_btn = item.add_view_button('Import csv file'), |
| 12 | + csv_export_btn = item.add_view_button('Export csv file'); |
| 13 | + csv_import_btn.click(function() { csv_import(item) }); |
| 14 | + csv_export_btn.click(function() { csv_export(item) }); |
| 15 | + } |
| 16 | + |
| 17 | + function csv_export(item) { |
| 18 | + item.server('export_scv', function(file_name, error) { |
| 19 | + if (error) { |
| 20 | + item.alert_error(error); |
| 21 | + } |
| 22 | + else { |
| 23 | + var url = [location.protocol, '//', location.host, location.pathname].join(''); |
| 24 | + url += 'static/files/' + file_name; |
| 25 | + window.open(encodeURI(url)); |
| 26 | + } |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + function csv_import(item) { |
| 31 | + task.upload('static/files', {callback: function(file_name) { |
| 32 | + item.server('import_scv', [file_name], function(error) { |
| 33 | + if (error) { |
| 34 | + item.warning(error); |
| 35 | + } |
| 36 | + item.refresh_page(true); |
| 37 | + }); |
| 38 | + }}); |
| 39 | + } |
| 40 | + |
| 41 | +These functions execute the following functions defined in the server module. |
| 42 | +In this module we use the Python csv module. We do not export system fields - |
| 43 | +primary key field and deletion flag field. |
| 44 | + |
| 45 | +Below is the code for Python 3: |
| 46 | + |
| 47 | +.. code-block:: py |
| 48 | + |
| 49 | + import os |
| 50 | + import csv |
| 51 | + |
| 52 | + def export_scv(item): |
| 53 | + copy = item.copy() |
| 54 | + copy.open() |
| 55 | + file_name = item.item_name + '.csv' |
| 56 | + path = os.path.join(item.task.work_dir, 'static', 'files', file_name) |
| 57 | + with open(path, 'w', encoding='utf-8') as csvfile: |
| 58 | + fieldnames = [] |
| 59 | + for field in copy.fields: |
| 60 | + if not field.system_field(): |
| 61 | + fieldnames.append(field.field_name) |
| 62 | + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) |
| 63 | + writer.writeheader() |
| 64 | + for c in copy: |
| 65 | + dic = {} |
| 66 | + for field in copy.fields: |
| 67 | + if not field.system_field(): |
| 68 | + dic[field.field_name] = field.text |
| 69 | + writer.writerow(dic) |
| 70 | + return file_name |
| 71 | + |
| 72 | + def import_scv(item, file_name): |
| 73 | + copy = item.copy() |
| 74 | + path = os.path.join(item.task.work_dir, 'static', 'files', file_name) |
| 75 | + with open(path, 'r', encoding='utf-8') as csvfile: |
| 76 | + copy.open(open_empty=True) |
| 77 | + reader = csv.DictReader(csvfile) |
| 78 | + for row in reader: |
| 79 | + print(row) |
| 80 | + copy.append() |
| 81 | + for field in copy.fields: |
| 82 | + if not field.system_field(): |
| 83 | + field.text = row[field.field_name] |
| 84 | + copy.post() |
| 85 | + copy.apply() |
| 86 | + |
| 87 | +For Python 2, this code looks like this: |
| 88 | + |
| 89 | +.. code-block:: py |
| 90 | + |
| 91 | + import os |
| 92 | + import csv |
| 93 | + |
| 94 | + def export_scv2(item): |
| 95 | + copy = item.copy() |
| 96 | + copy.open() |
| 97 | + file_name = item.item_name + '.csv' |
| 98 | + path = os.path.join(item.task.work_dir, 'static', 'files', file_name) |
| 99 | + with open(path, 'wb') as csvfile: |
| 100 | + fieldnames = [] |
| 101 | + for field in copy.fields: |
| 102 | + if not field.system_field(): |
| 103 | + fieldnames.append(field.field_name.encode('utf8')) |
| 104 | + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) |
| 105 | + writer.writeheader() |
| 106 | + for c in copy: |
| 107 | + dic = {} |
| 108 | + for field in copy.fields: |
| 109 | + if not field.system_field(): |
| 110 | + dic[field.field_name.encode('utf8')] = field.text.encode('utf8') |
| 111 | + writer.writerow(dic) |
| 112 | + return file_name |
| 113 | + |
| 114 | + def import_scv2(item, file_name): |
| 115 | + copy = item.copy() |
| 116 | + path = os.path.join(item.task.work_dir, 'static', 'files', file_name) |
| 117 | + with open(path, 'rb') as csvfile: |
| 118 | + item.task.execute('delete from %s' % item.table_name) |
| 119 | + copy.open(open_empty=True) |
| 120 | + reader = csv.DictReader(csvfile) |
| 121 | + for row in reader: |
| 122 | + print(row) |
| 123 | + copy.append() |
| 124 | + for field in copy.fields: |
| 125 | + if not field.system_field(): |
| 126 | + field.text = row[field.field_name.encode('utf8')].decode('utf8') |
| 127 | + copy.post() |
| 128 | + copy.apply() |
0 commit comments