From 18bbcadb2c6f418b955a10dcf57c28a9784ac9a2 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 7 Dec 2021 21:49:51 +0100 Subject: [PATCH] handle Einsatzrapport WebDav Inbox --- pylokid/library/webdav.py | 24 +++++++++++++++++++----- pylokid/main.py | 2 ++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pylokid/library/webdav.py b/pylokid/library/webdav.py index 624f0fe..c87649a 100644 --- a/pylokid/library/webdav.py +++ b/pylokid/library/webdav.py @@ -4,6 +4,7 @@ import os import json +import re from datetime import datetime import logging import asyncio @@ -11,7 +12,7 @@ import aioeasywebdav class WebDav: - """ WebDav Client """ + """WebDav Client""" def __init__(self, url, username, password, webdav_basedir, tmp_dir): self.logger = logging.getLogger(__name__) @@ -32,7 +33,7 @@ class WebDav: self.logger.info("WebDAV connection successfull") def upload(self, file_name, f_id, check_exists=True): - """ uploads a file to webdav - checks for existence before doing so """ + """uploads a file to webdav - checks for existence before doing so""" # upload with webdav if f_id == None: @@ -64,7 +65,7 @@ class WebDav: self.logger.info('[%s] File "%s" uploaded', f_id, file_name) def einsatz_exists(self, f_id): - """ check if an einsatz is already created """ + """check if an einsatz is already created""" remote_upload_dir = ( self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id @@ -75,8 +76,21 @@ class WebDav: else: return False + def einsatzrapport_with_f_id_exists(self): + """check if an einsatzrapport with an f_id exists in the WebDav Inbox""" + + filelist = self.loop.run_until_complete( + self.webdav.ls(f"{self.webdav_basedir}/Inbox") + ) + for file in filelist: + full_path = file[0] + parsed = re.search(".*Einsatzrapport_(F[0-9].*)\.pdf", full_path) + if parsed: + f_id = parsed.group(1) + self.logger.info("[%s] Found %s", f_id, full_path) + def store_data(self, f_id, file_name, data): - """ stores data on webdav """ + """stores data on webdav""" file_path = os.path.join(self.tmp_dir, file_name) @@ -88,7 +102,7 @@ class WebDav: self.upload(file_name, f_id, False) def get_lodur_data(self, f_id, filetype="_lodur.json"): - """ gets lodur data if it exists """ + """gets lodur data if it exists""" file_name = f_id + filetype file_path = os.path.join(self.tmp_dir, file_name) diff --git a/pylokid/main.py b/pylokid/main.py index b58e3c5..421be0b 100644 --- a/pylokid/main.py +++ b/pylokid/main.py @@ -261,6 +261,8 @@ def main(): logger.error("[%s] Unknown type: %s", f_id, f_type) # TODO check webdav for Einsatzrapport PDF with f_id in filename and then process it + logger.info("Checking WebDav Inbox folder for Einsatzrapporte to process") + webdav_client.einsatzrapport_with_f_id_exists() # send heartbeat requests.get(HEARTBEAT_URL)