mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-25 00:34:32 +00:00
Allow spots and alerts to have multiple activities #143
This commit is contained in:
@@ -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.activity
|
||||
and spot.activity_refs
|
||||
and len(spot.activity_refs) > 0
|
||||
and spot.activity_refs[0].id
|
||||
and get_ref_regex_for_activity(spot.activity)
|
||||
and not re.match(get_ref_regex_for_activity(spot.activity), spot.activity_refs[0].id)
|
||||
):
|
||||
self.set_status(422)
|
||||
self.write(
|
||||
safe_json_dumps(
|
||||
f"Error - '{spot.activity_refs[0].id}' does not look like a valid reference for {spot.activity}."
|
||||
# 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,7 +166,8 @@ class APISpotHandler(tornado.web.RequestHandler):
|
||||
|
||||
# Validate upstream submission requirements
|
||||
if submit_upstream and upstream_provider_name:
|
||||
if not spot.activity:
|
||||
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")
|
||||
@@ -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.activity)
|
||||
provider = self._find_provider(upstream_provider_name, spot.activities)
|
||||
if provider:
|
||||
try:
|
||||
# Submit spot to the upstream provider
|
||||
@@ -216,7 +212,7 @@ 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.activity if spot.activity 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
|
||||
@@ -242,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
|
||||
|
||||
|
||||
@@ -169,13 +169,13 @@ def alert_allowed_by_query(alert, query):
|
||||
# the alert is a dxpedition, or contests_skip_max_duration_check and the alert is a contest, it also
|
||||
# always passes the check.
|
||||
if (
|
||||
alert.activity == ActivityName.DXPEDITION
|
||||
ActivityName.DXPEDITION in alert.activities
|
||||
and "dxpeditions_skip_max_duration_check" in query
|
||||
and query.get("dxpeditions_skip_max_duration_check").upper() == "TRUE"
|
||||
):
|
||||
continue
|
||||
if (
|
||||
alert.activity == ActivityName.CONTEST
|
||||
ActivityName.CONTEST in alert.activities
|
||||
and "contests_skip_max_duration_check" in query
|
||||
and query.get("contests_skip_max_duration_check").upper() == "TRUE"
|
||||
):
|
||||
@@ -187,13 +187,13 @@ def alert_allowed_by_query(alert, query):
|
||||
if not alert.source or alert.source not in sources:
|
||||
return False
|
||||
case "activity":
|
||||
# If a list of activities is provided, the alert must have an activity and it must match one of them.
|
||||
# The special activity "NO_ACTIVITY", when supplied in the list, matches alerts with no activity.
|
||||
# If a list of activities is provided, the alert must have at least one activity that matches one of
|
||||
# them. The special activity "NO_ACTIVITY", when supplied in the list, matches alerts with no activity.
|
||||
activities = query.get(k).split(",")
|
||||
include_no_activity = "NO_ACTIVITY" in activities
|
||||
if not alert.activity and not include_no_activity:
|
||||
if not alert.activities and not include_no_activity:
|
||||
return False
|
||||
if alert.activity and alert.activity not in activities:
|
||||
if alert.activities and not any(a in activities for a in alert.activities):
|
||||
return False
|
||||
case "dx_continent":
|
||||
dxconts = query.get(k).split(",")
|
||||
|
||||
@@ -24,7 +24,7 @@ _V2_TO_V3_QUERY_PARAMS = {
|
||||
# Values of query parameters renamed in v3
|
||||
_V2_TO_V3_QUERY_VALUES = {
|
||||
"activity": {"NO_SIG": "NO_ACTIVITY"},
|
||||
"fields": {"sig": "activity", "sig_refs": "activity_refs"},
|
||||
"fields": {"sig": "activities", "sig_refs": "activity_refs"},
|
||||
}
|
||||
# Keys of JSON objects in API responses renamed in v3
|
||||
_V3_TO_V2_RESPONSE_KEYS = {
|
||||
@@ -54,19 +54,28 @@ class V2CompatibilityWrapper(CompatibilityWrapper):
|
||||
return rename_keys_and_values(obj, _V3_TO_V2_RESPONSE_KEYS, _V3_TO_V2_RESPONSE_VALUES)
|
||||
|
||||
|
||||
class V2APISpotsHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APISpotsHandler):
|
||||
class V2SpotsAlertsCompatibilityWrapper(V2CompatibilityWrapper):
|
||||
"""Extra translation for spots and alerts. In v3 these have a list of "activities" rather than a single activity,
|
||||
so for v2 we collapse this back down to a single value using the first activity in the list. This must happen
|
||||
before the generic key renaming, which would otherwise rename "activities" to "sigs" as it does for /options."""
|
||||
|
||||
def translate_response_object(self, obj):
|
||||
return super().translate_response_object(collapse_activities(obj))
|
||||
|
||||
|
||||
class V2APISpotsHandler(V2SpotsAlertsCompatibilityWrapper, RequestCompatibilityWrapper, APISpotsHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
class V2APISpotsStreamHandler(V2CompatibilityWrapper, StreamCompatibilityWrapper, APISpotsStreamHandler):
|
||||
class V2APISpotsStreamHandler(V2SpotsAlertsCompatibilityWrapper, StreamCompatibilityWrapper, APISpotsStreamHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
class V2APIAlertsHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APIAlertsHandler):
|
||||
class V2APIAlertsHandler(V2SpotsAlertsCompatibilityWrapper, RequestCompatibilityWrapper, APIAlertsHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
class V2APIAlertsStreamHandler(V2CompatibilityWrapper, StreamCompatibilityWrapper, APIAlertsStreamHandler):
|
||||
class V2APIAlertsStreamHandler(V2SpotsAlertsCompatibilityWrapper, StreamCompatibilityWrapper, APIAlertsStreamHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
@@ -118,7 +127,8 @@ class V2APISpotHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APIS
|
||||
|
||||
spot_data = dict(spot_data)
|
||||
if "sig" in spot_data:
|
||||
spot_data["activity"] = spot_data.pop("sig")
|
||||
sig = spot_data.pop("sig")
|
||||
spot_data["activities"] = [sig] if sig else []
|
||||
if "sig_refs" in spot_data:
|
||||
spot_data["activity_refs"] = spot_data.pop("sig_refs")
|
||||
if isinstance(spot_data.get("activity_refs"), list):
|
||||
@@ -130,3 +140,18 @@ class V2APISpotHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APIS
|
||||
refs.append(ref)
|
||||
spot_data["activity_refs"] = refs
|
||||
return spot_data
|
||||
|
||||
|
||||
def collapse_activities(obj):
|
||||
"""Utility method to replace the "activities" list in a spot or alert JSON object with a single "activity" value,
|
||||
being the first activity in the list, or None if there are none. The object can be a single spot/alert dict, or a
|
||||
list of them. Anything else is returned untouched. Used to translate v3's list of activities to the single sig
|
||||
expected in v2 API calls."""
|
||||
|
||||
if isinstance(obj, list):
|
||||
return [collapse_activities(i) for i in obj]
|
||||
if isinstance(obj, dict) and "activities" in obj:
|
||||
obj = dict(obj)
|
||||
activities = obj.pop("activities")
|
||||
obj["activity"] = activities[0] if activities else None
|
||||
return obj
|
||||
@@ -197,19 +197,19 @@ def spot_allowed_by_query(spot, query):
|
||||
if not spot.source or spot.source not in sources:
|
||||
return False
|
||||
case "activity":
|
||||
# If a list of activities is provided, the spot must have an activity and it must match one of them.
|
||||
# The special activity "NO_ACTIVITY", when supplied in the list, matches spots with no activity.
|
||||
# If a list of activities is provided, the spot must have at least one activity that matches one of
|
||||
# them. The special activity "NO_ACTIVITY", when supplied in the list, matches spots with no activity.
|
||||
activities = query.get(k).split(",")
|
||||
include_no_activity = "NO_ACTIVITY" in activities
|
||||
if not spot.activity and not include_no_activity:
|
||||
if not spot.activities and not include_no_activity:
|
||||
return False
|
||||
if spot.activity and spot.activity not in activities:
|
||||
if spot.activities and not any(a in activities for a in spot.activities):
|
||||
return False
|
||||
case "needs_activity":
|
||||
# If true, an activity is required, regardless of what it is, it just can't be missing. Mutually
|
||||
# exclusive with supplying the special "NO_ACTIVITY" parameter to the "activity" query param.
|
||||
needs_activity = query.get(k).upper() == "TRUE"
|
||||
if needs_activity and not spot.activity:
|
||||
if needs_activity and not spot.activities:
|
||||
return False
|
||||
case "needs_activity_ref":
|
||||
# If true, at least one activity ref is required, regardless of what it is, it just can't be missing.
|
||||
|
||||
Reference in New Issue
Block a user