From f41817a5dd4379aa8109120658448d5a4611d134 Mon Sep 17 00:00:00 2001 From: midgard Date: Tue, 1 Oct 2019 20:57:23 +0200 Subject: [PATCH] Disable webhook for testlocation, add test to seed --- app/database/add_testlocation.py | 24 ++++++++++++++++++++++++ app/database/create_database.py | 2 ++ app/notification.py | 31 ++++++++++++++++++++----------- 3 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 app/database/add_testlocation.py diff --git a/app/database/add_testlocation.py b/app/database/add_testlocation.py new file mode 100644 index 0000000..0449c6c --- /dev/null +++ b/app/database/add_testlocation.py @@ -0,0 +1,24 @@ +from app import db +from models import Location, Product + +STUFFS = [ + ("Broodje zever", 540), + ("Broodje aap", 0), + ("Broodje goud", 500000), +] + + +def add() -> None: + testlocation = Location() + testlocation.configure( + "Testlocation", + "Please ignore!", + "0469 69 69 69", + "http://localhost:8000/", + ) + db.session.add(testlocation) + + for stuff in STUFFS: + entry = Product() + entry.configure(testlocation, *stuff) + db.session.add(entry) diff --git a/app/database/create_database.py b/app/database/create_database.py index 3af6957..320dded 100644 --- a/app/database/create_database.py +++ b/app/database/create_database.py @@ -3,10 +3,12 @@ import add_fitchen import add_oceans_garden import add_primadonna import add_simpizza +import add_testlocation from app import db, create_app entry_sets = { "Admins": add_admins.add, + "Testlocation": add_testlocation.add, "Ocean's Garden": add_oceans_garden.add, "SimPizza": add_simpizza.add, "Primadonna": add_primadonna.add, diff --git a/app/notification.py b/app/notification.py index 13b62d1..2637e76 100644 --- a/app/notification.py +++ b/app/notification.py @@ -1,29 +1,38 @@ import json +import typing from datetime import datetime from threading import Thread import requests from flask import current_app as app from flask import url_for +from models.order import Order -def post_order_to_webhook(order_item) -> None: - message = "" +def webhook_text(order_item: Order) -> typing.Optional[str]: + if "Testlocation" in order_item.location.name: + return None + if order_item.courrier is not None: - message = " {3} is going to {1}, order <{0}|here>! Deadline in {2} minutes!".format( + return " {3} is going to {1}, order <{0}|here>! Deadline in {2} minutes!".format( url_for("order_bp.order", id=order_item.id, _external=True), order_item.location.name, remaining_minutes(order_item.stoptime), order_item.courrier.username.title(), ) - else: - message = " New order for {}. Deadline in {} minutes. <{}|Open here.>".format( - order_item.location.name, - remaining_minutes(order_item.stoptime), - url_for("order_bp.order", id=order_item.id, _external=True), - ) - webhookthread = WebhookSenderThread(message, app.config["SLACK_WEBHOOK"]) - webhookthread.start() + + return " New order for {}. Deadline in {} minutes. <{}|Open here.>".format( + order_item.location.name, + remaining_minutes(order_item.stoptime), + url_for("order_bp.order", id=order_item.id, _external=True), + ) + + +def post_order_to_webhook(order_item: Order) -> None: + message = webhook_text(order_item) + if message: + webhookthread = WebhookSenderThread(message, app.config["SLACK_WEBHOOK"]) + webhookthread.start() class WebhookSenderThread(Thread):