Moved from flaskAPI to regular flask. Started reworking SQL queries from sql.text to straight sqlalchemy ORM. Login, listShows, register methods have been reworked to comply. Also have listShows returning nicely formatted json now.
This commit is contained in:
91
main.py
91
main.py
@@ -1,11 +1,37 @@
|
|||||||
import database as db
|
|
||||||
import feedparser as fp
|
import feedparser as fp
|
||||||
from flask import jsonify
|
from flask import Flask, jsonify
|
||||||
from flask_api import FlaskAPI, status, exceptions
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://pc:pc@67.149.14.121:3306/Podcast'
|
||||||
|
|
||||||
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
|
|
||||||
app = FlaskAPI(__name__)
|
class users(db.Model):
|
||||||
|
userID = db.Column(db.Integer, primary_key=True)
|
||||||
|
username = db.Column(db.Text, unique=True)
|
||||||
|
password = db.Column(db.Text)
|
||||||
|
name = db.Column(db.Text)
|
||||||
|
name = db.Column(db.Text)
|
||||||
|
email = db.Column(db.Text)
|
||||||
|
|
||||||
|
class shows(db.Model):
|
||||||
|
showID = db.Column(db.Integer, primary_key=True)
|
||||||
|
podcastID = db.Column(db.Integer)
|
||||||
|
userID = db.Column(db.Integer)
|
||||||
|
link = db.Column(db.Text)
|
||||||
|
started = db.Column(db.Boolean)
|
||||||
|
position = db.Column(db.Text)
|
||||||
|
|
||||||
|
|
||||||
|
class podcasts(db.Model):
|
||||||
|
podcastID = db.Column(db.Integer, primary_key=True)
|
||||||
|
userID = db.Column(db.Integer)
|
||||||
|
title = db.Column(db.Text)
|
||||||
|
URL = db.Column(db.Text)
|
||||||
|
artworkURL = db.Column(db.Text)
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
@@ -16,10 +42,11 @@ def login():
|
|||||||
username = 'Dan'
|
username = 'Dan'
|
||||||
password = 'password'
|
password = 'password'
|
||||||
|
|
||||||
query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username)
|
try:
|
||||||
result = db.con.execute(query).fetchone()
|
user = Users.query.filter_by(username=username).first()
|
||||||
|
except:
|
||||||
return jsonify(result[0])
|
jsonify({'message': 'Login Failed'})
|
||||||
|
return jsonify(userid=user.userID)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/updateFeeds/<int:userID>/', methods={'GET'})
|
@app.route('/updateFeeds/<int:userID>/', methods={'GET'})
|
||||||
@@ -97,44 +124,32 @@ def addFeed(userID,newURL):
|
|||||||
|
|
||||||
@app.route('/listShows/<int:userID>/', methods=['GET'])
|
@app.route('/listShows/<int:userID>/', methods=['GET'])
|
||||||
def listShows(userID):
|
def listShows(userID):
|
||||||
podcasts=[]
|
output = []
|
||||||
pc=""
|
|
||||||
y = 0
|
y = 0
|
||||||
# grabs podcast urls from logged in user
|
# grabs podcast urls from logged in user
|
||||||
query = db.sql.text("""select podcastID, title from Podcasts where userID = %i""" % userID)
|
mreh = podcasts.query.filter_by(userID=userID).all()
|
||||||
results = db.con.execute(query).fetchall()
|
|
||||||
for each in results:
|
|
||||||
# podcasts.append(results[y][0])
|
|
||||||
podcasts.append(results[y][1])
|
|
||||||
|
|
||||||
y=y+1
|
for each in mreh:
|
||||||
# x=0
|
result = {}
|
||||||
# for each2 in podcasts:
|
result['podcastID'] = each.podcastID
|
||||||
# pc=jsonify(
|
result['Title'] = each.title
|
||||||
# showID=podcasts[0][0],
|
output.append(result)
|
||||||
# showname=podcasts[0][1]
|
|
||||||
# )
|
|
||||||
# x=x+1
|
|
||||||
# return jsonify(pc)
|
|
||||||
return jsonify(podcasts)
|
|
||||||
|
|
||||||
@app.route('/register/<string:username>,<string:password>,<string:name>,<string:email>', methods=['GET'])
|
return jsonify(shows=output)
|
||||||
|
|
||||||
|
@app.route('/register/<string:username>&<string:password>&<string:name>&<string:email>', methods=['POST'])
|
||||||
def register(username, password, name, email):
|
def register(username, password, name, email):
|
||||||
# checks to see if the username already exists
|
# checks to see if the username already exists
|
||||||
query = db.sql.text("""select username from Users where username = \"%s\"""" % username)
|
if not users.query.filter_by(username=username).first():
|
||||||
exists = db.con.execute(query).fetchone()
|
new_user = users(username=username, password=password, name=name, email=email)
|
||||||
# if the username doesn't already exist it will be added
|
db.session.add(new_user)
|
||||||
if exists is None or exists[0] != username:
|
db.session.commit()
|
||||||
query = db.sql.text(
|
|
||||||
"""insert into Users (username, password, name, email) values(\"%s\", \"%s\", \"%s\", \"%s\") """ % (
|
|
||||||
username, password, name, email))
|
|
||||||
db.con.execute(query)
|
|
||||||
# Returns the new userID
|
# Returns the new userID
|
||||||
query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username)
|
newID = users.query.filter_by(username=username).first()
|
||||||
result = db.con.execute(query).fetchone()
|
return jsonify(userid=newID.userID)
|
||||||
return jsonify(result[0])
|
# if the username doesn't already exist it will be added
|
||||||
else:
|
else:
|
||||||
return "Username already exists"
|
return jsonify(message='Username already exists')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user