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
|
||||
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_migrate import Migrate
|
||||
import requests
|
||||
|
@ -160,10 +160,24 @@ def random_quote():
|
|||
return mattermost_response(response)
|
||||
return mattermost_response('No quotes found matching "{}"'.format(text_contains), ephemeral=True)
|
||||
|
||||
|
||||
@app.route('/robots.txt', methods=['GET'])
|
||||
def get_robots():
|
||||
return send_file('templates/robots.txt')
|
||||
|
||||
@app.route('/', methods=['GET'])
|
||||
|
||||
@app.route('/quotes.html', methods=['GET'])
|
||||
def list_quotes():
|
||||
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.admin = admin
|
||||
|
||||
|
||||
class Quote(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
quoter = db.Column(db.String(255), unique=False, nullable=False)
|
||||
quotee = db.Column(db.String(255), unique=False, nullable=True)
|
||||
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(
|
||||
db.DateTime, nullable=False,
|
||||
default=datetime.utcnow
|
||||
)
|
||||
db.DateTime, nullable=False,
|
||||
default=datetime.utcnow
|
||||
)
|
||||
|
||||
QUOTEE_REGEX = re.compile('\W*(\w+).*')
|
||||
|
||||
|
@ -45,4 +46,3 @@ class Quote(db.Model):
|
|||
# Experimentally try to find quoted user
|
||||
quotee_match = Quote.QUOTEE_REGEX.search(quote)
|
||||
self.quotee = quotee_match.group(1) if quotee_match is not None else None
|
||||
|
||||
|
|
Loading…
Reference in a new issue