/
create_db_items.py
60 lines (49 loc) · 2.06 KB
/
create_db_items.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
import csv
from flask import Flask
from models import db, Category, Item, Manufacturer
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///products.db'
db.init_app(app)
def create_db():
# read in csv file using csv module
with open('info.csv', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
rows = list(reader)
# lowercase column names and replace spaces with underscores
for row in rows:
row['name'] = row.pop('Name')
row['description'] = row.pop('Description')
row['price'] = float(row.pop('Price').replace(',', '.'))
row['quantity'] = int(row.pop('Quantity'))
row['category'] = row.pop('Category')
row['manufacturer'] = row.pop('Manufacturer')
# create database
with app.app_context():
db.create_all()
# add categories to database
categories = set(row['category'] for row in rows)
for name in categories:
category = Category(name=name)
db.session.add(category)
# add manufacturers to database
manufacturers = set(row['manufacturer'] for row in rows)
for name in manufacturers:
manufacturer = Manufacturer(name=name)
db.session.add(manufacturer)
# add items to database
for row in rows:
item = Item(name=row['name'], description=row['description'], price=row['price'], quantity=row['quantity'])
# set category relationship
category_name = row['category']
category = Category.query.filter_by(name=category_name).first()
item.category = category
# set manufacturer relationship
manufacturer_name = row['manufacturer']
manufacturer = Manufacturer.query.filter_by(name=manufacturer_name).first()
item.manufacturers.append(manufacturer)
db.session.add(item)
db.session.commit()
return "Database created successfully!"
if __name__ == "__main__":
with app.app_context():
create_db()