Skip to content

Commit

Permalink
return form description in endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Oct 16, 2024
1 parent 01764d0 commit c89da10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
42 changes: 21 additions & 21 deletions piccolo_admin/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,21 @@ class GenerateFileURLResponseModel(BaseModel):
file_url: str = Field(description="A URL which the file is accessible on.")


class GroupItem(BaseModel):
name: str
slug: str


class GroupedTableNamesResponseModel(BaseModel):
grouped: t.Dict[str, t.List[str]] = Field(default_factory=list)
ungrouped: t.List[str] = Field(default_factory=list)


class GroupedFormNamesResponseModel(BaseModel):
grouped: t.Dict[str, t.List[t.Dict[str, t.Any]]] = Field(
class GroupedFormsResponseModel(BaseModel):
grouped: t.Dict[str, t.List[FormConfigResponseModel]] = Field(
default_factory=list
)
ungrouped: t.List[t.Dict[str, t.Any]] = Field(default_factory=list)
ungrouped: t.List[FormConfigResponseModel] = Field(default_factory=list)


@dataclass
Expand Down Expand Up @@ -638,9 +643,9 @@ def __init__(

private_app.add_api_route(
path="/forms/grouped/",
endpoint=self.get_form_list_grouped, # type: ignore
endpoint=self.get_grouped_forms, # type: ignore
methods=["GET"],
response_model=GroupedFormNamesResponseModel,
response_model=GroupedFormsResponseModel,
tags=["Forms"],
)

Expand Down Expand Up @@ -959,12 +964,12 @@ def get_forms(self) -> t.List[FormConfigResponseModel]:
for form in self.forms
]

def get_form_list_grouped(self) -> GroupedFormNamesResponseModel:
def get_grouped_forms(self) -> GroupedFormsResponseModel:
"""
Returns a list of custom forms registered with the admin,
grouped using `form_group`.
Returns a list of custom forms registered with the admin, grouped using
`form_group`.
"""
response = GroupedFormNamesResponseModel()
response = GroupedFormsResponseModel()
group_names = sorted(
{
v.form_group
Expand All @@ -975,20 +980,15 @@ def get_form_list_grouped(self) -> GroupedFormNamesResponseModel:
response.grouped = {i: [] for i in group_names}
for _, form_config in self.form_config_map.items():
form_group = form_config.form_group
form_config_response = FormConfigResponseModel(
name=form_config.name,
slug=form_config.slug,
description=form_config.description,
)
if form_group is None:
response.ungrouped.append(
{
"name": form_config.name,
"slug": form_config.slug,
}
)
response.ungrouped.append(form_config_response)
else:
response.grouped[form_group].append(
{
"name": form_config.name,
"slug": form_config.slug,
}
)
response.grouped[form_group].append(form_config_response)

return response

Expand Down
18 changes: 16 additions & 2 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,19 +503,33 @@ def test_forms_grouped(self):
"grouped": {
"Download forms": [
{
"description": "Download a list of movies for "
"the director as a CSV file.",
"name": "Download director movies",
"slug": "download-director-movies",
},
{
"description": "Download the schedule for the "
"day.",
"name": "Download schedule",
"slug": "download-schedule",
},
],
"Text forms": [
{"name": "Booking form", "slug": "booking-form"}
{
"description": "Make a booking for a customer.",
"name": "Booking form",
"slug": "booking-form",
}
],
},
"ungrouped": [{"name": "Calculator", "slug": "calculator"}],
"ungrouped": [
{
"description": "Adds two numbers together.",
"name": "Calculator",
"slug": "calculator",
}
],
},
)

Expand Down

0 comments on commit c89da10

Please sign in to comment.