Cache zammad instances

Otherwise we end up with too many open files error.
This commit is contained in:
Michal Čihař 2020-08-18 11:24:50 +02:00
parent 0f8696adbb
commit bc0dd8301e

View file

@ -54,14 +54,22 @@ with open("rt2zammad.json") as handle:
config = json.load(handle) config = json.load(handle)
def get_zammad(**kwargs): ZAMMADS = {}
return ZammadAPI(
def get_zammad(user=None):
if user not in ZAMMADS:
kwargs = {}
if user:
kwargs["on_behalf_of"] = user
ZAMMADS[user] = ZammadAPI(
host=config["zammad_host"], host=config["zammad_host"],
username=config["zammad_user"], username=config["zammad_user"],
password=config["zammad_password"], password=config["zammad_password"],
is_secure=config["zammad_secure"], is_secure=config["zammad_secure"],
**kwargs **kwargs
) )
return ZAMMADS[user]
target = get_zammad() target = get_zammad()
@ -166,9 +174,7 @@ def get_user(userdata, attr="login"):
for ticket in tickets: for ticket in tickets:
label = "RT-{}".format(ticket["ticket"]["id"].split("/")[1]) label = "RT-{}".format(ticket["ticket"]["id"].split("/")[1])
print("Importing {}".format(label)) print("Importing {}".format(label))
new = get_zammad( new = get_zammad(get_user(users[ticket["ticket"]["Creator"]])).ticket.create(
on_behalf_of=get_user(users[ticket["ticket"]["Creator"]])
).ticket.create(
{ {
"title": "{} [{}]".format(ticket["ticket"]["Subject"], label), "title": "{} [{}]".format(ticket["ticket"]["Subject"], label),
"group": "Users", "group": "Users",
@ -208,7 +214,7 @@ for ticket in tickets:
chown = creator_id != new["customer_id"] chown = creator_id != new["customer_id"]
if chown: if chown:
target.ticket.update(new["id"], {"customer_id": creator_id}) target.ticket.update(new["id"], {"customer_id": creator_id})
TicketArticle(get_zammad(on_behalf_of=get_user(users[item["Creator"]]))).create( TicketArticle(get_zammad(get_user(users[item["Creator"]]))).create(
{ {
"ticket_id": new["id"], "ticket_id": new["id"],
"body": item["Content"], "body": item["Content"],