-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Schemas to handle missing body, empty transaction list, missing fields, additional date formats #48
Open
MattFaz
wants to merge
8
commits into
main
Choose a base branch
from
discord-reported-issues
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a4fbafe
Introduce Schemas to handle no body, missing body, missing fields
MattFaz a7126bc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] edf7e3d
Handle different date formats
MattFaz e0ead7c
Merge branch 'discord-reported-issues' of github.com:bobokun/actualta…
MattFaz c01b52b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e00dcf7
Handle date errors, remove models, simplify schema, remove redundant …
MattFaz b0960a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f18a4b2
Merge branch 'main' into discord-reported-issues
MattFaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ __pycache__/ | |
.tox | ||
*.env | ||
actualtap_py.egg-info/ | ||
|
||
# API Testing Client | ||
bruno/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import random | ||
import string | ||
import time | ||
|
||
from fastapi import HTTPException | ||
from fastapi import Security | ||
from fastapi.security.api_key import APIKeyHeader | ||
|
@@ -20,9 +16,3 @@ async def get_api_key(api_key_header: str = Security(api_key_header)): | |
status_code=403, | ||
detail="Could not validate credentials", | ||
) | ||
|
||
|
||
def generate_custom_id(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed as its only used in 1 function, and we can use the default python |
||
timestamp = str(int(time.time())) | ||
random_chars = "".join(random.choices(string.ascii_uppercase + string.digits, k=6)) | ||
return f"ID-{timestamp}-{random_chars}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
from datetime import date | ||
from datetime import datetime | ||
from typing import Union | ||
|
||
|
||
def convert_to_date(date_str: str) -> date: | ||
# Define the format of the input date string | ||
date_format = "%b %d, %Y" | ||
def convert_to_date(date_input: Union[str, datetime]) -> date: | ||
if isinstance(date_input, datetime): | ||
return date_input.date() | ||
|
||
# Parse the date string into a datetime object | ||
datetime_obj = datetime.strptime(date_str, date_format) | ||
# Try different date formats | ||
date_formats = [ | ||
"%Y-%m-%d", # 2024-11-25 (ISO format) | ||
"%b %d, %Y", # Nov 25, 2024 | ||
"%b %d %Y", # Nov 25 2024 | ||
] | ||
|
||
# Extract and return the date part | ||
return datetime_obj.date() | ||
for date_format in date_formats: | ||
try: | ||
datetime_obj = datetime.strptime(date_input, date_format) | ||
return datetime_obj.date() | ||
except ValueError: | ||
continue | ||
|
||
# If none of the formats worked, raise an error with examples | ||
raise ValueError( | ||
"Invalid date format. Accepted formats:\n" "- YYYY-MM-DD (e.g. 2024-11-25)\n" "- MMM DD, YYYY (e.g. Nov 25, 2024)\n" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from datetime import date, datetime | ||
from decimal import Decimal | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field, field_validator | ||
|
||
from core.util import convert_to_date | ||
|
||
|
||
class Transaction(BaseModel): | ||
account: str = Field(..., description="Account name or ID is required") | ||
amount: Decimal = Field(default=Decimal(0), description="Transaction amount") | ||
date: datetime = Field( | ||
default_factory=datetime.now, | ||
description=( | ||
"Transaction date in formats: YYYY-MM-DD, MMM DD, YYYY, or MMM DD YYYY" | ||
), | ||
) | ||
payee: Optional[str] = None | ||
notes: Optional[str] = None | ||
cleared: bool = False | ||
|
||
@field_validator("amount", mode="before") | ||
def validate_amount(cls, v): | ||
try: | ||
return Decimal(str(v)) if v else Decimal(0) | ||
except Exception: | ||
raise ValueError("Invalid amount format. Must be a valid decimal number.") | ||
|
||
@field_validator("date", mode="before") | ||
def parse_date(cls, value): | ||
try: | ||
parsed_date = convert_to_date(value) | ||
# If convert_to_date returns a date object, convert it to datetime | ||
if isinstance(parsed_date, date) and not isinstance(parsed_date, datetime): | ||
return datetime.combine(parsed_date, datetime.min.time()) | ||
return parsed_date | ||
except ValueError as e: | ||
raise ValueError(str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed as
/transaction
and/transactions/
will work regardless, no need to add both.Additionally we do not need the dependency on
get_api_key
here as it's already added inmain.py
when we add the route