Merge pull request #48 from ZeusWPI/feat/38/post_to_slack
Post to slack
This commit is contained in:
commit
586e2ca443
1 changed files with 47 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
||||||
|
|
||||||
__author__ = 'feliciaan'
|
__author__ = 'feliciaan'
|
||||||
|
import json
|
||||||
|
from threading import Thread
|
||||||
|
import requests
|
||||||
from flask import url_for, render_template, abort, redirect, Blueprint, flash, session, request
|
from flask import url_for, render_template, abort, redirect, Blueprint, flash, session, request
|
||||||
from flask.ext.login import current_user, login_required
|
from flask.ext.login import current_user, login_required
|
||||||
import random
|
import random
|
||||||
|
@ -29,6 +31,7 @@ def order_create():
|
||||||
orderForm.populate_obj(order)
|
orderForm.populate_obj(order)
|
||||||
db.session.add(order)
|
db.session.add(order)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
post_order_to_webhook(order)
|
||||||
return redirect(url_for('.order', id=order.id))
|
return redirect(url_for('.order', id=order.id))
|
||||||
return orders(form=orderForm)
|
return orders(form=orderForm)
|
||||||
|
|
||||||
|
@ -206,3 +209,46 @@ def get_orders(expression=None):
|
||||||
else:
|
else:
|
||||||
orders = Order.query.filter((expression & (Order.public == True))).all()
|
orders = Order.query.filter((expression & (Order.public == True))).all()
|
||||||
return orders
|
return orders
|
||||||
|
|
||||||
|
|
||||||
|
def post_order_to_webhook(order_item):
|
||||||
|
message = ''
|
||||||
|
if order_item.courrier is not None:
|
||||||
|
message = '{3} is going to {1}, order <{0}|here>! Deadline in {2} minutes!'.format(
|
||||||
|
url_for('.order', id=order_item.id, _external=True),
|
||||||
|
order_item.location.name,
|
||||||
|
remaining_minutes(order_item.stoptime),
|
||||||
|
order_item.courrier.username.title())
|
||||||
|
else:
|
||||||
|
message = '<{}|Open here.> New order for {}. Deadline in {} minutes.'.format(
|
||||||
|
url_for('.order', id=order_item.id, _external=True),
|
||||||
|
order_item.location.name,
|
||||||
|
remaining_minutes(order_item.stoptime))
|
||||||
|
webhookthread = WebhookSenderThread(message)
|
||||||
|
webhookthread.start()
|
||||||
|
|
||||||
|
|
||||||
|
class WebhookSenderThread(Thread):
|
||||||
|
|
||||||
|
def __init__(self, message):
|
||||||
|
super(WebhookSenderThread, self).__init__()
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.slack_webhook()
|
||||||
|
|
||||||
|
def slack_webhook(self):
|
||||||
|
js = json.dumps({'text': self.message})
|
||||||
|
url = app.config['SLACK_WEBHOOK']
|
||||||
|
if len(url) > 0:
|
||||||
|
requests.post(url, data=js)
|
||||||
|
else:
|
||||||
|
app.logger.info(str(js))
|
||||||
|
|
||||||
|
|
||||||
|
def remaining_minutes(value):
|
||||||
|
delta = value - datetime.now()
|
||||||
|
if delta.total_seconds() < 0:
|
||||||
|
return "0"
|
||||||
|
minutes, _ = divmod(delta.total_seconds(), 60)
|
||||||
|
return "%02d" % minutes
|
Loading…
Reference in a new issue