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

Enhancement: add "request" as a parameter in the method get_list_query #384

Closed
whchi opened this issue Nov 6, 2023 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@whchi
Copy link
Contributor

whchi commented Nov 6, 2023

Is your feature request related to a problem? Please describe.
I am developing a feature that requires filtering my relationships based on a specific column.

The relationship is such that 1 post can have N categories. When I am on my category page.

I want to select posts where the status is "published" and deleted_at is "None".

Describe the solution you'd like
I found this discussion #236 which helped me in resolving my issue.

Describe alternatives you've considered
I have also considered whether there is a way for the current model to know where the request is coming from when using get_list_query.

Additional context
I have overridden the get_list_query as follows:

    def get_list_query(self, referer: str | None) -> Select:
        if referer:
            import urllib.parse
            path = urllib.parse.urlparse(referer).path
            for field in self.fields:
                if isinstance(field, HasOne) and field.identity == 'categories' and path.startswith('/admin/categories'):
                    return select(self.model).where(
                        self.model.status == Status.PUBLISHED,
                        is_(self.model.deleted_at, None),
                    )

The async def find_all method seems a bit complicated:

    async def find_all(
            self,
            request: Request,
            skip: int = 0,
            limit: int = 100,
            where: Union[Dict[str, Any], str, None] = None,
            order_by: Optional[List[str]] = None,
    ) -> Sequence[Any]:
        ...
        # The only line I need is to add the referer from request.headers
        stmt = self.get_list_query(request.headers.get('referer')).offset(skip)
        ... # rest of the original code

If it is possible to pass the request into the get_list_query method, then there might be no need to override the async def find_all method.

@whchi whchi added the enhancement New feature or request label Nov 6, 2023
@whchi whchi changed the title Enhancement: add "request" as parameter in the method get_list_query Enhancement: add "request" as a parameter in the method get_list_query Nov 6, 2023
@sglebs
Copy link

sglebs commented Nov 7, 2023

I also needed this. I added it manually, see #274

@hasansezertasan
Copy link
Contributor

I also need this one.

What do you think about this one @jowilf?

@jowilf
Copy link
Owner

jowilf commented Jan 29, 2024

Duplicate of #489

@jowilf jowilf marked this as a duplicate of #489 Jan 29, 2024
@jowilf jowilf closed this as completed Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants