mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-25 08:44:33 +00:00
sig->activity and multiple activity changes for API v3. #143
This commit is contained in:
@@ -21,7 +21,7 @@ RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
|
||||
|
||||
|
||||
class APISpotHandler(tornado.web.RequestHandler):
|
||||
"""API request handler for /api/v2/spot (POST)"""
|
||||
"""API request handler for /api/v3/spot (POST)"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -142,24 +142,19 @@ class APISpotHandler(tornado.web.RequestHandler):
|
||||
self.set_header("Content-Type", "application/json")
|
||||
return
|
||||
|
||||
# Reject if activity ref format incorrect for activity
|
||||
if (
|
||||
spot.sig
|
||||
and spot.sig_refs
|
||||
and len(spot.sig_refs) > 0
|
||||
and spot.sig_refs[0].id
|
||||
and get_ref_regex_for_activity(spot.sig)
|
||||
and not re.match(get_ref_regex_for_activity(spot.sig), spot.sig_refs[0].id)
|
||||
):
|
||||
self.set_status(422)
|
||||
self.write(
|
||||
safe_json_dumps(
|
||||
f"Error - '{spot.sig_refs[0].id}' does not look like a valid reference for {spot.sig}."
|
||||
# Reject if any activity ref format is incorrect for its activity
|
||||
for activity_ref in spot.activity_refs:
|
||||
ref_regex = get_ref_regex_for_activity(activity_ref.activity) if activity_ref.activity else None
|
||||
if activity_ref.id and ref_regex and not re.match(ref_regex, activity_ref.id):
|
||||
self.set_status(422)
|
||||
self.write(
|
||||
safe_json_dumps(
|
||||
f"Error - '{activity_ref.id}' does not look like a valid reference for {activity_ref.activity}."
|
||||
)
|
||||
)
|
||||
)
|
||||
self.set_header("Cache-Control", "no-store")
|
||||
self.set_header("Content-Type", "application/json")
|
||||
return
|
||||
self.set_header("Cache-Control", "no-store")
|
||||
self.set_header("Content-Type", "application/json")
|
||||
return
|
||||
|
||||
# Reject upstream submission if not permitted
|
||||
if submit_upstream and not ALLOW_UPSTREAM_SPOTTING:
|
||||
@@ -171,13 +166,14 @@ class APISpotHandler(tornado.web.RequestHandler):
|
||||
|
||||
# Validate upstream submission requirements
|
||||
if submit_upstream and upstream_provider_name:
|
||||
if not spot.sig:
|
||||
if not spot.activities:
|
||||
# TODO when we allow spotting to cluster upstream, we need to remove this restriction
|
||||
self.set_status(422)
|
||||
self.write(safe_json_dumps("Error - an activity must be selected to submit upstream."))
|
||||
self.set_header("Cache-Control", "no-store")
|
||||
self.set_header("Content-Type", "application/json")
|
||||
return
|
||||
if not spot.sig_refs and upstream_provider_name != "Tiles":
|
||||
if not spot.activity_refs and upstream_provider_name != "Tiles":
|
||||
self.set_status(422)
|
||||
self.write(safe_json_dumps("Error - an activity reference is required to submit upstream."))
|
||||
self.set_header("Cache-Control", "no-store")
|
||||
@@ -201,7 +197,7 @@ class APISpotHandler(tornado.web.RequestHandler):
|
||||
# Submit upstream if requested
|
||||
upstream_warning = None
|
||||
if submit_upstream and upstream_provider_name:
|
||||
provider = self._find_provider(upstream_provider_name, spot.sig)
|
||||
provider = self._find_provider(upstream_provider_name, spot.activities)
|
||||
if provider:
|
||||
try:
|
||||
# Submit spot to the upstream provider
|
||||
@@ -216,12 +212,13 @@ class APISpotHandler(tornado.web.RequestHandler):
|
||||
f"Spot was saved locally but upstream submission to {upstream_provider_name} failed."
|
||||
)
|
||||
else:
|
||||
upstream_warning = f"No enabled provider named '{upstream_provider_name}' supports upstream submission for {spot.sig if spot.sig else ''} spots."
|
||||
upstream_warning = f"No enabled provider named '{upstream_provider_name}' supports upstream submission for {', '.join(spot.activities)} spots."
|
||||
|
||||
# If we successfully submitted the spot upstream, don't add it direct to Spothole, otherwise it will be a
|
||||
# duplicate with what immediately comes back from the API. But if we weren't asked to send it upstream, or
|
||||
# we were but it failed, we should still add it to our database anyway.
|
||||
if not submit_upstream or upstream_warning:
|
||||
spot.source = "API"
|
||||
spot.infer_missing()
|
||||
self._spots.set(spot.id, spot)
|
||||
|
||||
@@ -241,11 +238,11 @@ class APISpotHandler(tornado.web.RequestHandler):
|
||||
self.set_header("Cache-Control", "no-store")
|
||||
self.set_header("Content-Type", "application/json")
|
||||
|
||||
def _find_provider(self, provider_name, activity) -> SpotProvider | None:
|
||||
"""Find an enabled provider by name that can submit spots for the given activity."""
|
||||
def _find_provider(self, provider_name, activities) -> SpotProvider | None:
|
||||
"""Find an enabled provider by name that can submit spots for at least one of the given activities."""
|
||||
|
||||
for p in self._spot_providers:
|
||||
if p.enabled and p.name == provider_name and p.can_submit_spot(activity):
|
||||
if p.enabled and p.name == provider_name and any(p.can_submit_spot(a) for a in activities):
|
||||
return p
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user