Skip to content

Commit

Permalink
chore(#31): apply easy ruff linting suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
bensteUEM committed Jan 15, 2025
1 parent ed798ea commit 94116d7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 81 deletions.
19 changes: 10 additions & 9 deletions church_web_helper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import logging
import logging.config
import os
from datetime import datetime, time
import re
import urllib
from datetime import datetime, time, timedelta
from datetime import datetime, time
from pathlib import Path

import pytz
import pandas as pd
import pytz
import toml
import vobject
from churchtools_api.churchtools_api import ChurchToolsApi as CTAPI
Expand Down Expand Up @@ -43,11 +42,11 @@
from church_web_helper.helper import (
deduplicate_df_index_with_lists,
extract_relevant_calendar_appointment_shortname,
get_group_name_services,
get_plan_months_docx,
get_plan_months_xlsx,
get_primary_resource,
get_special_day_name,
get_group_name_services,
get_title_name_services,
)

Expand Down Expand Up @@ -244,8 +243,8 @@ def download_events():
event_choices=event_choices,
service_groups=session["serviceGroups"],
)
elif request.method == "POST":
if "event_id" not in request.form.keys():
if request.method == "POST":
if "event_id" not in request.form:
return redirect(url_for("download_events"))
event_id = int(request.form["event_id"])
if "submit_docx" in request.form:
Expand Down Expand Up @@ -396,7 +395,7 @@ def download_plan_months():
from_date=from_date,
to_date=to_date,
)
elif request.method == "POST":
if request.method == "POST":
logger.info("Responding to POST request")

selected_calendars = [
Expand Down Expand Up @@ -661,7 +660,7 @@ def download_plan_months():
to_date=to_date,
)

elif action == "DOCx Document Download":
if action == "DOCx Document Download":
logger.debug("Preparing Download as DOCx")
document = get_plan_months_docx(df_data, from_date=from_date)
filename = f"Monatsplan_{from_date.strftime('%Y_%B')}.docx"
Expand All @@ -672,7 +671,7 @@ def download_plan_months():
os.remove(filename)
return response

elif action == "Excel Download":
if action == "Excel Download":
logger.debug("Preparing Download as Excel")
filename = f"Monatsplan_{from_date.strftime('%Y_%B')}.xlsx"
workbook = get_plan_months_xlsx(
Expand All @@ -684,6 +683,8 @@ def download_plan_months():
)
os.remove(filename)
return response
return None
return None


@app.route("/ct/calendar_appointments")
Expand Down
73 changes: 30 additions & 43 deletions church_web_helper/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
It is used to outsource parts which don't need to be part of app.py
"""

from collections import OrderedDict
import logging
from churchtools_api.churchtools_api import ChurchToolsApi as CTAPI
from dateutil.relativedelta import relativedelta
from collections import OrderedDict
from datetime import datetime

from datetime import datetime, timedelta
import docx
from docx.shared import Pt, RGBColor, Cm
import docx.table
import pandas as pd
import xlsxwriter
from churchtools_api.churchtools_api import ChurchToolsApi as CTAPI
from dateutil.relativedelta import relativedelta
from docx.oxml import OxmlElement, ns
from docx.oxml.ns import qn

import xlsxwriter
from docx.shared import Cm, Pt, RGBColor

logger = logging.getLogger(__name__)

Expand All @@ -43,15 +42,14 @@ def get_special_day_name(
from_=trunc_date,
to_=trunc_date + relativedelta(days=1) - relativedelta(seconds=1),
)
if special_names:
if len(special_names) > 0:
return special_names[0]["caption"]
if special_names and len(special_names) > 0:
return special_names[0]["caption"]

return ""


def extract_relevant_calendar_appointment_shortname(longname: str) -> str:
"""Tries to extract a shortname for "special ocasion events" based on a mapping
"""Tries to extract a shortname for "special ocasion events" based on a mapping.
Excecution order is relevant!
Expand Down Expand Up @@ -100,7 +98,7 @@ def extract_relevant_calendar_appointment_shortname(longname: str) -> str:


def get_plan_months_docx(data: pd.DataFrame, from_date: datetime) -> docx.Document:
"""Function which converts a Dataframe into a DOCx document used for final print modifications
"""Function which converts a Dataframe into a DOCx document used for final print modifications.
Args:
data: pre-formatted data to be used as base
Expand All @@ -109,7 +107,6 @@ def get_plan_months_docx(data: pd.DataFrame, from_date: datetime) -> docx.Docume
Returns:
document reference
"""

document = docx.Document()
padding_left = 1.5
padding_right = -0.25
Expand All @@ -130,7 +127,7 @@ def get_plan_months_docx(data: pd.DataFrame, from_date: datetime) -> docx.Docume
run.font.size = Pt(32)
run.font.color.rgb = RGBColor.from_string("000000")

locations = set([item[0] for item in data.columns[2:]])
locations = {item[0] for item in data.columns[2:]}

table = document.add_table(rows=1, cols=len(locations) + 1)
hdr_cells = table.rows[0].cells
Expand All @@ -141,7 +138,7 @@ def get_plan_months_docx(data: pd.DataFrame, from_date: datetime) -> docx.Docume
for run in paragraph.runs:
run.bold = True

for index, df_row in data.iterrows():
for _index, df_row in data.iterrows():
row_cells = table.add_row().cells
para = row_cells[0].paragraphs[0]
para.add_run(df_row["shortDay"].iloc[0]).add_break()
Expand Down Expand Up @@ -172,7 +169,7 @@ def get_plan_months_docx(data: pd.DataFrame, from_date: datetime) -> docx.Docume
def get_plan_months_xlsx(
data: pd.DataFrame, from_date: datetime, filename: str
) -> xlsxwriter.Workbook:
"""Function which converts a Dataframe into a XLXs used as admin overview printout
"""Function which converts a Dataframe into a XLXs used as admin overview printout.
Args:
data: pre-formatted data to be used as base
Expand All @@ -182,13 +179,12 @@ def get_plan_months_xlsx(
Returns:
workbook reference
"""

workbook = xlsxwriter.Workbook(filename)

heading = f"{from_date.strftime('%B %Y')}"
worksheet = workbook.add_worksheet(name=heading)

locations = set([item[0] for item in data.columns[2:]])
locations = {item[0] for item in data.columns[2:]}

row = 0

Expand Down Expand Up @@ -228,7 +224,7 @@ def get_plan_months_xlsx(
TIME | Predigt | ABM | Organist
| | Taufe | Musik
"""

column_offsets = [0, 1, 2, 3, 0, 1, 2, 3]
Expand All @@ -246,7 +242,7 @@ def get_plan_months_xlsx(

# Iterate all data rows
location_column_offset = 0
for index, df_row in data.iterrows():
for _index, df_row in data.iterrows():
format_content = workbook.add_format({"bold": True, "border": 1})
format_content_b = workbook.add_format({"bold": True, "border": 1, "bottom": 2})

Expand All @@ -255,12 +251,12 @@ def get_plan_months_xlsx(

max_events_per_date = max([len(i) for i in df_row[slice(None), "shortTime"]])
for row_offset, column_offset, column_value in zip(
row_offsets, column_offsets, column_references
row_offsets, column_offsets, column_references, strict=False
):
for location_index, location in enumerate(locations):
location_column_offset = location_index * NUMBER_OF_COLUMNS_PER_LOCATION

for event_per_day_offset in range(0, max_events_per_date):
for event_per_day_offset in range(max_events_per_date):
value = ""
if column_value in df_row[location].index:
if len(df_row[location, column_value]) > event_per_day_offset:
Expand All @@ -285,15 +281,14 @@ def get_plan_months_xlsx(


def deduplicate_df_index_with_lists(df_input: pd.DataFrame) -> pd.DataFrame:
"""Flattens a df with multiple same index entries to list entries
"""Flattens a df with multiple same index entries to list entries.
Args:
df_input: the original dataframe which contains multiple entries for "shortDay" index per column
Returns:
flattened df which has unique shortDay and combined lists in cells
"""

shortDays = list(OrderedDict.fromkeys(df_input["shortDay"]).keys())
df_output = pd.DataFrame(columns=df_input.columns)
for shortDay in shortDays:
Expand Down Expand Up @@ -332,7 +327,7 @@ def deduplicate_df_index_with_lists(df_input: pd.DataFrame) -> pd.DataFrame:
def generate_event_paragraph(
target_cell: docx.table._Cell, relevant_entry: pd.Series
) -> None:
"""function which generates the content of one table cell.
"""Function which generates the content of one table cell.
Used with get_plan_months_docx
Iterates through all items in relevant row and using the columns to generate the text.
Expand Down Expand Up @@ -372,12 +367,11 @@ def generate_event_paragraph(


def change_table_format(table: docx.table) -> None:
"""Inplace overwrite of styles
"""Inplace overwrite of styles.
Args:
table: the table to modify
"""

# Access the XML element of the table and move ident because by default it's 1,9cm off
tbl_pr = table._element.xpath("w:tblPr")[0]
tbl_indent = OxmlElement("w:tblInd")
Expand All @@ -401,8 +395,8 @@ def change_table_format(table: docx.table) -> None:

def set_page_margins(
doc: docx.Document, top: float, bottom: float, left: float, right: float
):
"""Helper to set document page borders in cm
) -> None:
"""Helper to set document page borders in cm.
Args:
doc: the document to change
Expand All @@ -420,7 +414,7 @@ def set_page_margins(
section.right_margin = Cm(right)


def set_cell_border(cell):
def set_cell_border(cell) -> None:
"""Function to add borders to a cell.
Args:
Expand All @@ -443,7 +437,7 @@ def set_cell_border(cell):
tcPr.append(tcBorders)


def set_cell_margins(cell, top=0, start=0, bottom=0, end=0):
def set_cell_margins(cell, top=0, start=0, bottom=0, end=0) -> None:
"""Function to set cell margins (padding).
Args:
Expand Down Expand Up @@ -486,7 +480,7 @@ def get_primary_resource(
api: CTAPI,
considered_resource_ids: list[int],
) -> str:
"""Helper which is used to get the primary resource allocation of an event
"""Helper which is used to get the primary resource allocation of an event.
Args:
appointment_id: id of calendar entry
Expand All @@ -502,9 +496,7 @@ def get_primary_resource(
to_=relevant_date,
appointment_id=appointment_id,
)
locations = {booking["base"]["resource"]["name"] for booking in bookings}

return locations
return {booking["base"]["resource"]["name"] for booking in bookings}


def get_title_name_services(
Expand All @@ -515,8 +507,7 @@ def get_title_name_services(
considered_program_services: list[int],
considered_groups: list[int],
) -> str:
"""
Helper function which retrieves a text representation of a service including the persons title based on considered groups.
"""Helper function which retrieves a text representation of a service including the persons title based on considered groups.
1. Lookup relevant services
2. Lookup the prefix of the person to be used based on group assignemnts
Expand Down Expand Up @@ -562,7 +553,7 @@ def get_title_name_services(
def get_group_title_of_person(
person_id: int, relevant_groups: list[int], api: CTAPI
) -> str:
"""Retrieve name of first group for specified person and gender if possible
"""Retrieve name of first group for specified person and gender if possible.
Args:
person_id: CT id of the user
Expand Down Expand Up @@ -630,7 +621,6 @@ def get_group_name_services(
Returns:
text which can be used as suffix - empty in case no special service
"""

relevant_event = api.get_event_by_calendar_appointment(
appointment_id=appointment_id, start_date=relevant_date
)
Expand Down Expand Up @@ -661,7 +651,4 @@ def get_group_name_services(
for group_id in relevant_group_ids:
group = api.get_groups(group_id=group_id)[0]
result_groups.append(group["name"])
result_string = (
"mit " + " und ".join(result_groups) if len(result_groups) > 0 else ""
)
return result_string
return "mit " + " und ".join(result_groups) if len(result_groups) > 0 else ""
Loading

0 comments on commit 94116d7

Please sign in to comment.