Add json API
This commit is contained in:
parent
7cddad10a9
commit
dba7d587b1
2 changed files with 21 additions and 7 deletions
18
app/app.py
18
app/app.py
|
@ -1,6 +1,6 @@
|
||||||
import json
|
import json
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from flask import Flask, request, Response, abort, render_template, send_file
|
from flask import Flask, request, Response, abort, render_template, send_file, jsonify
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
import requests
|
import requests
|
||||||
|
@ -160,10 +160,24 @@ def random_quote():
|
||||||
return mattermost_response(response)
|
return mattermost_response(response)
|
||||||
return mattermost_response('No quotes found matching "{}"'.format(text_contains), ephemeral=True)
|
return mattermost_response('No quotes found matching "{}"'.format(text_contains), ephemeral=True)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/robots.txt', methods=['GET'])
|
@app.route('/robots.txt', methods=['GET'])
|
||||||
def get_robots():
|
def get_robots():
|
||||||
return send_file('templates/robots.txt')
|
return send_file('templates/robots.txt')
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
|
||||||
|
@app.route('/quotes.html', methods=['GET'])
|
||||||
def list_quotes():
|
def list_quotes():
|
||||||
return render_template('quotes.html', quotes=reversed(models.Quote.query.all()))
|
return render_template('quotes.html', quotes=reversed(models.Quote.query.all()))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/quotes.json', methods=['GET'])
|
||||||
|
def json_quotes():
|
||||||
|
all_quotes = models.Quote.query.all()
|
||||||
|
return jsonify(list({
|
||||||
|
'quoter': q.quoter,
|
||||||
|
'quotee': q.quotee,
|
||||||
|
'channel': q.channel,
|
||||||
|
'quote': q.quote,
|
||||||
|
'created_at': q.created_at.isoformat()
|
||||||
|
} for q in all_quotes))
|
||||||
|
|
|
@ -17,16 +17,17 @@ class User(db.Model):
|
||||||
self.username = username
|
self.username = username
|
||||||
self.admin = admin
|
self.admin = admin
|
||||||
|
|
||||||
|
|
||||||
class Quote(db.Model):
|
class Quote(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
quoter = db.Column(db.String(255), unique=False, nullable=False)
|
quoter = db.Column(db.String(255), unique=False, nullable=False)
|
||||||
quotee = db.Column(db.String(255), unique=False, nullable=True)
|
quotee = db.Column(db.String(255), unique=False, nullable=True)
|
||||||
channel = db.Column(db.String(255), unique=False, nullable=False)
|
channel = db.Column(db.String(255), unique=False, nullable=False)
|
||||||
quote = db.Column(db.String(16383), unique=False, nullable=False)
|
quote = db.Column(db.String(16383), unique=False, nullable=False)
|
||||||
created_at = db.Column(
|
created_at = db.Column(
|
||||||
db.DateTime, nullable=False,
|
db.DateTime, nullable=False,
|
||||||
default=datetime.utcnow
|
default=datetime.utcnow
|
||||||
)
|
)
|
||||||
|
|
||||||
QUOTEE_REGEX = re.compile('\W*(\w+).*')
|
QUOTEE_REGEX = re.compile('\W*(\w+).*')
|
||||||
|
|
||||||
|
@ -45,4 +46,3 @@ class Quote(db.Model):
|
||||||
# Experimentally try to find quoted user
|
# Experimentally try to find quoted user
|
||||||
quotee_match = Quote.QUOTEE_REGEX.search(quote)
|
quotee_match = Quote.QUOTEE_REGEX.search(quote)
|
||||||
self.quotee = quotee_match.group(1) if quotee_match is not None else None
|
self.quotee = quotee_match.group(1) if quotee_match is not None else None
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue