84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
|
from flask import render_template, abort, redirect, url_for, abort, flash, request, current_app, make_response, g
|
||
|
from flask_login import login_required, login_user, logout_user, current_user
|
||
|
from flask_sqlalchemy import get_debug_queries
|
||
|
|
||
|
from . import livesupport
|
||
|
from .forms import MessageForm
|
||
|
from .. import db
|
||
|
from ..email import send_email
|
||
|
from ..models import User, Permission, SupportTopic, SupportLine, contact_proxmaster
|
||
|
|
||
|
import base64
|
||
|
from datetime import date, time, datetime
|
||
|
from dateutil.relativedelta import relativedelta
|
||
|
|
||
|
@livesupport.after_app_request
|
||
|
def after_request(response):
|
||
|
for query in get_debug_queries():
|
||
|
if query.duration >= current_app.config['SLOW_DB_QUERY_TIME']:
|
||
|
current_app.logger.warning('Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n' % (query.statement, query.parameters, query.duration, query.context))
|
||
|
return response
|
||
|
|
||
|
#SUPPORT
|
||
|
@livesupport.route("/support", methods=['GET'])
|
||
|
@login_required
|
||
|
def support_list():
|
||
|
""" general enquiry and list all open support tasks """
|
||
|
cuser = current_user
|
||
|
form = MessageForm()
|
||
|
|
||
|
alltopics = cuser.inv_topics.all()
|
||
|
return render_template('livesupport/support_list.html', form=form, inv_topics=alltopics)
|
||
|
|
||
|
@livesupport.route("/support/<string:topic>/", methods=['GET', 'POST'])
|
||
|
@login_required
|
||
|
def support(topic):
|
||
|
""" block item for support chatbox. invoked from vdc_pool or supportlist """
|
||
|
cuser = current_user
|
||
|
form = MessageForm()
|
||
|
|
||
|
if request.method == "GET":
|
||
|
support_topic = SupportTopic.query.filter_by(hashtag=str(topic)).first()
|
||
|
if support_topic == None:
|
||
|
class EmptySupport():
|
||
|
hashtag=str(topic)
|
||
|
timestamp=datetime.utcnow()
|
||
|
support_topic = EmptySupport()
|
||
|
return render_template('livesupport/support_item.html', form=form, support=support_topic)
|
||
|
else:
|
||
|
if support_topic.user_id != cuser.pid:
|
||
|
abort(403) #TODO: hidden 403. there is a topic like that but its not yours!
|
||
|
else:
|
||
|
#topic is yours. show it.
|
||
|
return render_template('livesupport/support_item.html', form=form, support=support_topic)
|
||
|
|
||
|
if request.method == "POST" and form.validate_on_submit():
|
||
|
support_topic = SupportTopic.query.filter_by(hashtag=str(topic)).first()
|
||
|
if support_topic == None:
|
||
|
#no topic. create one?
|
||
|
if cuser.inv_topics.all() != []:
|
||
|
#check if other topics exist, and ratelimit
|
||
|
last_topic = cuser.inv_topics.order_by(SupportTopic.timestamp.desc()).first()
|
||
|
now = datetime.utcnow()
|
||
|
time_last_topic = last_topic.timestamp
|
||
|
expiry = time_last_topic + relativedelta(time_last_topic, minutes=+5)
|
||
|
if now < expiry:
|
||
|
flash('ratelimit. try again later')
|
||
|
return redirect(url_for('livesupport.support_list'))
|
||
|
#create new topic
|
||
|
new_topic = SupportTopic(user_id=cuser.pid, hashtag=str(topic))
|
||
|
db.session.add(new_topic)
|
||
|
new_line = SupportLine(topic_id=new_topic.pid, line=str(form.line.data))
|
||
|
db.session.add(new_line)
|
||
|
|
||
|
else:
|
||
|
if support_topic.user_id == cuser.pid:
|
||
|
new_line = SupportLine(topic_id=support_topic.pid, line=form.line.data)
|
||
|
db.session.add(new_line)
|
||
|
else:
|
||
|
abort(403) #TODO: hidden 404
|
||
|
|
||
|
db.session.commit()
|
||
|
return redirect(url_for('livesupport.support_list'))
|
||
|
|