Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
/api/exportVaccinateTheState, refs #558
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed May 12, 2021
1 parent e28a4ef commit edb24c4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vaccinate/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def replacement_view_function(request, *args, **kwargs):
finally:
if response is None:
response = HttpResponseServerError()
if getattr(request, "skip_api_logging", False):
return response
# Create the log record
post_body = None
post_body_json = None
Expand Down Expand Up @@ -258,6 +260,8 @@ def wrapper(
) -> Callable[..., HttpResponseBase]:
@wraps(view_fn)
def inner(request: HttpRequest, *args, **kwargs: Any) -> HttpResponseBase:
if getattr(request, "skip_jwt_auth", False):
return view_fn(request, *args, **kwargs)
# Two other kinds of auth to possibly check
if allow_session_auth and request.user.is_authenticated:
return view_fn(request, *args, **kwargs)
Expand Down
16 changes: 16 additions & 0 deletions vaccinate/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,22 @@ def api_export(request):
return JsonResponse({"ok": 1})


@csrf_exempt
@beeline.traced(name="api_export_vaccinate_the_states")
def api_export_vaccinate_the_states(request):
if request.method != "POST":
return JsonResponse(
{"error": "Must be a POST"},
status=400,
)
if not exporter.api_export_vaccinate_the_states():
return JsonResponse(
{"error": "Failed to export; check Sentry"},
status=500,
)
return JsonResponse({"ok": 1})


def api_export_preview_locations(request):
# Show a preview of the export API for a subset of locations
location_ids = request.GET.getlist("id")
Expand Down
1 change: 1 addition & 0 deletions vaccinate/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
path("api/taskTypes", api_views.task_types),
path("api/availabilityTags", api_views.availability_tags),
path("api/export", api_views.api_export),
path("api/exportVaccinateTheStates", api_views.api_export_vaccinate_the_states),
path("api/exportPreview/Locations.json", api_views.api_export_preview_locations),
path("api/exportPreview/Providers.json", api_views.api_export_preview_providers),
path("api/exportMapbox", export_mapbox_views.export_mapbox),
Expand Down
29 changes: 29 additions & 0 deletions vaccinate/core/exporter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from typing import Callable, Dict, Generator, Iterator, List, Optional

import beeline
from django.contrib.auth.models import AnonymousUser
from core import models
from core.exporter.storage import GoogleStorageWriter, LocalWriter, StorageWriter
from api.search import search_locations
from django.db import transaction
from django.db.models import Count, F, Q, QuerySet
from django.test.client import RequestFactory
from sentry_sdk import capture_exception

DEPLOYS: Dict[str, List[StorageWriter]] = {
Expand All @@ -30,6 +33,32 @@
}


VTS_DEPLOYS: Dict[str, StorageWriter] = {
"testing": LocalWriter("local/api/vaccinatethestates"),
"staging": GoogleStorageWriter("vaccinateca-api-staging", "v0-vts"),
"production": GoogleStorageWriter("vaccinateca-api", "v0-vts"),
}


def api_export_vaccinate_the_states() -> bool:
request = RequestFactory().get("/api/searchLocations?all=1&format=v0preview")
request.user = AnonymousUser()
request.skip_jwt_auth = True # type: ignore[attr-defined]
request.skip_api_logging = True # type: ignore[attr-defined]
response = search_locations(request)
writer = VTS_DEPLOYS[os.environ.get("DEPLOY", "testing")]
ok = True
try:
writer.write(
"locations.json",
(chunk.decode("utf-8") for chunk in response.streaming_content),
)
except Exception as e:
capture_exception(e)
ok = False
return ok


def api_export() -> bool:
deploy_env = DEPLOYS[os.environ.get("DEPLOY", "testing")]
ok = True
Expand Down
5 changes: 5 additions & 0 deletions vaccinate/core/test_api_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,8 @@ def test_api_serialization():
with dataset() as ds:
api(1, ds).write(writer)
api(0, ds).write(writer)


@pytest.mark.django_db
def test_api_export_vaccinate_the_states(client, ten_locations):
client.post("/api/exportVaccinateTheStates")

0 comments on commit edb24c4

Please sign in to comment.