From 2700b961967284bfd2ac27952e475b03c8255612 Mon Sep 17 00:00:00 2001 From: Daniel Townsend Date: Sun, 16 Jan 2022 21:11:53 +0000 Subject: [PATCH 1/3] modified example schema so we can test ForeignKey with target_column set --- piccolo_admin/example.py | 31 +++++++++++++++++++++++++++---- piccolo_admin/example_data.py | 21 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/piccolo_admin/example.py b/piccolo_admin/example.py index 40bfdf83..d89dd86d 100644 --- a/piccolo_admin/example.py +++ b/piccolo_admin/example.py @@ -41,7 +41,13 @@ from pydantic import BaseModel, validator from piccolo_admin.endpoints import FormConfig, TableConfig, create_admin -from piccolo_admin.example_data import DIRECTORS, MOVIE_WORDS, MOVIES, STUDIOS +from piccolo_admin.example_data import ( + DIRECTORS, + MOVIE_WORDS, + MOVIES, + REVIEWS, + STUDIOS, +) class Sessions(SessionsBase): @@ -75,7 +81,7 @@ def get_readable(cls): class Studio(Table, help_text="A movie studio."): pk = UUID(primary_key=True) - name = Varchar() + name = Varchar(unique=True) facilities = JSON() @@ -90,7 +96,7 @@ class Genre(int, enum.Enum): romance = 7 musical = 8 - name = Varchar(length=300) + name = Varchar(length=300, unique=True) rating = Real(help_text="The rating on IMDB.") duration = Interval() director = ForeignKey(references=Director) @@ -104,6 +110,21 @@ class Genre(int, enum.Enum): genre = SmallInt(choices=Genre, null=True) studio = ForeignKey(Studio) + @classmethod + def get_readable(cls): + return Readable(template="%s", columns=[cls.name]) + + +class Review(Table): + reviewer = Varchar() + content = Text() + rating = Integer() + movie = ForeignKey(Movie, target_column=Movie.name) + + @classmethod + def get_readable(cls): + return Readable(template="%s %s", columns=[cls.reviewer, cls.rating]) + class BusinessEmailModel(BaseModel): email: str @@ -176,6 +197,7 @@ async def booking_endpoint(request, data): Director, Movie, Studio, + Review, User, Sessions, ) @@ -208,7 +230,7 @@ async def booking_endpoint(request, data): ) APP = create_admin( - [movie_config, director_config, Studio], + [movie_config, director_config, Studio, Review], forms=[ FormConfig( name="Business email form", @@ -261,6 +283,7 @@ def populate_data(inflate: int = 0, engine: str = "sqlite"): Director.insert(*[Director(**d) for d in DIRECTORS]).run_sync() # type: ignore # noqa: E501 Movie.insert(*[Movie(**m) for m in MOVIES]).run_sync() # type: ignore Studio.insert(*[Studio(**s) for s in STUDIOS]).run_sync() # type: ignore + Review.insert(*[Review(**r) for r in REVIEWS]).run_sync() # type: ignore if engine == "postgres": # We need to update the sequence, as we explicitly set the IDs for the diff --git a/piccolo_admin/example_data.py b/piccolo_admin/example_data.py index 641392a7..ccb49359 100644 --- a/piccolo_admin/example_data.py +++ b/piccolo_admin/example_data.py @@ -341,6 +341,27 @@ } ] +REVIEWS = [ + { + "reviewer": "John McCloud", + "content": "Great film - loved the ending!", + "rating": 5, + "movie": MOVIES[0]['name'] + }, + { + "reviewer": "Penelope Jones", + "content": "The special effects blew me away.", + "rating": 4, + "movie": MOVIES[1]['name'] + }, + { + "reviewer": "Arnold Adams", + "content": "It was good. I enjoyed the plot.", + "rating": 3, + "movie": MOVIES[2]['name'] + }, +] + # Some random words used to generate fake movie names. MOVIE_WORDS = [ From e1c1830db80a9cce8e675eaa19bc936a3c918123 Mon Sep 17 00:00:00 2001 From: Daniel Townsend Date: Sun, 16 Jan 2022 21:17:57 +0000 Subject: [PATCH 2/3] fix tests --- tests/test_endpoints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index b6951f92..72fa9b5c 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -347,7 +347,7 @@ def test_tables(self): self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), - ["movie", "director", "studio"], + ["movie", "director", "studio", "review"], ) def test_get_user(self): From a5146be7f90f711217feac5b703801f1c5970bc7 Mon Sep 17 00:00:00 2001 From: Daniel Townsend Date: Thu, 20 Jan 2022 14:48:44 +0000 Subject: [PATCH 3/3] make links work from row listing --- admin_ui/src/components/RowFormSelect.vue | 2 +- admin_ui/src/views/RowListing.vue | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/admin_ui/src/components/RowFormSelect.vue b/admin_ui/src/components/RowFormSelect.vue index bcc26ca8..4e6dbbec 100644 --- a/admin_ui/src/components/RowFormSelect.vue +++ b/admin_ui/src/components/RowFormSelect.vue @@ -35,7 +35,7 @@ }" class="add" target="_blank" - v-if="!isFilter" + v-if="!isFilter && getKeySelectID(property.title)" > diff --git a/admin_ui/src/views/RowListing.vue b/admin_ui/src/views/RowListing.vue index 17aaac71..90430ef8 100644 --- a/admin_ui/src/views/RowListing.vue +++ b/admin_ui/src/views/RowListing.vue @@ -114,7 +114,7 @@ {{ row[name + "_readable"] }}