diff --git a/app/app.py b/app/app.py index 28a96ac..3962f05 100644 --- a/app/app.py +++ b/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)) diff --git a/app/models.py b/app/models.py index 6971e7a..8faa491 100644 --- a/app/models.py +++ b/app/models.py @@ -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 -