-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
7 deletions.
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
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,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) |
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,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! |
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