Skip to content
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

Import JSON #51

Merged
merged 3 commits into from
Oct 18, 2023
Merged

Import JSON #51

merged 3 commits into from
Oct 18, 2023

Conversation

abhinna1
Copy link
Contributor

import from JSON implemented | import from CSV refactored | recurring expenses template duplicate messages removed.

Copy link
Owner

@iokiwi iokiwi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies that it took me a while to get around to reviewing this. On a whole it looks pretty good! Just a few small bits that could use a brush up :)

I love that it passes flake8 and black 💯 thanks heaps for this contribution, and keep up the great work.

raise ValueError("Invalid CSV data in the file.")


def upload_json_date(data: dict):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is _date a typo? Should it be data instead?

Copy link
Owner

@iokiwi iokiwi Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to update everywhere this method is called as well :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I love the type hint :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a typo, I'll fix it in the next commit.

for item in data:
try:
recurring_expense = RecurringExpense(
id=item["id"] if "id" in item else None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idiomatic python way of doing this is

id=item.get("id")
# None

If item does not have an id, item.get("id") will return None

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, you can also supply a default value to get. So you can also do item.get("foo", "bar")

If foo is not a key, the default value bar will be returned instead


try:
recurring_expense = RecurringExpense(
id=UUID(row[0]) if row[0] else None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the intended behavior here is? Unless the row is empty, there will always be a row[0] right?
If the row is completely empty, then we probably wont make it to this line

What are we trying to check here? Maybe it handles the following scenario

id,active,particulars,amount,currency,amount_nzd,period
00000000-0000-0000-0000-000000000000,True,Foo,1.00,NZD,1.00,1
,True,Bar,1.00,NZD,1.00,1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually put that so that the ID in case the ID is not provided by the user in the import file, it's generated automatically, but we can have it mandatory as well.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah thats fine. I sort of realised the answer while I was typing the question I think its fine as it is?

id=UUID(row[0]) if row[0] else None,
active=parse_bool(row[1]),
particulars=row[2].strip(" "),
amount=row[3].strip("$ "),
Copy link
Owner

@iokiwi iokiwi Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm suprised this doesn't need to be converted to Decimal? It seems to work none the less so 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so, the csv upload logic was per-written I just refactored it into a different function. Guess it's fine?

Comment on lines 122 to 124
if uploaded_file.content_type not in _IMPORT_CONTENT_TYPES:
messages.error(request, "Invalid file type")
return HttpResponseRedirect(reverse("recurring_expenses:import"))
Copy link
Owner

@iokiwi iokiwi Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that we are checking to make sure the file's content type is one of the expected content_types

Comment on lines 43 to 48
_IMPORT_CONTENT_KEYS = {
"json": "application/json",
"csv": "text/csv",
}

_IMPORT_CONTENT_TYPES = {value: key for key, value in _IMPORT_CONTENT_KEYS.items()}
Copy link
Owner

@iokiwi iokiwi Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants are fine but in python we don't really prefix them with an _

I would prefer _IMPORT_CONTENT_KEYS becomes IMPORT_CONTENT_KEYS

Same for any other constants

https://realpython.com/python-constants/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will rename those.

app/recurring_expenses/views.py Show resolved Hide resolved
@iokiwi iokiwi self-assigned this Oct 18, 2023
@abhinna1
Copy link
Contributor Author

@iokiwi I've made the appropriate changes 😄

@iokiwi iokiwi merged commit 3b63ea6 into iokiwi:main Oct 18, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants