Comments (9)
Hi Daniel
Do you want set max_page_size
in general or in PiccoloCRUD
init method so that end user can change that param. For general we can achive this with small code changes like this in crud endpoints.py
.
class PiccoloCRUD(Router):
# Same limit as in csv export.
# With this we don't need to change anything in piccolo_admin
max_page_size : int = 1000
# unchanged code
async def _get_all(
self, params: t.Optional[t.Dict[str, t.Any]] = None
) -> Response:
# unchanged code
# Pagination
page_size = split_params.page_size or self.page_size
if page_size > self.max_page_size:
return JSONResponse(
{
"error": "The page size limit has been exceeded",
},
status_code=403,
)
# rest of unchanged code
If we want to change max_page_size
in Piccolo CRUD init method we need to adjust CSV export and dynamic page size selector in piccolo_admin
if end user set smaller max_page_size
than 1000 (for CSV) or 100 (for dynamic page size selector). I think that first choice is easier for prevent abuse of an endpoint and I think that we shouldn't allow (like in second choice) end user ability to change max_page_size
param, because end user can set limit too high. What do you think about that?
from piccolo_api.
@sinisaos I think just setting max_page_size
in the constructor for PiccoloCRUD
is OK - we could have it default to something quite large (say 1000).
The user should never be able to override this using URL params.
create_admin
could also accept a max_page_size
attribute, and pass it to any child PiccoloCRUD
instances. We wouldn't be able to set specific limits per endpoint, but this is probably OK.
It's a good point with the CSV download. We could expose the max_page_size
value in an endpoint like /api/meta
, /api/tables/<tablename>/meta
, or potentially even add it to /api/tables/<tablename>/schema
somehow. What do you think?
from piccolo_api.
@dantownsend We can do this via constructor and make changes like you said, but if we set max_page_size as a class variable and set it to 1000 (like code example in above comment), we only need to make changes to PiccoloCRUD
crud endpoints.py
because the class variable will be accesible to all instances and we don't need to change anything else. CSV export will work because the limit is already 1000. Also dynamic page size changes will work because max limit of 100 records per page will be smaller then 1000. If you don't like this way we can do it from constructor.
from piccolo_api.
@sinisaos I think adding it to the constructor should be OK.
class PiccoloCRUD(Router):
"""
Wraps a Piccolo table with CRUD methods for use in a REST API.
"""
def __init__(
self,
table: t.Type[Table],
read_only: bool = True,
allow_bulk_delete: bool = False,
page_size: int = 15,
max_page_size: int = 1000
) -> None:
self.max_page_size = max_page_size
...
If the default is set to 1000, it shouldn't effect much of the existing code.
from piccolo_api.
@dantownsend It's OK if you don't override in instance with max_size_page
in piccolo_admin endpoints.py
like this:
table_routes: t.List[BaseRoute] = [
Mount(
path=f"/{table._meta.tablename}/",
app=PiccoloCRUD(
table,
read_only=read_only,
page_size=page_size,
max_page_size=50, # if you do that CSV export and dynamic page size don't work
),
)
for table in tables
]
and we need to change more files in piccolo_admin
to make this work.
If max_page_size
is set as a class variable we can't change it in PiccoloCRUD
instance and CSV export (because the limit is already 1000) will work, and dynamic page size will work (because max limit of 100 records per page will be smaller then 1000).
Sorry, I can't explain it better but try it with example from my first comment and you see what I mean :).
from piccolo_api.
@sinisaos Ah, I understand now! Sorry.
What you're saying, is don't make the maximum configurable at all. I think that works for a first version, and as you say, it means less changes to other code.
from piccolo_api.
@dantownsend Yes exactly that :). If you want I can make PR with this code changes that you can make proper look at this.
from piccolo_api.
@sinisaos That would be great - thanks!
from piccolo_api.
@dantownsend Done. Cheers.
from piccolo_api.
Related Issues (20)
- Fix code scanning alert - Unused global variable
- Batch version of the get endpoint HOT 6
- Drop Python 3.7 support
- Catch foreign key constraint errors in `PiccoloCRUD`
- `schema_extra` param not passed to `pydantic_model_{output | optional | plural}` methods HOT 1
- Piccolo Admin API docs are not rendering
- Better error display on PATCH request
- Alpha version for Pydantic 2.0 / Piccolo 1.0a1? HOT 2
- PiccoloCRUD `post_single` return id of the inserted row instead of the row HOT 3
- Add `ne` operator
- Python 3.12 support
- RateLimitingMiddleware tests failed HOT 5
- Stop multi-dimensional arrays from breaking
- Issue with updating and bulk updating `BaseUser` via admin panel HOT 15
- Updating middleware syntax HOT 6
- Make `default-src` configurable in `CSPMiddleware`
- Hide parameter in Validators to hide Piccolo Admin table link from sidebar if the validators fail. HOT 8
- Add `excluded_paths` to `SessionsAuthBackend`
- Replace deprecated `abstractproperty`
- Fix for Pydantic breaking changes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from piccolo_api.