Skip to content

Commit

Permalink
optimized logging G and reduced complexity SIM #102
Browse files Browse the repository at this point in the history
  • Loading branch information
bensteUEM committed Oct 8, 2024
1 parent 1b7bf1f commit 13420a2
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 101 deletions.
45 changes: 21 additions & 24 deletions churchtools_api/churchtools_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ def login_ct_rest_api(self, **kwargs):
if response.status_code == 200:
response_content = json.loads(response.content)
logger.info(
"Token Login Successful as {}".format(
response_content["data"]["email"],
),
"Token Login Successful as %s",
response_content["data"]["email"],
)
self.session.headers["CSRF-Token"] = self.get_ct_csrf_token()
return json.loads(response.content)["data"]["id"]
logger.warning(
f"Token Login failed with {response.content.decode()}",
"Token Login failed with %s",
response.content.decode(),
)
return False

Expand All @@ -110,12 +110,11 @@ def login_ct_rest_api(self, **kwargs):
if response.status_code == 200:
response_content = json.loads(response.content)
person = self.who_am_i()
logger.info(
"User/Password Login Successful as {}".format(person["email"]),
)
logger.info("User/Password Login Successful as %s", person["email"])
return person["id"]
logger.warning(
f"User/Password Login failed with {response.content.decode()}",
"User/Password Login failed with %s",
response.content.decode(),
)
return False
return None
Expand All @@ -132,10 +131,11 @@ def get_ct_csrf_token(self):
response = self.session.get(url=url)
if response.status_code == 200:
csrf_token = json.loads(response.content)["data"]
logger.info(f"CSRF Token erfolgreich abgerufen {csrf_token}")
logger.debug("CSRF Token erfolgreich abgerufen %s", csrf_token)
return csrf_token
logger.warning(
f"CSRF Token not updated because of Response {response.content.decode()}",
"CSRF Token not updated because of Response %s",
response.content.decode(),
)
return None

Expand All @@ -150,15 +150,11 @@ def who_am_i(self):
if response.status_code == 200:
response_content = json.loads(response.content)
if "email" in response_content["data"]:
logger.info("Who am I as {}".format(response_content["data"]["email"]))
logger.info("Who am I as %s", response_content["data"]["email"])
return response_content["data"]
logger.warning(
"User might not be logged in? {}".format(response_content["data"]),
)
logger.warning("User might not be logged in? %s", response_content["data"])
return False
logger.warning(
f"Checking who am i failed with {response.status_code}",
)
logger.warning("Checking who am i failed with %s", response.status_code)
return False

def check_connection_ajax(self) -> bool:
Expand All @@ -173,7 +169,8 @@ def check_connection_ajax(self) -> bool:
logger.debug("Response AJAX Connection successful")
return True
logger.debug(
f"Response AJAX Connection failed with {json.load(response.content)}",
"Response AJAX Connection failed with %s",
json.load(response.content),
)
return False

Expand All @@ -188,7 +185,7 @@ def get_global_permissions(self) -> dict:
response_content = json.loads(response.content)
response_data = response_content["data"].copy()
logger.debug(
f"First response of Global Permissions successful {response_content}",
"First response of Global Permissions successful %s", response_content
)

return response_data
Expand Down Expand Up @@ -225,10 +222,11 @@ def get_services(self, **kwargs):
response_data = result

logger.debug(
f"Services load successful with {len(response_data)} entries",
"Services load successful with %s entries",
len(response_data),
)
return response_data
logger.info(f"Services requested failed: {response.status_code}")
logger.info("Services requested failed: %s", response.status_code)
return None

def get_tags(self, type="songs"):
Expand All @@ -250,7 +248,7 @@ def get_tags(self, type="songs"):
if response.status_code == 200:
response_content = json.loads(response.content)
response_content["data"].copy()
logger.debug(f"SongTags load successful {response_content}")
logger.debug("SongTags load successful %s", response_content)

return response_content["data"]
logger.warning(
Expand All @@ -277,8 +275,7 @@ def get_options(self) -> dict:
if response.status_code == 200:
response_content = json.loads(response.content)
response_data = response_content["data"].copy()

logger.debug(f"SongTags load successful {response_content}")
logger.debug("SongTags load successful %s", response_content)
return {item["name"]: item for item in response_data}
logger.warning(
"%s Something went wrong fetching Song-tags: %s",
Expand Down
34 changes: 16 additions & 18 deletions churchtools_api/churchtools_api_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@ def combine_paginated_response_data(
"""
response_data = response_content["data"].copy()

if meta := response_content.get("meta"):
if pagination := meta.get("pagination"):
for page in range(pagination["current"], pagination["lastPage"]):
logger.debug(
"running paginated request for page {} of {}".format(
page + 1,
pagination["lastPage"],
),
)
new_param = {"page": page + 1}
if kwargs.get("params"):
kwargs["params"].update(new_param)
else:
kwargs["params"] = new_param

response = self.session.get(url=url, **kwargs)
response_content = json.loads(response.content)
response_data.extend(response_content["data"])
if pagination := response_content.get("meta", {}).get("pagination"):
for page in range(pagination["current"], pagination["lastPage"]):
logger.debug(
"running paginated request for page %s of %s",
page + 1,
pagination["lastPage"],
)
new_param = {"page": page + 1}
if kwargs.get("params"):
kwargs["params"].update(new_param)
else:
kwargs["params"] = new_param

response = self.session.get(url=url, **kwargs)
response_content = json.loads(response.content)
response_data.extend(response_content["data"])
return response_data
55 changes: 31 additions & 24 deletions churchtools_api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,16 @@ def get_AllEventData_ajax(self, eventId) -> dict:
response_content = json.loads(response.content)
if len(response_content["data"]) > 0:
response_data = response_content["data"][str(eventId)]
logger.debug(f"AJAX Event data len= {len(response_data)}")
logger.debug("AJAX Event data len=%s", len(response_data))
return response_data
logger.info(
f"AJAX All Event data not successful - no event found: {response.status_code}",
"AJAX All Event data not successful - no event found:%s",
response.status_code,
)
return None
logger.info(
f"AJAX All Event data not successful: {response.status_code}",
"AJAX All Event data not successful: %s",
response.status_code,
)
return None

Expand Down Expand Up @@ -244,12 +246,13 @@ def set_event_services_counts_ajax(self, eventId, serviceId, servicesCount) -> b
# Generate form specific data
item_id = 0
data = {"id": eventId, "func": "addOrRemoveServiceToEvent"}
for serviceIdRow, serviceCount in servicesOfServiceGroup.items():
for item_id, (serviceIdRow, serviceCount) in enumerate(
servicesOfServiceGroup.items()
):
data[f"col{item_id}"] = serviceIdRow
if serviceCount > 0:
data[f"val{item_id}"] = "checked"
data[f"count{item_id}"] = serviceCount
item_id += 1

response = self.session.post(url=url, headers=headers, params=params, data=data)

Expand All @@ -266,11 +269,14 @@ def set_event_services_counts_ajax(self, eventId, serviceId, servicesCount) -> b
if number_match and response_success:
return True
logger.warning(
f"Request was successful but serviceId {serviceId} not changed to count {servicesCount} ",
"Request was successful but serviceId %s not changed to count %s ",
serviceId,
servicesCount,
)
return False
logger.info(
f"set_event_services_counts_ajax not successful: {response.status_code}",
"set_event_services_counts_ajax not successful: %s",
response.status_code,
)
return False

Expand Down Expand Up @@ -317,12 +323,15 @@ def set_event_admins_ajax(self, eventId: int, admin_ids: list) -> bool:
response_content = json.loads(response.content)
response_data = response_content["status"] == "success"
logger.debug(
f"Setting Admin IDs {admin_ids} for event {eventId} success",
"Setting Admin IDs %s for event %s success", admin_ids, eventId
)

return response_data
logger.info(
f"Setting Admin IDs {admin_ids} for event {eventId} failed with : {response.status_code}",
"Setting Admin IDs %s for event %s failed with : %s",
admin_ids,
eventId,
response.status_code,
)
return False

Expand All @@ -340,11 +349,12 @@ def get_event_agenda(self, eventId: int) -> list:
if response.status_code == 200:
response_content = json.loads(response.content)
response_data = response_content["data"].copy()
logger.debug(f"Agenda load successful {len(response_content)} items")
logger.debug("Agenda load successful %s items", len(response_content))

return response_data
logger.info(
f"Event requested that does not have an agenda with status: {response.status_code}",
"Event requested that does not have an agenda with status: %s",
response.status_code,
)
return None

Expand Down Expand Up @@ -427,17 +437,15 @@ def export_event_agenda(
if response.status_code == 200:
response_content = json.loads(response.content)
agenda_data = response_content["data"].copy()
logger.debug(f"Agenda package found {response_content}")
logger.debug("Agenda package found %s", response_content)
result_ok = self.file_download_from_url(
"{}/{}".format(self.domain, agenda_data["url"]),
target_path,
)
if result_ok:
logger.debug("download finished")
else:
logger.warning(
f"export of event_agenda failed: {response.status_code}",
)
logger.warning("export of event_agenda failed: %s", response.status_code)

return result_ok

Expand All @@ -452,7 +460,7 @@ def get_event_agenda_docx(self, agenda, **kwargs):
"""
excludeBeforeEvent = kwargs.get("excludeBeforeEvent", False)

logger.debug("Trying to get agenda for: " + agenda["name"])
logger.debug("Trying to get agenda for: %s", agenda["name"])

document = docx.Document()
heading = agenda["name"]
Expand All @@ -478,12 +486,10 @@ def get_event_agenda_docx(self, agenda, **kwargs):
document.add_heading(item["title"], level=1)
continue

if (
pre_event_last_item
): # helper for event start heading which is not part of the ct_api
if not item["isBeforeEvent"]:
pre_event_last_item = False
document.add_heading("Eventstart", level=1)
# helper for event start heading which is not part of the ct_api
if pre_event_last_item and not item["isBeforeEvent"]:
pre_event_last_item = False
document.add_heading("Eventstart", level=1)

agenda_item += 1

Expand Down Expand Up @@ -584,10 +590,11 @@ def get_event_masterdata(self, **kwargs) -> list | list[list] | dict | list[dict
if kwargs.get("returnAsDict"):
response_data2 = response_data.copy()
response_data = {item["id"]: item for item in response_data2}
logger.debug(f"Event Masterdata load successful {response_data}")
logger.debug("Event Masterdata load successful len=%s", response_data)

return response_data
logger.info(
f"Event Masterdata requested failed: {response.status_code}",
"Event Masterdata requested failed: %s",
response.status_code,
)
return None
17 changes: 8 additions & 9 deletions churchtools_api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def file_upload(
url = f"{self.domain}/api/files/{domain_type}/{domain_identifier}"

if overwrite:
logger.debug(f"deleting old file {source_file} before new upload")
logger.debug("deleting old file %s before new upload", source_file)
delete_file_name = (
source_file.name.split("/")[-1]
if custom_file_name is None
Expand All @@ -60,9 +60,7 @@ def file_upload(
if custom_file_name is None:
files = {"files[]": (source_file.name.split("/")[-1], source_file)}
elif "/" in custom_file_name:
logger.warning(
f"/ in file name ({custom_file_name}) will fail upload!",
)
logger.warning("/ in file name (%s) will fail upload!", custom_file_name)
files = {}
else:
files = {"files[]": (custom_file_name, source_file)}
Expand All @@ -86,7 +84,7 @@ def file_upload(
if response.status_code == 200:
try:
response_content = json.loads(response.content)
logger.debug(f"Upload successful {response_content}")
logger.debug("Upload successful len=%s", response_content)
return True
except (json.JSONDecodeError, TypeError, UnicodeDecodeError):
logger.warning(response.content.decode())
Expand Down Expand Up @@ -166,7 +164,8 @@ def file_download(
response_content = json.loads(response.content)
arrangement_files = response_content["data"].copy()
logger.debug(
f"SongArrangement-Files load successful {response_content}",
"SongArrangement-Files load successful len=%s",
response_content,
)
file_found = False

Expand All @@ -177,13 +176,13 @@ def file_download(
break

if file_found:
logger.debug(f"Found File: {filename}")
logger.debug("Found File: %s", filename)
# Build path OS independent
fileUrl = str(file["fileUrl"])
path_file = os.sep.join([target_path, filename])
StateOK = self.file_download_from_url(fileUrl, path_file)
else:
logger.warning(f"File {filename} does not exist")
logger.warning("File %s does not exist", filename)

return StateOK
logger.warning(
Expand Down Expand Up @@ -214,7 +213,7 @@ def file_download_from_url(self, file_url: str, target_path: str) -> bool:
# and set chunk_size parameter to None.
# if chunk:
f.write(chunk)
logger.debug(f"Download of {file_url} successful")
logger.debug("Download of %s successful",file_url)
return True
logger.warning(
"%s Something went wrong during file_download: %s",
Expand Down
Loading

0 comments on commit 13420a2

Please sign in to comment.