-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_ingredients.py
executable file
·48 lines (43 loc) · 1.56 KB
/
import_ingredients.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
#! /usr/bin/env python
from argparse import ArgumentParser
from collections import namedtuple
import csv
from hipcooks import app, db, models
from logging import error
from os.path import basename, join
from shutil import copy
from sqlalchemy import func
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
resource_row = namedtuple("resource_row", ["name", "description", "filename"])
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()
with open(args.csv, "rb") as data:
resources = [resource_row(*(unicode(cell, "utf-8")
for cell in row if cell != ""))
for row in csv.reader(data) if any(row)]
db.session.execute(models.ResourcesIngredients.__table__.delete())
for order, resource in enumerate(resources):
src = join(args.photo_dir, resource.filename)
dest = join(
app.config["UPLOAD_FOLDER"],
models.ResourcesIngredients.PICTURE_DIR,
resource.filename
)
try:
copy(src, dest)
except IOError:
error('Couldn\'t import resource "%s" from "%s" to "%s"',
resource.name, src, dest)
raise
continue
db.session.begin()
db.session.add(models.ResourcesIngredients(
picture=resource.filename,
name=resource.name,
description=resource.description,
order=order,
))
db.session.commit()