From 66fd50743f894eab833995b2a1342ab3ce330750 Mon Sep 17 00:00:00 2001 From: redfast00 Date: Thu, 14 Feb 2019 19:09:08 +0100 Subject: [PATCH] Fix quotes --- app/app.py | 17 +++++++++++------ setup_database.py | 6 ++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/app.py b/app/app.py index 31311d5..87872d9 100644 --- a/app/app.py +++ b/app/app.py @@ -126,17 +126,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') @@ -149,13 +149,18 @@ 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 = f'{selected_quote.quote}' + return mattermost_response(response) + return mattermost_response(f'No quotes found matching "{text_contains}"', ephemeral=True) @app.route('/', 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())) diff --git a/setup_database.py b/setup_database.py index 25c16f8..d736e97 100644 --- a/setup_database.py +++ b/setup_database.py @@ -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()