Attempt to add quotes to mattermore

This commit is contained in:
Rien Maertens 2018-10-03 01:18:00 +02:00
parent 6194ea59e4
commit 53d05ef5c5
No known key found for this signature in database
GPG key ID: 943CAB70C511D23C
4 changed files with 77 additions and 3 deletions

View file

@ -1,9 +1,11 @@
import json import json
from functools import wraps from functools import wraps
from flask import Flask, request, Response, abort from flask import Flask, request, Response, abort, render_template
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
import requests import requests
import config import config
import re
import pdb
app = Flask(__name__) app = Flask(__name__)
@ -57,7 +59,6 @@ def requires_token(token_name):
def decorated_function(*args, **kwargs): def decorated_function(*args, **kwargs):
expected_token = config.tokens[token_name] expected_token = config.tokens[token_name]
token = request.values.get('token') token = request.values.get('token')
print(token, expected_token)
if expected_token != token: if expected_token != token:
return abort(401) return abort(401)
return f(*args, **kwargs) return f(*args, **kwargs)
@ -123,3 +124,22 @@ def door(username):
tokens = request.values.get('text').strip().split() tokens = request.values.get('text').strip().split()
command = tokens[0].lower() command = tokens[0].lower()
return mattermost_response(slotmachien_request(username, command)) return mattermost_response(slotmachien_request(username, command))
QUOTEE_REGEX = re.compile('\W*(\w+).*')
@app.route('/addquote', methods=['POST'])
@requires_token('quote')
def add_quote():
user = request.values['user_name']
channel = request.values['channel_name']
quote_text = request.values['text']
quote = models.Quote(user, quote_text, channel)
db.session.add(quote)
db.session.commit()
return mattermost_response("Your quote has been added.")
@app.route('/', methods=['GET'])
def list_quotes():
return render_template('quotes.html', quotes = models.Quote.query.all())

View file

@ -1,4 +1,6 @@
from .app import db from .app import db
from datetime import datetime
import re
class User(db.Model): class User(db.Model):
@ -14,3 +16,30 @@ class User(db.Model):
super() super()
self.username = username self.username = username
self.admin = admin 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(1023), unique=True, nullable=False)
created_at = db.Column(
db.DateTime, nullable=False,
default=datetime.utcnow
)
QUOTEE_REGEX = re.compile('\W*(\w+).*')
def __repr__(self):
return f"<Quote {self.quoter} \"{self.quote}\">"
def __init__(self, quoter, quote, channel):
super()
self.quoter = quoter
self.quote = quote
self.channel = channel
self.created_at = datetime.utcnow()
# 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

24
app/templates/quotes.html Normal file
View file

@ -0,0 +1,24 @@
<html>
<head>
<title>Zeus WPI Quotes</title>
</head>
<body>
<h1>Zeus WPI Quotes</h1>
<table>
<tr>
<th>Quoter</th>
<th>Quotee</th>
<th>When</th>
<th>Quote</th>
</tr>
{% for quote in quotes %}
<tr>
<td>{{ quote.quoter }}</td>
<td>{{ quote.quotee }}</td>
<td>{{ quote.created_at }}</td>
<td>{{ quote.quote }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>

View file

@ -2,7 +2,8 @@ DATABASE_URL = 'sqlite:////tmp/mattermost.db'
tokens = { tokens = {
'authorize': '123', 'authorize': '123',
'door': '123', 'door': '123',
'revoke': '123' 'revoke': '123',
'quote': '123'
} }
slotmachien_url = 'https://kelder.zeus.ugent.be/slotmachien/slack/' slotmachien_url = 'https://kelder.zeus.ugent.be/slotmachien/slack/'
slotmachien_token = '123' slotmachien_token = '123'