Skip to content

Commit

Permalink
fix black and add pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrai2 committed Jul 26, 2024
1 parent 0f3d132 commit 0e5a70b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
14 changes: 11 additions & 3 deletions shiny_invoice/ui_existing_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tinydb import TinyDB, Query
from tinydb.operations import set


@module.ui
def existing_invoices_ui():
"""Defines the shiny ui for existing invoices"""
Expand Down Expand Up @@ -66,7 +67,12 @@ def get_filtered_invoices() -> pd.DataFrame | str:
df["Created At"] = df["Created At"].apply(lambda x: x.strftime("%d.%m.%Y"))
df["Due Date"] = df["Due Date"].apply(lambda x: x.strftime("%d.%m.%Y"))
df["Paid At"] = df["Paid At"].apply(
lambda x: datetime.datetime.strptime(x, "%Y-%m-%d").strftime("%d.%m.%Y") if x != "Unpaid" else "Unpaid")
lambda x: (
datetime.datetime.strptime(x, "%Y-%m-%d").strftime("%d.%m.%Y")
if x != "Unpaid"
else "Unpaid"
)
)
return df

def _filter_invoices(df):
Expand Down Expand Up @@ -110,12 +116,14 @@ def patch_table(patch):
Invoices = Query()
invoice_id = table_data.iloc[patch.get("row_index")]["Id"]
parsed_date = datetime.datetime.strptime(patch.get("value"), "%d.%m.%Y")
datastore.update(set("paid_at", parsed_date.strftime("%Y-%m-%d")), Invoices.id == invoice_id)
datastore.update(
set("paid_at", parsed_date.strftime("%Y-%m-%d")), Invoices.id == invoice_id
)
except Exception:
ui.notification_show(
"Error while updating invoice, please only use the date format '%d.%m.%Y'.",
type="error",
duration=6
duration=6,
)
return patch.get("value")

Expand Down
28 changes: 14 additions & 14 deletions shiny_invoice/ui_new_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def new_invoice_ui(config):

@module.server
def new_invoice_server(input, output, session, config):

datastore = TinyDB(config.get("paths").get("datastore"))

with open(Path(config.get("paths").get("html_template")), "r", encoding="utf8") as file:
Expand Down Expand Up @@ -81,8 +80,10 @@ def invoice_number_ui():
invoice_ids = [int(invoice.get("Id")) for invoice in datastore.all()]
next_id = 1
if invoice_ids:
next_id = max(invoice_ids) +1
number_ui = ui.input_text(id="invoice_number", label="Invoice Number", value=str(next_id), width="100%")
next_id = max(invoice_ids) + 1
number_ui = ui.input_text(
id="invoice_number", label="Invoice Number", value=str(next_id), width="100%"
)
return number_ui

@render.ui
Expand Down Expand Up @@ -112,23 +113,22 @@ def already_existing_id_modal():
)
def download_button():
"""Download the currently created invoice"""
datastore.insert({
"Id": input.invoice_number(),
"Created At": str(input.created_at_date()),
"Due Date": str(input.due_date()),
"Paid At": "Unpaid",
"Customer": customer_name(),
})
datastore.insert(
{
"Id": input.invoice_number(),
"Created At": str(input.created_at_date()),
"Due Date": str(input.due_date()),
"Paid At": "Unpaid",
"Customer": customer_name(),
}
)
ui.notification_show(
"Reload page to update 'Existing Invoices' view.",
type="message",
duration=None
"Reload page to update 'Existing Invoices' view.", type="message", duration=None
)
with io.BytesIO() as buf:
buf.write(render_invoice().encode("utf8"))
yield buf.getvalue()


@reactive.calc
def render_invoice():
total_net = calculate_totals()
Expand Down

0 comments on commit 0e5a70b

Please sign in to comment.