From d70e5c116bbc9056b58c2a4e2e52ad40cacb7a89 Mon Sep 17 00:00:00 2001 From: Trey <73353716+TreyWW@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:33:47 +0100 Subject: [PATCH] Minor enhancements --- backend/admin.py | 4 +++ backend/utils/dataclasses.py | 51 +++++++++++++++++++++++++++++++++ docs/debugging/python/poetry.md | 21 ++++++++++++++ pyproject.toml | 13 ++++----- 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 backend/utils/dataclasses.py create mode 100644 docs/debugging/python/poetry.md diff --git a/backend/admin.py b/backend/admin.py index c2816df8..81f81390 100644 --- a/backend/admin.py +++ b/backend/admin.py @@ -85,6 +85,10 @@ class EmailSendStatusAdmin(admin.ModelAdmin): readonly_fields = ["aws_message_id"] +class InvoiceURLAdmin(admin.ModelAdmin): + readonly_fields = ["expires"] + + admin.site.register(QuotaLimit, QuotaLimitAdmin) admin.site.register(QuotaUsage, QuotaUsageAdmin) admin.site.register(QuotaOverrides, QuotaOverridesAdmin) diff --git a/backend/utils/dataclasses.py b/backend/utils/dataclasses.py new file mode 100644 index 00000000..a27b7534 --- /dev/null +++ b/backend/utils/dataclasses.py @@ -0,0 +1,51 @@ +from typing import TypeVar + + +T = TypeVar("T") + + +def extract_to_dataclass(request, class_type: [T], request_types: list[str], *args, **kwargs) -> [T]: + """ + + Turn kwargs from Key:Value and get request.POST.get(value) and set class.key = request.POST.get(value) + + Usage: + + from pydantic.dataclasses import dataclass + from typing import Optional + + @dataclass + class MyView: + name: str + age: int + different: bool + non_required: Optional[str] + + def myview(request): + try: + data = extract_to_dataclass(request, MyView, ["post"], "name", "age", diff_bool="different") + except pydantic.ValidationError: + pass + """ + data = {} + if "get" in request_types: + if args: + data |= {key: request.GET.get(key) for key in args} + + if kwargs: + data |= {key: request.GET.get(value) for key, value in kwargs.items()} + + if "post" in request_types: + if args: + data |= {key: request.POST.get(key) for key in args} + + if kwargs: + data |= {key: request.POST.get(value) for key, value in kwargs.items()} + + if "headers" in request_types: + if args: + data |= {key: request.headers.get(key) for key in args} + + if kwargs: + data |= {key: request.headers.get(value) for key, value in kwargs.items()} + return class_type(**data) diff --git a/docs/debugging/python/poetry.md b/docs/debugging/python/poetry.md new file mode 100644 index 00000000..cd658664 --- /dev/null +++ b/docs/debugging/python/poetry.md @@ -0,0 +1,21 @@ +# Already installed + +The error where `poetry install` says "Skipped for the following reason: Already installed" but `pip freeze` shows that these +clearly aren't installed, you may need to configure your poetry environment to use the local ./venv/ instead of a global one. + +First, make sure you aren't in a venv (`deactivate`). If there isn't a /venv/ folder inside of the project, you'll need to +create one with `python -m venv ./venv/`. Now activate it. `./venv/Scripts/activate`. + +After you have a venv and it's activated you can run these poetry config commands: + +```shell +pip install poetry + +poetry config virtualenvs.in-project true + +poetry config virtualenvs.path ./venv/ + +poetry install +``` + +And this time they should all be correctly installed! diff --git a/pyproject.toml b/pyproject.toml index e9e69f85..47e9a536 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,5 @@ [tool.poetry] name = "MyFinances" -package-mode = false version = "0.3.0" description = "github.com/TreyWW/MyFinances" authors = ["TreyWW"] @@ -20,12 +19,6 @@ django-mathfilters = "^1.0.0" sample-helper-aws-appconfig = "^2.1.0" redis = { extras = ["hiredis"], version = "^5.0.1" } -djangorestframework = "^3.14.0" -django-tz-detect = "^0.5.0" -typos = "^1.20.3" -pre-commit = "^3.7.0" -pulumi-aws = "^6.29.0" - [tool.poetry.group.mypy.dependencies] mypy = "1.7.1" djangorestframework-stubs = { extras = ["compatible-mypy"], version = "^3.14.5" } @@ -37,6 +30,12 @@ boto3-stubs = { extras = [ "stepfunctions", ], version = "^1.34.76" } +djangorestframework = "^3.14.0" +django-tz-detect = "^0.5.0" +typos = "^1.20.3" +pre-commit = "^3.7.0" +pulumi-aws = "^6.29.0" + [tool.poetry.group.django.dependencies] Django = "^5.0.2" gunicorn = "22.0.0"