Commit dba7d587 authored by j's avatar j

Add json API

parent 7cddad10
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment