Skip to content

Commit ef6d3e2

Browse files
committed
WIP
1 parent 5f8173a commit ef6d3e2

12 files changed

Lines changed: 263 additions & 4 deletions

File tree

metrics/api/settings/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
match config.APIENV:
44
case "LOCAL":
55
from .local import *
6+
case "TEST":
7+
from .test import *
68
case "STANDALONE":
79
from .standalone import *
810

metrics/api/settings/test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
from metrics.api.settings import ROOT_LEVEL_BASE_DIR
4+
5+
DATA_UPLOAD_MAX_NUMBER_FIELDS = None
6+
7+
DEBUG = True
8+
9+
DATABASES = {
10+
"default": {
11+
"TIME_ZONE": "Europe/London",
12+
"ENGINE": "django.db.backends.sqlite3",
13+
"NAME": os.path.join(ROOT_LEVEL_BASE_DIR, "test.sqlite3"),
14+
},
15+
"test": {
16+
"TIME_ZONE": "Europe/London",
17+
"ENGINE": "django.db.backends.sqlite3",
18+
"NAME": os.path.join(ROOT_LEVEL_BASE_DIR, "test.sqlite3"),
19+
},
20+
}
21+
22+
INTERNAL_IPS = ["127.0.0.1"]

metrics/api/urls_construction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def construct_cms_admin_urlpatterns(
9898
]
9999

100100

101-
DEFAULT_PUBLIC_API_PREFIX = "api/public/timeseries/"
101+
DEFAULT_PUBLIC_API_PREFIX = "api/public/"
102102

103103

104104
def construct_public_api_urlpatterns(

public_api/urls.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
GeographyTypeListViewV2,
1010
MetricListViewV2,
1111
PublicAPIRootViewV2,
12+
SearchView,
1213
SubThemeDetailViewV2,
1314
SubThemeListViewV2,
1415
ThemeDetailViewV2,
@@ -33,6 +34,9 @@
3334
)
3435
from public_api.views.timeseries_viewset import APITimeSeriesViewSet
3536

37+
TIMESERIES_PREFIX = "timeseries/"
38+
SEARCH_PREFIX = "search/"
39+
3640

3741
def construct_url_patterns_for_public_api(
3842
*,
@@ -48,8 +52,12 @@ def construct_url_patterns_for_public_api(
4852
set of versioned URLS.
4953
"""
5054
urls = []
51-
urls.extend(_construct_version_one_urls(prefix=prefix))
52-
urls.extend(_construct_version_two_urls(prefix=prefix))
55+
# Timeseries API
56+
urls.extend(_construct_version_one_urls(prefix=prefix + TIMESERIES_PREFIX))
57+
urls.extend(_construct_version_two_urls(prefix=prefix + TIMESERIES_PREFIX))
58+
59+
# Search API
60+
urls.extend(_construct_search_urls(prefix=prefix + SEARCH_PREFIX))
5361

5462
if MetricsPublicAPIInterface.is_auth_enabled():
5563
urls.append(
@@ -222,3 +230,25 @@ def _construct_version_two_urls(
222230
name="timeseries-list-v2",
223231
),
224232
]
233+
234+
235+
def _construct_search_urls(
236+
*,
237+
prefix: str,
238+
) -> list[resolvers.URLResolver]:
239+
"""Returns a list of URLResolvers for the public search API
240+
241+
Args:
242+
prefix: The prefix to add to the start of the url paths
243+
244+
Returns:
245+
List of `URLResolver` objects each representing a
246+
set of versioned URLS.
247+
"""
248+
return [
249+
path(
250+
f"{prefix}v1",
251+
SearchView.as_view(),
252+
name="search",
253+
),
254+
]

public_api/version_02/views/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
)
1414

1515
from .root_view import PublicAPIRootViewV2
16+
17+
from .search import SearchView
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from drf_spectacular.utils import extend_schema
2+
from rest_framework.request import Request
3+
from rest_framework.response import Response
4+
from rest_framework.views import APIView
5+
from wagtail.api.v2.serializers import get_serializer_class
6+
from wagtail.models import Page
7+
8+
from cms.topic.models import TopicPage
9+
from public_api.version_02.views.base import PUBLIC_API_TAG
10+
11+
12+
@extend_schema(tags=[PUBLIC_API_TAG])
13+
class SearchView(APIView):
14+
"""This endpoint provides search results and could in the future provide
15+
autocomplete suggestions etc.
16+
17+
"""
18+
19+
def get(self, request: Request):
20+
search = request.GET.get("search")
21+
limit = int(request.GET.get("limit", 0))
22+
fields = request.GET.get("fields", ["title", "slug"])
23+
meta = request.GET.get("meta", ["id"])
24+
topic_results = TopicPage.objects.all().search(search)
25+
if not limit or topic_results.count() < limit:
26+
# TODO: go get more
27+
# results = queryset
28+
results = topic_results
29+
else:
30+
results = topic_results[0:limit]
31+
print(f"AIDAN: returning {results}")
32+
serialized = get_serializer_class(Page, fields, meta)(results, many=True)
33+
return Response(serialized.data)

scripts/_cache.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function _cache_help() {
99
echo
1010
echo " flush-redis - flush and re-fill the redis (private api) cache"
1111
echo " flush-redis-reserved-namespace - blue-green update the reserved namespace in the redis (private api) cache"
12+
echo " flush-search - flush and re-build the wagtail search index"
1213

1314
return 0
1415
}
@@ -20,6 +21,7 @@ function _cache() {
2021
case $verb in
2122
"flush-redis") _cache_flush_redis $args ;;
2223
"flush-redis-reserved-namespace") _cache_flush_redis_reserved_namespace $args ;;
24+
"flush-search") _flush_search $args ;;
2325

2426
*) _cache_help ;;
2527
esac
@@ -34,3 +36,8 @@ function _cache_flush_redis_reserved_namespace() {
3436
uhd venv activate
3537
python manage.py hydrate_private_api_cache_reserved_namespace
3638
}
39+
40+
function _flush_search() {
41+
uhd venv activate
42+
python manage.py wagtail_update_index
43+
}

scripts/_tests.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ function _tests_unit() {
4141

4242
function _tests_integration() {
4343
uhd venv activate
44-
python -m pytest tests/integration "$@"
44+
rm -f test.sqlite3
45+
uhd django migrate
46+
uhd bootstrap all
47+
pytest tests/integration "$@"
4548
}
4649

4750
function _tests_system() {

tests/factories/cms/page.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from cms.common.models import CommonPage
4+
5+
6+
class CommonPageFactory(factory.django.DjangoModelFactory):
7+
"""
8+
Factory for creating `CommonPage` instances for tests
9+
"""
10+
11+
class Meta:
12+
model = CommonPage

tests/factories/cms/topic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from cms.topic.models import TopicPage
4+
5+
6+
class TopicPageFactory(factory.django.DjangoModelFactory):
7+
"""
8+
Factory for creating `Topic` instances for tests
9+
"""
10+
11+
class Meta:
12+
model = TopicPage

0 commit comments

Comments
 (0)