forked from SaraBee/MCGJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# MCGJ | ||
|
||
This is | ||
|
||
Data Model Notes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from flask import Flask | ||
app = Flask(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import sqlite3 | ||
import json | ||
import click | ||
from flask import Flask, current_app, g | ||
from flask.cli import with_appcontext | ||
|
||
DATABASE = "mcg.db" | ||
app = Flask(__name__) | ||
|
||
def get_db(): | ||
db = getattr(g, "_database", None) | ||
if db is None: | ||
db = g._database = sqlite3.connect(DATABASE) | ||
def dict_factory(cursor, row): | ||
d = {} | ||
for idx, col in enumerate(cursor.description): | ||
d[col[0]] = row[idx] | ||
return d | ||
db.row_factory = dict_factory | ||
return db | ||
|
||
|
||
@app.teardown_appcontext | ||
def close_connection(exception): | ||
db = getattr(g, "_database", None) | ||
if db is not None: | ||
db.close() | ||
|
||
|
||
def init_db(): | ||
with app.app_context(): | ||
db = get_db() | ||
with app.open_resource("init-db.sql") as f: | ||
db.cursor().executescript(f.read()) | ||
db.commit() | ||
|
||
@click.command("init-db") | ||
@with_appcontext | ||
def init_db_command(): | ||
"""DESTROY existing data and create a new table.""" | ||
init_db() | ||
click.echo("Initialized the database.") | ||
|
||
|
||
def print_records(table: str) -> str: | ||
db = get_db() | ||
cursor = db.cursor() | ||
|
||
command = """ | ||
SELECT * FROM "{}"; | ||
""".format(table) | ||
|
||
results = [] | ||
for row in cursor.execute(command): | ||
results.append(row) | ||
|
||
return(json.dumps(results)) | ||
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
db = sqlite3.connect(DATABASE) | ||
|
||
def dict_factory(cursor, row): | ||
d = {} | ||
for idx, col in enumerate(cursor.description): | ||
d[col[0]] = row[idx] | ||
return d | ||
db.row_factory = dict_factory | ||
|
||
cursor = db.cursor() | ||
|
||
command = """ | ||
SELECT * FROM tracks | ||
""" | ||
|
||
tracks = [] | ||
for track in cursor.execute(command): | ||
tracks.append(track) | ||
|
||
print(tracks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- Demo row table | ||
INSERT INTO sessions (id, create_date, update_date, name, date, spotify_url, current_round) | ||
VALUES (1, 1590518158 , 1590518159, "MCG 2020-05-26", 1590518158, "https: | ||
//open.spotify.com/playlist/5BTObgLJeCfLaP8unbefIs?si=q2zzZR4RTbOpkJchCMq2Cw", 1); | ||
|
||
INSERT INTO tracks (id, create_date, update_date, person, track_name, track_url, session_id, done, round_number, round_position) | ||
VALUES (1, 1590518159, 1590518160, "Sara Bobo", "MissDat†Booty†", "https://open.spotify.com/track/4UJIkpP55qZuq1ecP5luqQ?si=E44FBYM0SXmwAuCM0dZ_wg", 1, 1, 1, 0); | ||
|
||
INSERT INTO tracks (id, create_date, update_date, person, session_id, done, round_number) | ||
VALUES (2, 1590518159, 1590518160, "Toph Allen", 1, 0, 1); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from flask import Flask | ||
from .db import print_records | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def print_tracks(): | ||
return print_records('tracks') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CREATE TABLE sessions ( | ||
id INTEGER PRIMARY KEY, | ||
create_date INTEGER NOT NULL, | ||
update_date INTEGER, | ||
name TEXT NOT NULL, | ||
date INTEGER, | ||
spotify_url TEXT, | ||
current_round INTEGER | ||
); | ||
|
||
CREATE TABLE tracks ( | ||
id INTEGER PRIMARY KEY, | ||
create_date INTEGER NOT NULL, | ||
update_date INTEGER, | ||
person TEXT, | ||
track_name TEXT, | ||
track_url TEXT, | ||
session_id INTEGER, | ||
done INTEGER, | ||
round_number INTEGER, | ||
round_position INTEGER | ||
); |