-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
168 lines (130 loc) · 5.23 KB
/
app.py
File metadata and controls
168 lines (130 loc) · 5.23 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, jsonify, request, abort, make_response, render_template
import json
from flask_sqlalchemy import SQLAlchemy
import random
with open('config.json') as c:
params = json.load(c)['params']
app = Flask(__name__)
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username=params['username'],
password=params['password'],
hostname=params['hostname'],
databasename=params['databasename'],
)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
# app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:@localhost/quotebuddy" -> uncomment this when running local
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
############################################
# ROOT
############################################
@app.route('/')
def query_developer_details():
return jsonify({
"author": "TheBotbox",
"author_url": "http://thebotbox.online/",
"base_url": "thebotbox.pythonanywhere.com",
"project_name": "QuoteBuddy",
"project_url": "https://github.com/TheBotBox/QuoteBuddy"
})
class Quotes(db.Model):
id = db.Column(db.Integer, nullable=False, primary_key=True)
quote = db.Column(db.String(120), nullable=False)
author = db.Column(db.String(30), nullable=False)
############################################
# GET RANDOM QUOTE
############################################
@app.route('/get-random-quote', methods=['GET'])
def get_random():
try:
all_quotes = Quotes.query.filter_by().all()
length_of_quotes = len(all_quotes)
quote_id = random.randint(1, length_of_quotes)
quote = Quotes.query.filter_by(id=quote_id).first()
return make_response(jsonify({
'quote': quote.quote,
'author': quote.author,
'quote_id': quote.id
}), 200)
except:
return make_response(jsonify({
'error': 'Quote not found'
}), 404)
############################################
# GET QUOTE BY ID
############################################
@app.route('/get-quote/<string:quote_id>', methods=['GET'])
def get_quotes_by_id(quote_id):
try:
quote = Quotes.query.filter_by(id=quote_id).first()
if int(quote.id) != int(quote_id):
abort(404)
except:
abort(404)
return make_response(jsonify({'quote': quote.quote,
'author': quote.author,
'quote_id': quote.id}), 200)
############################################
# CREATE QUOTE
############################################
@app.route('/create-quote', methods=['POST'])
def create_quote():
if not request.json:
return make_response(jsonify({'error': 'Invalid request. JSON expected'}), 400)
if 'quote' not in request.json:
return make_response(jsonify({'error': "'quote' missing"}), 400)
if 'author' not in request.json:
return make_response(jsonify({'error': "'author' missing"}), 400)
request_quote = request.json['quote']
request_author = request.json['author']
entry = Quotes(quote=request_quote, author=request_author)
db.session.add(entry)
db.session.commit()
return make_response(jsonify({'message': 'Quote updated successfully',
'quote_id': entry.id}), 200)
############################################
# CLEAR TABLE | NEEDS CREDENTIALS
############################################
@app.route('/clear-table', methods=['POST'])
def clear_table():
if not request.json:
return make_response(jsonify({'error': 'Invalid request. JSON expected'}), 400)
if 'username' not in request.json:
return make_response(jsonify({'error': "'username' missing"}), 400)
if 'password' not in request.json:
return make_response(jsonify({'error': "'password' missing"}), 400)
user_name = request.json['username']
password = request.json['password']
if user_name == params['clear_table_user_name'] and password == params['clear_table_password']:
db.session.query(Quotes).delete()
db.session.commit()
return make_response({
'message': 'table cleared'
}, 200)
else:
return make_response(jsonify({
'error': 'Unauthorized to delete table'
}), 401)
############################################
# 404 Handling
############################################
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Quote not found'}), 404)
############################################
# Post a quote -> GUI Panel
############################################
@app.route('/post-quote', methods=['GET', 'POST'])
def post_quote():
if request.method == 'POST':
request_quote = request.form.get('post_a_quote')
request_author = request.form.get('author')
entry = Quotes(quote=request_quote, author=request_author)
db.session.add(entry)
db.session.commit()
return make_response(jsonify({'message': 'Quote updated successfully',
'quote_id': entry.id}), 200)
return render_template('index.html')
if __name__ == "__main__":
app.run()