Merge branch 'master' of github.com:ZeusWPI/mattermore

This commit is contained in:
Robbe Van Herck 2019-02-19 14:52:48 +01:00
commit a4453b8265
No known key found for this signature in database
GPG key ID: 4D1C8E8FBD1C7188
5 changed files with 97 additions and 15 deletions

View file

@ -25,3 +25,4 @@ virtualenv -p python3 venv
./venv/bin/python run_dev.py
```
*migrations*

View file

@ -1,8 +1,9 @@
import json
from functools import wraps
from flask import Flask, request, Response, abort, render_template
from flask import Flask, request, Response, abort, render_template, send_file, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from datetime import datetime
import requests
import config
import random
@ -146,17 +147,17 @@ def door(username):
command = tokens[0].lower()
return mattermost_response(slotmachien_request(username, command), ephemeral=True)
@app.route('/cammiechat', methods=['POST'])
@requires_token('cammiechat')
@requires_regular
def cammiechat(username):
headers = {
"X-Username": username
"X-Username": username
}
requests.post("https://kelder.zeus.ugent.be/messages/", data=request.values.get('text').strip(), headers=headers)
return mattermost_response("Message sent", ephemeral=True)
QUOTEE_REGEX = re.compile('\W*(\w+).*')
@app.route('/addquote', methods=['POST'])
@requires_token('quote')
@ -169,13 +170,89 @@ def add_quote():
db.session.commit()
return mattermost_response("{} added the quote \"{}\"".format(user, quote_text))
@app.route('/quote', methods=['GET'])
@app.route('/quote', methods=['POST'])
def random_quote():
text_contains = request.values['text']
matches = models.Quote.query.filter(models.Quote.quote.contains(text_contains))
return mattermost_response(random.choice(matches))
matches = models.Quote.query.filter(models.Quote.quote.contains(text_contains)).all()
if matches:
selected_quote = random.choice(matches)
response = selected_quote.quote
return mattermost_response(response)
return mattermost_response('No quotes found matching "{}"'.format(text_contains), ephemeral=True)
@app.route('/', methods=['GET'])
@app.route('/robots.txt', methods=['GET'])
def get_robots():
return send_file('templates/robots.txt')
@app.route('/quotes.html', methods=['GET'])
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))
RESTO_TEMPLATE = """
# Resto menu
## Soepjes
{soup_table}
## Vleesjes
{meat_table}
## Visjes
{fish_table}
## Niet-vleesjes
{vegi_table}
## Groentjes
{vegetable_table}
"""
@app.route('/resto', methods=['GET'])
def resto_menu():
today = datetime.today()
url = "https://zeus.ugent.be/hydra/api/2.0/resto/menu/nl/{}/{}/{}.json"\
.format(today.year, today.month, today.day)
resto = requests.get(url).json()
if not resto["open"]:
return 'De resto is vandaag gesloten.'
else:
def table_for(kind):
items = [meal for meal in resto["meals"] if meal["kind"] == kind]
maxwidth = max(map(lambda item: len(item["name"]), items))
return "\n".join("{name: <{width}}{price}".format(
name=item["name"],
width=maxwidth + 2,
price=item["price"])
for item in items
)
return RESTO_TEMPLATE.format(
soup_table=table_for("soup"),
meat_table=table_for("meat"),
fish_table=table_for("fish"),
vegi_table=table_for("vegetarian"),
vegetable_table="\n".join(resto["vegetables"])
)
@app.route('/resto.json', methods=['GET'])
def resto_menu_json():
return mattermost_response(resto_menu(), ephemeral=True)

View file

@ -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

2
app/templates/robots.txt Normal file
View file

@ -0,0 +1,2 @@
User-agent: *
Disallow: /

View file

@ -4,5 +4,7 @@ from app.app import db, models
db.create_all()
db.session.add(models.User('admin', admin=True))
db.session.commit()
exists = models.User.query.filter_by(username='admin').first()
if not exists:
db.session.add(models.User('admin', admin=True))
db.session.commit()