-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathseed_database.py
More file actions
90 lines (74 loc) · 3.5 KB
/
Copy pathseed_database.py
File metadata and controls
90 lines (74 loc) · 3.5 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import csv
import os
import random
from flask import Flask
from models import db, Category, Game, Publisher
from utils.database import get_connection_string
def create_app():
"""Create and configure Flask app for database operations"""
app = Flask(__name__)
# Configure and initialize the database
app.config['SQLALCHEMY_DATABASE_URI'] = get_connection_string()
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
# Create tables
with app.app_context():
db.create_all()
return app
def create_games():
"""Create games, categories and publishers from CSV data for crowd funding platform"""
app = create_app()
with app.app_context():
# Track which categories and publishers have been created
categories = {} # name -> category object
publishers = {} # name -> publisher object
# Read the CSV file
csv_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'seed_data', 'games.csv')
game_count = 0
with open(csv_path, mode='r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
game_count += 1
# Process category
category_name = row['Category']
if category_name not in categories:
# Create new category if it doesn't exist
category_description = f"Collection of {category_name} games available for crowdfunding"
category = Category(
name=category_name,
description=category_description
)
db.session.add(category)
db.session.flush() # Get ID without committing
categories[category_name] = category
# Process publisher
publisher_name = row['Publisher']
if publisher_name not in publishers:
# Create new publisher if it doesn't exist
publisher_description = f"{publisher_name} is a game publisher seeking funding for exciting new titles"
publisher = Publisher(
name=publisher_name,
description=publisher_description
)
db.session.add(publisher)
db.session.flush() # Get ID without committing
publishers[publisher_name] = publisher
# Generate random star rating between 3.0 and 5.0 (one decimal place)
star_rating = round(random.uniform(3.0, 5.0), 1)
# Create the game with enhanced description for crowdfunding context
game = Game(
title=row['Title'],
description=row['Description'] + " Support this game through our crowdfunding platform!",
category_id=categories[category_name].id,
publisher_id=publishers[publisher_name].id,
star_rating=star_rating,
)
db.session.add(game)
# Commit all changes at once
db.session.commit()
print(f"Added {game_count} games with {len(categories)} categories and {len(publishers)} publishers")
def seed_database():
create_games()
if __name__ == '__main__':
seed_database()