-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_products.py
executable file
·57 lines (47 loc) · 1.67 KB
/
import_products.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
#!/usr/bin/python
import sys
import os
import csv
from shutil import copy
from PIL import Image
from argparse import ArgumentParser
from hipcooks import models, db
import re
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), "uploads")
parser = ArgumentParser(description='Import ingredients')
parser.add_argument('csv', help="CSV file with ingredient data")
parser.add_argument('photo_dir', default=".",
help="directory containing the photos to import")
args = parser.parse_args()
fptr = open(args.csv, "rb")
reader = csv.reader(fptr)
db.session.begin(subtransactions=True)
for line in reader:
prod = models.Product.query.filter(models.Product.id == line[0]).first()
if prod is None:
sys.stderr.write("Can't locate product {}\n".format(line[0]))
continue
prod.name=line[2]
prod.type=line[3]
prod.splash_type = (line[4] == "YES")
prod.description = line[5]
prod.row = line[7]
prod.column = line[8]
if prod.photo is None:
prod.photo = models.ClassPhoto()
db.session.add(prod)
main_photo = os.path.join(args.photo_dir, "large", line[11])
base_name = prod.base_name
copy(main_photo, os.path.join(UPLOAD_FOLDER, "product_images", base_name))
if line[6] != "":
thumbnail_photo = os.path.join(args.photo_dir, "thumbs", line[6])
copy(thumbnail_photo, os.path.join(UPLOAD_FOLDER, "thumbnails", base_name))
else:
im = Image.open(main_photo)
im.thumbnail((130,105))
output_file = os.path.join(UPLOAD_FOLDER, 'thumbnails', base_name)
im.save(output_file, "JPEG")
prod.photo.photo = base_name
db.session.add(prod)
db.session.commit()
fptr.close()