2019-08-28 03:46:04 +02:00
|
|
|
import json
|
2019-10-01 20:57:23 +02:00
|
|
|
import typing
|
2019-08-28 03:46:04 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from flask import current_app as app
|
|
|
|
from flask import url_for
|
2019-10-01 20:57:23 +02:00
|
|
|
from models.order import Order
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
2019-10-01 20:57:23 +02:00
|
|
|
def webhook_text(order_item: Order) -> typing.Optional[str]:
|
|
|
|
if "Testlocation" in order_item.location.name:
|
|
|
|
return None
|
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
if order_item.courrier is not None:
|
2019-10-01 20:57:23 +02:00
|
|
|
return "<!channel|@channel> {3} is going to {1}, order <{0}|here>! Deadline in {2} minutes!".format(
|
2019-09-05 03:33:29 +02:00
|
|
|
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(),
|
|
|
|
)
|
2019-10-01 20:57:23 +02:00
|
|
|
|
|
|
|
return "<!channel|@channel> 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()
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WebhookSenderThread(Thread):
|
2019-09-11 22:38:01 +02:00
|
|
|
def __init__(self, message: str, url: str) -> None:
|
2019-08-28 03:46:04 +02:00
|
|
|
super(WebhookSenderThread, self).__init__()
|
|
|
|
self.message = message
|
2019-09-11 22:38:01 +02:00
|
|
|
self.url = url
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def run(self) -> None:
|
2019-08-28 03:46:04 +02:00
|
|
|
self.slack_webhook()
|
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def slack_webhook(self) -> None:
|
2019-09-11 22:38:01 +02:00
|
|
|
if self.url:
|
2019-09-16 23:14:52 +02:00
|
|
|
requests.post(self.url, json={"text": self.message})
|
2019-08-28 03:46:04 +02:00
|
|
|
else:
|
2019-09-11 22:38:01 +02:00
|
|
|
print(self.message)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def remaining_minutes(value) -> str:
|
2019-08-28 03:46:04 +02:00
|
|
|
delta = value - datetime.now()
|
|
|
|
if delta.total_seconds() < 0:
|
|
|
|
return "0"
|
2019-09-11 22:38:01 +02:00
|
|
|
minutes = delta.total_seconds() // 60
|
2019-08-28 03:46:04 +02:00
|
|
|
return "%02d" % minutes
|