-
Robert Latta authoredRobert Latta authored
ims_otrs.py 1.94 KiB
import csv
import html
from io import StringIO
import requests
from flask import Blueprint, request, Response, current_app
from inventory_provider.db import ims_data
from inventory_provider.db.ims import IMS
from inventory_provider.routes import common
from inventory_provider.tasks.ims_worker import OTRSFiles, export_data_for_otrs
routes = Blueprint("otrs", __name__)
@routes.after_request
def after_request(resp):
return common.after_request(resp)
def get_otrs_data(fn):
ims_config = current_app.config['INVENTORY_PROVIDER_CONFIG']["ims"]
ds = IMS(
ims_config['api'],
ims_config['username'],
ims_config['password'])
with StringIO() as sio:
writer = csv.writer(sio, delimiter='^')
writer.writerows(fn(ds))
data = sio.getvalue()
return Response(
response=data,
status=requests.codes.ok,
mimetype="text/html")
@routes.route('customer-companies')
def get_customer_companies_data():
return get_otrs_data(ims_data.otrs_get_customer_company_rows)
@routes.route('customer-users')
def get_customer_users_data():
return get_otrs_data(ims_data.otrs_get_customer_users_rows)
@routes.route('export')
def send_exports():
files_value = request.args.get('files', None)
if files_value:
try:
files_value = int(files_value)
except ValueError:
return Response(
response=html.escape('<files> should be an Integer'),
status=requests.codes.bad_request,
mimetype="text/html")
if files_value < 0 or files_value > sum(OTRSFiles):
return Response(
response=html.escape(f'Bad value for <files> {files_value}'),
status=requests.codes.bad_request,
mimetype="text/html")
task = export_data_for_otrs.delay(files_value)
return Response(
response=task.id,
status=requests.codes.ok,
mimetype="text/html")