Alberto Donato has proposed merging ~ack/maas-site-manager:accepted-sites into 
maas-site-manager:main.

Commit message:
add accepted and created fields to Site, only return accepted ones in /sites



Requested reviews:
  MAAS Committers (maas-committers)

For more details, see:
https://code.launchpad.net/~ack/maas-site-manager/+git/site-manager/+merge/442289
-- 
Your team MAAS Committers is requested to review the proposed merge of 
~ack/maas-site-manager:accepted-sites into maas-site-manager:main.
diff --git a/backend/msm/db/_tables.py b/backend/msm/db/_tables.py
index 41bc816..70affca 100644
--- a/backend/msm/db/_tables.py
+++ b/backend/msm/db/_tables.py
@@ -1,6 +1,8 @@
+from datetime import datetime
 from uuid import uuid4
 
 from sqlalchemy import (
+    Boolean,
     Column,
     ForeignKey,
     Integer,
@@ -27,6 +29,8 @@ Site = Table(
     Column("street", Text),
     Column("timezone", Text),
     Column("url", Text),
+    Column("accepted", Boolean, nullable=False, index=True, default=False),
+    Column("created", DateTime, nullable=False, default=datetime.utcnow),
 )
 
 
@@ -49,7 +53,7 @@ Token = Table(
         "value", UUID(as_uuid=True), nullable=False, index=True, default=uuid4
     ),
     Column("expired", DateTime, nullable=False),
-    Column("created", DateTime, nullable=False),
+    Column("created", DateTime, nullable=False, default=datetime.utcnow),
 )
 
 
diff --git a/backend/msm/db/queries.py b/backend/msm/db/queries.py
index e0d0cff..ff5df47 100644
--- a/backend/msm/db/queries.py
+++ b/backend/msm/db/queries.py
@@ -107,6 +107,7 @@ async def get_filtered_sites(
     street: list[str] | None = None,
     timezone: list[str] | None = None,
     url: list[str] | None = None,
+    accepted: bool | None = None,
 ) -> tuple[int, Iterable[SiteSchema]]:
     filters = filters_from_arguments(
         Site,
@@ -119,6 +120,8 @@ async def get_filtered_sites(
         timezone=timezone,
         url=url,
     )
+    if accepted is not None:
+        filters.append(Site.c.accepted == accepted)
     count = (
         await session.execute(
             select(func.count())
diff --git a/backend/msm/user_api/_base.py b/backend/msm/user_api/_base.py
index 48fb1b0..1f4d1f1 100644
--- a/backend/msm/user_api/_base.py
+++ b/backend/msm/user_api/_base.py
@@ -52,6 +52,7 @@ async def sites(
         session,
         offset=pagination_params.offset,
         limit=pagination_params.size,
+        accepted=True,  # only list accepted sites here
         **filter_params._asdict(),
     )
     return PaginatedSites(
diff --git a/backend/tests/user_api/test_handlers.py b/backend/tests/user_api/test_handlers.py
index 1c9c121..8a5d97c 100644
--- a/backend/tests/user_api/test_handlers.py
+++ b/backend/tests/user_api/test_handlers.py
@@ -62,6 +62,7 @@ async def test_list_sites(
         "street": "110 Southwark St",
         "timezone": "Europe/London",
         "url": "https://londoncalling.example.com";,
+        "accepted": True,
     }
     site2 = site1.copy()
     site2.update(
@@ -75,6 +76,9 @@ async def test_list_sites(
     sites = await fixture.create("site", [site1, site2])
     for site in sites:
         site["stats"] = None
+        del site["created"]
+        del site["accepted"]
+
     page1 = await authenticated_user_app_client.get("/sites")
     assert page1.status_code == 200
     assert page1.json() == {
@@ -104,6 +108,48 @@ async def test_list_sites(
 
 
 @pytest.mark.asyncio
+async def test_list_sites_only_accepted(
+    authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
+) -> None:
+    site1 = {
+        "name": "LondonHQ",
+        "city": "London",
+        "country": "gb",
+        "latitude": "51.509865",
+        "longitude": "-0.118092",
+        "note": "the first site",
+        "region": "Blue Fin Bldg",
+        "street": "110 Southwark St",
+        "timezone": "Europe/London",
+        "url": "https://londoncalling.example.com";,
+        "accepted": True,
+    }
+    site2 = site1.copy()
+    site2.update(
+        {
+            "name": "BerlinHQ",
+            "timezone": "Europe/Berlin",
+            "city": "Berlin",
+            "country": "de",
+            "accepted": False,
+        }
+    )
+    created_site, _ = await fixture.create("site", [site1, site2])
+    created_site["stats"] = None
+    del created_site["created"]
+    del created_site["accepted"]
+
+    page1 = await authenticated_user_app_client.get("/sites")
+    assert page1.status_code == 200
+    assert page1.json() == {
+        "page": 1,
+        "size": 20,
+        "total": 1,
+        "items": [created_site],
+    }
+
+
+@pytest.mark.asyncio
 async def test_list_sites_filter_timezone(
     authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
 ) -> None:
@@ -118,6 +164,7 @@ async def test_list_sites_filter_timezone(
         "street": "110 Southwark St",
         "timezone": "Europe/London",
         "url": "https://londoncalling.example.com";,
+        "accepted": True,
     }
     site2 = site1.copy()
     site2.update(
@@ -130,6 +177,8 @@ async def test_list_sites_filter_timezone(
     )
     [created_site, _] = await fixture.create("site", [site1, site2])
     created_site["stats"] = None
+    del created_site["created"]
+    del created_site["accepted"]
     page1 = await authenticated_user_app_client.get(
         "/sites?timezone=Europe/London"
     )
@@ -160,6 +209,7 @@ async def test_list_sites_with_stats(
                 "street": "110 Southwark St",
                 "timezone": "Europe/London",
                 "url": "https://londoncalling.example.com";,
+                "accepted": True,
             }
         ],
     )
@@ -180,6 +230,8 @@ async def test_list_sites_with_stats(
     del site_data["site_id"]
     site_data["last_seen"] = site_data["last_seen"].isoformat()
     site["stats"] = site_data
+    del site["created"]
+    del site["accepted"]
 
     page = await authenticated_user_app_client.get("/sites")
     assert page.status_code == 200
@@ -291,6 +343,7 @@ async def test_sites_fails_without_login(
             "street": "110 Southwark St",
             "timezone": "Europe/London",
             "url": "https://londoncalling.example.com";,
+            "accepted": True,
         }
     ]
     await fixture.create("site", site)
diff --git a/test-data/import.sh b/test-data/import.sh
index 8adf22f..da238be 100755
--- a/test-data/import.sh
+++ b/test-data/import.sh
@@ -35,8 +35,8 @@ EOF
 fi
 
 (
-    copy_cmd sites.csv 'site(id, city, country, latitude, longitude, name, note, region, street, timezone, url)'
+    copy_cmd sites.csv 'site(id, city, country, latitude, longitude, name, note, region, street, timezone, url, accepted, created)'
     copy_cmd tokens.csv 'token(site_id, value, expired, created)'
     copy_cmd users.csv '"user"(email, full_name, password)'
     copy_cmd site_data.csv 'site_data(site_id, allocated_machines, deployed_machines, ready_machines, error_machines, last_seen)'
-) | psql "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
+) | psql --single-transaction "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
diff --git a/test-data/sites.csv b/test-data/sites.csv
index 9798eba..b25a675 100644
--- a/test-data/sites.csv
+++ b/test-data/sites.csv
@@ -1,9 +1,9 @@
-id,city, country, latitude, longitude, name, note, region, street, timezone, url
-1,London,GB,51.501990,-0.092200,Canonical Group Limited,4th Floor,,201 Borough High Street,Europe/London,https://london.canonical.example.com
-2,Austin,US,30.269612,-97.741057,Canonical USA Inc.,Perry Brooks Building - Suite 300,,720 Brazos Street,America/Chicago,https://austin.canonical.example.com
-3,Boston,US,42.358859,-71.059615,Canonical USA Inc. 001,Suite 210,,18 Tremont Street,America/Chicago,https://boston.canonical.example.com
-4,Shanghai,CN,31.187270,121.436829,Canonical China,上海市漕溪北路331号12楼1246室,,No. 331 North Caoxi Road,Asia/Shanghai,https://shanghai.canonical.example.com
-5,Beijing,CN,39.908447,116.448690,Canonical China 001,China World Office 1; 北京市朝阳区建国门外大街1号国贸写字楼1座11层1118-19室 100004,Chaoyang District,1 Jianguomenwai Avenue,Asia/Shanghai,https://shanghai.canonical.example.com
-6,Taipei City,TW,25.058098,121.543406,Canonical Group Limited - Taiwan Branch,105402 台北市松山區民生東路三段100號12樓,Songshan Dist.,"12F.,No. 100,Sec. 3,Minsheng E. Rd.",Asia/Taipei,https://taiwan.canonical.example.com
-7,Douglas,IM,54.153072,-4.481012,Canonical Limited,2nd Floor - Clarendon House,,Victoria Street,Europe/London,https://canonical.example.com
-8,Tokyo,JP,35.673242,139.740669,Canonical Japan K.K,3rd Floor - Sanno Park Tower,,2-11-1 Nagata-cho Chiyoda-ku,Japan,https://japan.canonical.example.com
+id,city,country,latitude,longitude,name,note,region,street,timezone,url,accepted,created
+1,London,GB,51.501990,-0.092200,Canonical Group Limited,4th Floor,,201 Borough High Street,Europe/London,https://london.canonical.example.com,t,2023-04-19 14:01:40.107611
+2,Austin,US,30.269612,-97.741057,Canonical USA Inc.,Perry Brooks Building - Suite 300,,720 Brazos Street,America/Chicago,https://austin.canonical.example.com,t,2023-04-28 14:01:34.229025
+3,Boston,US,42.358859,-71.059615,Canonical USA Inc. 001,Suite 210,,18 Tremont Street,America/Chicago,https://boston.canonical.example.com,t,2023-05-04 14:01:49.706886
+4,Shanghai,CN,31.187270,121.436829,Canonical China,上海市漕溪北路331号12楼1246室,,No. 331 North Caoxi Road,Asia/Shanghai,https://shanghai.canonical.example.com,f,2023-04-03 14:01:27.419476
+5,Beijing,CN,39.908447,116.448690,Canonical China 001,China World Office 1; 北京市朝阳区建国门外大街1号国贸写字楼1座11层1118-19室 100004,Chaoyang District,1 Jianguomenwai Avenue,Asia/Shanghai,https://shanghai.canonical.example.com,t,2023-05-03 15:02:11.107339
+6,Taipei City,TW,25.058098,121.543406,Canonical Group Limited - Taiwan Branch,105402 台北市松山區民生東路三段100號12樓,Songshan Dist.,"12F.,No. 100,Sec. 3,Minsheng E. Rd.",Asia/Taipei,https://taiwan.canonical.example.com,t,2023-05-04 14:02:24.156508
+7,Douglas,IM,54.153072,-4.481012,Canonical Limited,2nd Floor - Clarendon House,,Victoria Street,Europe/London,https://canonical.example.com,f,2023-04-13 14:02:31.611254
+8,Tokyo,JP,35.673242,139.740669,Canonical Japan K.K,3rd Floor - Sanno Park Tower,,2-11-1 Nagata-cho Chiyoda-ku,Japan,https://japan.canonical.example.com,t,2022-05-03 14:02:42.955134
-- 
Mailing list: https://launchpad.net/~sts-sponsors
Post to     : sts-sponsors@lists.launchpad.net
Unsubscribe : https://launchpad.net/~sts-sponsors
More help   : https://help.launchpad.net/ListHelp

Reply via email to