add parent filter
This commit is contained in:
parent
44c5fbf284
commit
204362eac2
@ -4,7 +4,7 @@
|
|||||||
<h1>Cat list ({{ cat_list | length }})</h1>
|
<h1>Cat list ({{ cat_list | length }})</h1>
|
||||||
<div class="cat_header cat"
|
<div class="cat_header cat"
|
||||||
hx-get="/hx/cat_list"
|
hx-get="/hx/cat_list"
|
||||||
hx-include="input[name='sortby'], input[name='state_filters']"
|
hx-include="#catlist .cat_header input, #catlist .cat_header select"
|
||||||
hx-target="#catlist_data"
|
hx-target="#catlist_data"
|
||||||
hx-trigger="load, reloadCatList from:document, change, htmx:after-request from:(#cat-form form) queue: last"
|
hx-trigger="load, reloadCatList from:document, change, htmx:after-request from:(#cat-form form) queue: last"
|
||||||
>
|
>
|
||||||
@ -29,10 +29,14 @@
|
|||||||
<div>
|
<div>
|
||||||
Parent A
|
Parent A
|
||||||
<input type="radio" name="sortby" value="parent_a" />
|
<input type="radio" name="sortby" value="parent_a" />
|
||||||
|
<select name="fltr_parent" hx-get="/hx/get_parent_cat_options" hx-trigger="load, reloadCatList from:document", hhx-swap="innerHTML">
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Parent B
|
Parent B
|
||||||
<input type="radio" name="sortby" value="parent_b" />
|
<input type="radio" name="sortby" value="parent_b" />
|
||||||
|
<select name="fltr_parent" hx-get="/hx/get_parent_cat_options" hx-trigger="load, reloadCatList from:document", hhx-swap="innerHTML">
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
Stats
|
Stats
|
||||||
@ -50,4 +54,4 @@
|
|||||||
id="catlist_data"
|
id="catlist_data"
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
-</div>
|
</div>
|
||||||
|
|||||||
@ -4,6 +4,7 @@ from litestar.plugins.htmx import HTMXPlugin
|
|||||||
from litestar.contrib.jinja import JinjaTemplateEngine
|
from litestar.contrib.jinja import JinjaTemplateEngine
|
||||||
from litestar.template.config import TemplateConfig
|
from litestar.template.config import TemplateConfig
|
||||||
from litestar.static_files import create_static_files_router
|
from litestar.static_files import create_static_files_router
|
||||||
|
from advanced_alchemy.extensions.litestar import EngineConfig
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from .config import config
|
from .config import config
|
||||||
from .controllers import CatController, FrontendController, GenerationController
|
from .controllers import CatController, FrontendController, GenerationController
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
import itertools
|
||||||
from litestar import Controller, get, post
|
from litestar import Controller, get, post
|
||||||
from litestar.di import Provide
|
from litestar.di import Provide
|
||||||
from litestar.response import Template
|
from litestar.response import Template
|
||||||
from litestar.plugins.htmx import HTMXRequest, HTMXTemplate
|
from litestar.plugins.htmx import HTMXRequest, HTMXTemplate
|
||||||
from advanced_alchemy.filters import OrderBy, LimitOffset, CollectionFilter
|
from advanced_alchemy.filters import OrderBy, LimitOffset, CollectionFilter, ComparisonFilter, FilterGroup, LogicalOperatorMap
|
||||||
|
from sqlalchemy import or_
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
from ..models import Cat, CatRepository, StateEnum, SexEnum
|
from ..models import Cat, CatRepository, StateEnum, SexEnum
|
||||||
@ -40,6 +42,7 @@ class FrontendController(Controller):
|
|||||||
async def cat_list(
|
async def cat_list(
|
||||||
self,
|
self,
|
||||||
cats_repo: CatRepository,
|
cats_repo: CatRepository,
|
||||||
|
fltr_parent: list[str] | None = None,
|
||||||
state_filters: list[str] | None = None,
|
state_filters: list[str] | None = None,
|
||||||
sortby: str = 'name',
|
sortby: str = 'name',
|
||||||
sortorder: str = 'asc',
|
sortorder: str = 'asc',
|
||||||
@ -58,6 +61,18 @@ class FrontendController(Controller):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if fltr_parent:
|
||||||
|
for parent_id in map(UUID, filter(bool, fltr_parent)):
|
||||||
|
filters.append(
|
||||||
|
FilterGroup(
|
||||||
|
or_,
|
||||||
|
[
|
||||||
|
ComparisonFilter(Cat.parent_a, 'eq', parent_id),
|
||||||
|
ComparisonFilter(Cat.parent_b, 'eq', parent_id),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
elts = await cats_repo.list(
|
elts = await cats_repo.list(
|
||||||
*filters,
|
*filters,
|
||||||
OrderBy(sortby, sortorder)
|
OrderBy(sortby, sortorder)
|
||||||
@ -77,6 +92,33 @@ class FrontendController(Controller):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@get(path='/hx/get_parent_cat_options')
|
||||||
|
async def get_parent_cat_options(
|
||||||
|
self,
|
||||||
|
cats_repo: CatRepository,
|
||||||
|
) -> Template:
|
||||||
|
all_cats = await cats_repo.list()
|
||||||
|
parent_ids = set(itertools.chain.from_iterable([ (cat.parent_a, cat.parent_b) for cat in all_cats ]))
|
||||||
|
parent_cats = list(
|
||||||
|
filter(
|
||||||
|
lambda elt: elt.id in parent_ids,
|
||||||
|
all_cats
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return HTMXTemplate(
|
||||||
|
template_name='templates/cat_options.html.j2',
|
||||||
|
context={
|
||||||
|
'cat_list': parent_cats
|
||||||
|
},
|
||||||
|
re_target="this",
|
||||||
|
after="receive"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@get(path='/hx/cat_form')
|
@get(path='/hx/cat_form')
|
||||||
async def get_hx_cat_form(self,
|
async def get_hx_cat_form(self,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user