zammad-form/backend/app.py
2025-01-21 12:47:35 +01:00

102 lines
No EOL
3.5 KiB
Python

import os
import logging
from flask import Flask, send_file, request, jsonify
from requests import HTTPError
from zammad_py import ZammadAPI
from zammad_py.api import Object as ZamObject
logger = logging.getLogger(__name__)
ZAMMAD_TOKEN = os.getenv("ZAMMAD_TOKEN")
ZAMMAD_URL = os.getenv("ZAMMAD_URL")
if not ZAMMAD_TOKEN or not ZAMMAD_URL:
logger.error('Missing conf env var(s)')
app = Flask(__name__,
static_folder=os.path.join("..","frontend","dist"))
zammad = ZammadAPI(url=ZAMMAD_URL, http_token=ZAMMAD_TOKEN)
@app.route("/")
def main():
index_path = os.path.join(app.static_folder, "index.html")
return send_file(index_path)
@app.route('/api/hello')
def start():
return "Hallo, Welt!"
@app.route('/api/buildings')
def api_buildings():
#return ["Theater Dortmund", "Schauspiel Dortmund", "Probebühnen Alte Straße", "Lager Alte Straße",
# "Probebühne KJT","Schreinerei","Schlosserei", "Lager Niedersachsenweg" ]
zob = ZamObject(zammad)
for z in zob.all():
if "gebaeude" == z['name']:
return z['data_option']['options']
return jsonify([])
@app.route("/api/groups")
def api_groups():
#return [{"text":'Hausmeister', "value":"5"}, {"text":"Haustechnik", "value":"4"}, {"text":"Leitung-HBT", "value":"2"}, {"text":"IT", "value":"4"}]
arr = []
for x in zammad.group.all():
if "Users" != x['name']:
arr.append(x['name'])
return jsonify(arr)
@app.route("/api/submit", methods=['POST'])
def api_submit():
data = request.json
print(data)
if data['firstname'] != "" and data['lastname'] != "" and data['email'] != "":
try:
zammad.user.create({"firstname":data['firstname'],"lastname":data['lastname'],"email":data['email'],"roles":["Customer"]})
except HTTPError as e:
print("User creation failed: {}".format(e))
if "Invalid email" in str(e):
return jsonify(error='Invalid Email'), 500
try:
zammad.on_behalf_of = data['email']
title = (data['description'][:25] + '..') if len(data['description']) > 25 else data['description']
zammad.ticket.create({"title":"Web-Form: {}".format(title),"customer":data['email'],
"group":data['group'],
"gebaeude":data['building'],
"raumnummer":data['room'],
"article": {
"subject": "",
"body": data['description'],
"type": "note",
"internal": "false"
}})
except HTTPError as e:
print("Ticket creation failed: {}".format(e))
return jsonify(error=str(e)), 500
return "ok"
# Everything not declared before (not a Flask route / API endpoint)...
@app.route("/<path:path>")
def route_frontend(path):
# ...could be a static file needed by the front end that
# doesn't use the `static` path (like in `<script src="bundle.js">`)
file_path = os.path.join(app.static_folder, path)
if os.path.isfile(file_path):
return send_file(file_path)
# ...or should be handled by the SPA's "router" in front end
else:
index_path = os.path.join(app.static_folder, "index.html")
return send_file(index_path)
if __name__ == "__main__":
# Only for debugging while developing
app.run(host="0.0.0.0", debug=True, port=8080)