Skip to content

Commit

Permalink
replace dataclasses with old school classes (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Feb 24, 2022
1 parent ef69a27 commit d5eeee1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
5 changes: 5 additions & 0 deletions docs/source/contributing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Contributing

If you want to explore the interior of Piccolo API more deeply and help with the project, follow these instructions.

-------------------------------------------------------------------------------

Get the tests running
---------------------

Expand All @@ -19,6 +21,7 @@ Get the tests running
* Run the test suite with Postgres: ``./scripts/test-postgres.sh``
* Run the test suite with Sqlite: ``./scripts/test-sqlite.sh``

-------------------------------------------------------------------------------

Contributing to the docs
------------------------
Expand All @@ -31,6 +34,8 @@ The docs are written using Sphinx. To get them running locally:
* Serve the docs: ``python serve_docs.py``
* The docs will auto rebuild as you make changes.

-------------------------------------------------------------------------------

Code style
----------

Expand Down
54 changes: 37 additions & 17 deletions piccolo_api/crud/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import functools
import inspect
import typing as t
from dataclasses import dataclass, field

from starlette.exceptions import HTTPException
from starlette.requests import Request
Expand All @@ -15,27 +14,48 @@
ValidatorFunction = t.Callable[["PiccoloCRUD", Request], None]


@dataclass
class Validators:
"""
These validators are run by the corresponding method on
:class:`PiccoloCRUD`. Raises a Starlette exception if there is a problem.
:class:`PiccoloCRUD`.
The validator function is given the ``PiccoloCRUD`` instance, and the
Starlette ``Request`` instance, and should raise a Starlette
``HTTPException`` if there is a problem.
"""

every: t.List[ValidatorFunction] = field(default_factory=list)
get_single: t.List[ValidatorFunction] = field(default_factory=list)
put_single: t.List[ValidatorFunction] = field(default_factory=list)
patch_single: t.List[ValidatorFunction] = field(default_factory=list)
delete_single: t.List[ValidatorFunction] = field(default_factory=list)
post_single: t.List[ValidatorFunction] = field(default_factory=list)
get_all: t.List[ValidatorFunction] = field(default_factory=list)
delete_all: t.List[ValidatorFunction] = field(default_factory=list)
get_references: t.List[ValidatorFunction] = field(default_factory=list)
get_ids: t.List[ValidatorFunction] = field(default_factory=list)
get_new: t.List[ValidatorFunction] = field(default_factory=list)
get_schema: t.List[ValidatorFunction] = field(default_factory=list)
get_count: t.List[ValidatorFunction] = field(default_factory=list)
extra_context: t.Dict[str, t.Any] = field(default_factory=dict)
def __init__(
self,
every: t.List[ValidatorFunction] = [],
get_single: t.List[ValidatorFunction] = [],
put_single: t.List[ValidatorFunction] = [],
patch_single: t.List[ValidatorFunction] = [],
delete_single: t.List[ValidatorFunction] = [],
post_single: t.List[ValidatorFunction] = [],
get_all: t.List[ValidatorFunction] = [],
delete_all: t.List[ValidatorFunction] = [],
get_references: t.List[ValidatorFunction] = [],
get_ids: t.List[ValidatorFunction] = [],
get_new: t.List[ValidatorFunction] = [],
get_schema: t.List[ValidatorFunction] = [],
get_count: t.List[ValidatorFunction] = [],
extra_context: t.Dict[str, t.Any] = {},
):
self.every = every
self.get_single = get_single
self.put_single = put_single
self.patch_single = patch_single
self.delete_single = delete_single
self.post_single = post_single
self.get_all = get_all
self.delete_all = delete_all
self.get_references = get_references
self.get_ids = get_ids
self.get_new = get_new
self.get_schema = get_schema
self.get_count = get_count
self.extra_context = extra_context


def apply_validators(function):
Expand Down
29 changes: 19 additions & 10 deletions piccolo_api/fastapi/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import typing as t
from collections import defaultdict
from dataclasses import dataclass, field
from decimal import Decimal
from enum import Enum
from inspect import Parameter, Signature
Expand All @@ -26,20 +25,30 @@ class HTTPMethod(str, Enum):
delete = "DELETE"


@dataclass
class FastAPIKwargs:
"""
Allows kwargs to be passed into ``FastAPIApp.add_api_route``.
"""

all_routes: t.Dict[str, t.Any] = field(default_factory=dict)
get: t.Dict[str, t.Any] = field(default_factory=dict)
delete: t.Dict[str, t.Any] = field(default_factory=dict)
post: t.Dict[str, t.Any] = field(default_factory=dict)
put: t.Dict[str, t.Any] = field(default_factory=dict)
patch: t.Dict[str, t.Any] = field(default_factory=dict)
get_single: t.Dict[str, t.Any] = field(default_factory=dict)
delete_single: t.Dict[str, t.Any] = field(default_factory=dict)
def __init__(
self,
all_routes: t.Dict[str, t.Any] = {},
get: t.Dict[str, t.Any] = {},
delete: t.Dict[str, t.Any] = {},
post: t.Dict[str, t.Any] = {},
put: t.Dict[str, t.Any] = {},
patch: t.Dict[str, t.Any] = {},
get_single: t.Dict[str, t.Any] = {},
delete_single: t.Dict[str, t.Any] = {},
):
self.all_routes = all_routes
self.get = get
self.delete = delete
self.post = post
self.put = put
self.patch = patch
self.get_single = get_single
self.delete_single = delete_single

def get_kwargs(self, endpoint_name: str) -> t.Dict[str, t.Any]:
"""
Expand Down

0 comments on commit d5eeee1

Please sign in to comment.