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
|
||||
from flask import jsonify
|
||||
from flask_api import FlaskAPI, status, exceptions
|
||||
from flask import Flask, jsonify
|
||||
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'])
|
||||
def index():
|
||||
@@ -16,10 +42,11 @@ def login():
|
||||
username = 'Dan'
|
||||
password = 'password'
|
||||
|
||||
query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username)
|
||||
result = db.con.execute(query).fetchone()
|
||||
|
||||
return jsonify(result[0])
|
||||
try:
|
||||
user = Users.query.filter_by(username=username).first()
|
||||
except:
|
||||
jsonify({'message': 'Login Failed'})
|
||||
return jsonify(userid=user.userID)
|
||||
|
||||
|
||||
@app.route('/updateFeeds/<int:userID>/', methods={'GET'})
|
||||
@@ -97,44 +124,32 @@ def addFeed(userID,newURL):
|
||||
|
||||
@app.route('/listShows/<int:userID>/', methods=['GET'])
|
||||
def listShows(userID):
|
||||
podcasts=[]
|
||||
pc=""
|
||||
output = []
|
||||
y = 0
|
||||
# grabs podcast urls from logged in user
|
||||
query = db.sql.text("""select podcastID, title from Podcasts where userID = %i""" % userID)
|
||||
results = db.con.execute(query).fetchall()
|
||||
for each in results:
|
||||
# podcasts.append(results[y][0])
|
||||
podcasts.append(results[y][1])
|
||||
mreh = podcasts.query.filter_by(userID=userID).all()
|
||||
|
||||
y=y+1
|
||||
# x=0
|
||||
# for each2 in podcasts:
|
||||
# pc=jsonify(
|
||||
# showID=podcasts[0][0],
|
||||
# showname=podcasts[0][1]
|
||||
# )
|
||||
# x=x+1
|
||||
# return jsonify(pc)
|
||||
return jsonify(podcasts)
|
||||
for each in mreh:
|
||||
result = {}
|
||||
result['podcastID'] = each.podcastID
|
||||
result['Title'] = each.title
|
||||
output.append(result)
|
||||
|
||||
@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):
|
||||
# checks to see if the username already exists
|
||||
query = db.sql.text("""select username from Users where username = \"%s\"""" % username)
|
||||
exists = db.con.execute(query).fetchone()
|
||||
# if the username doesn't already exist it will be added
|
||||
if exists is None or exists[0] != username:
|
||||
query = db.sql.text(
|
||||
"""insert into Users (username, password, name, email) values(\"%s\", \"%s\", \"%s\", \"%s\") """ % (
|
||||
username, password, name, email))
|
||||
db.con.execute(query)
|
||||
if not users.query.filter_by(username=username).first():
|
||||
new_user = users(username=username, password=password, name=name, email=email)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
# Returns the new userID
|
||||
query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username)
|
||||
result = db.con.execute(query).fetchone()
|
||||
return jsonify(result[0])
|
||||
newID = users.query.filter_by(username=username).first()
|
||||
return jsonify(userid=newID.userID)
|
||||
# if the username doesn't already exist it will be added
|
||||
else:
|
||||
return "Username already exists"
|
||||
return jsonify(message='Username already exists')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user