|
| 1 | +from guitary.data.guitar import Guitar |
| 2 | +from guitary.data import session_factory |
| 3 | + |
| 4 | + |
| 5 | +def load_guitars_if_empty(): |
| 6 | + |
| 7 | + with session_factory.create_session() as ctx: |
| 8 | + count = ctx.session.query(Guitar).count() |
| 9 | + if count > 0: |
| 10 | + print(f"Not adding new data, there are {count} guitars already.") |
| 11 | + return |
| 12 | + |
| 13 | + guitars = [ |
| 14 | + Guitar(name='AX Black', price=499, img='/static/img/guitars/ax-black.jpg', style='electric'), |
| 15 | + Guitar(name='Jet Black Electric', price=599, img='/static/img/guitars/jet-black-electric.jpg', |
| 16 | + style='electric'), |
| 17 | + Guitar(name='Weezer Classic', price=1499, img='/static/img/guitars/weezer-classic.jpg', style='electric'), |
| 18 | + Guitar(name='Acoustic Black', price=1298, img='/static/img/guitars/black-acoustic.jpg', style='acoustic'), |
| 19 | + Guitar(name='Mellow Yellow', price=799, img='/static/img/guitars/mellow-yellow.jpg', style='electric'), |
| 20 | + Guitar(name='White Vibes', price=699, img='/static/img/guitars/white-vibes.jpg', style='electric'), |
| 21 | + Guitar(name='Brush Riffs', price=599, img='/static/img/guitars/brushed-black-electric.jpg', |
| 22 | + style='electric'), |
| 23 | + Guitar(name="Nature's Song", price=799, img='/static/img/guitars/natures-song.jpg', style='electric'), |
| 24 | + Guitar(name='Electric Wood Grain', price=399, img='/static/img/guitars/woodgrain-electric.jpg', |
| 25 | + style='electric'), |
| 26 | + ] |
| 27 | + |
| 28 | + # Do work here... |
| 29 | + for guitar in guitars: |
| 30 | + ctx.session.add(guitar) |
| 31 | + |
| 32 | + ctx.session.commit() |
0 commit comments