-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathupdate_item_barcode.py
68 lines (47 loc) · 1.84 KB
/
update_item_barcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import print_function
import frappe
import csv
def get_progress_bar(progress):
"""Returns progress bar based on the number"""
progress_line = "----------".replace('-', '#', progress)
return '[{:<10s}]'.format(progress_line)
def extract_to_csv():
"""Extract Items to a CSV file"""
fields = ['name', 'barcode']
filters = {'disabled': 0}
# Items
items = frappe.get_all('Item', filters=filters, fields=fields)
# File to saved to
file = open('barcode.csv', 'w')
# Opening the file
with file:
writer = csv.DictWriter(file, fieldnames=fields)
writer.writeheader()
# Max length of the items
max_length = len(items)
# Write item rows
for i, item in enumerate(items):
# Carriage return
end = '\r'
# For printing only
if i + 1 == max_length:
end = '\n'
progress = int((float(i+1) / float(max_length)) * 10)
print('\rWriting {0}/{1} to the csv {2}'.format(i + 1, max_length, get_progress_bar(progress)), end=end)
writer.writerow(item)
def import_from_csv():
"""Import the barcodes from the csv file"""
with open('./barcode.csv') as csvfile:
reader = csv.DictReader(csvfile)
rows = list(reader)
# Total items loaded
max_length = len(rows)
for i, row in enumerate(rows):
# Carriage return
end = '\r'
# For printing only
if i + 1 == max_length:
end = '\n'
progress = int((float(i + 1) / float(max_length)) * 10)
print('\rUpdating {0}/{1} to the Item {2}'.format(i + 1, max_length, get_progress_bar(progress)), end=end)
frappe.db.sql("""UPDATE `tabItem` SET barcode=%s WHERE name=%s""", (row['barcode'], row['name']))