Allow adding spots #2

This commit is contained in:
Ian Renton
2025-10-04 13:03:52 +01:00
parent f75f79a108
commit 0419aab83f
2 changed files with 30 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import json
import logging
from datetime import datetime, timedelta
from threading import Thread
from types import SimpleNamespace
import bottle
import pytz
@@ -10,6 +11,7 @@ from bottle import run, response, template
from core.config import MAX_SPOT_AGE
from core.constants import BANDS, ALL_MODES, MODE_TYPES, SIGS, CONTINENTS
from core.utils import serialize_everything
from data.spot import Spot
# Provides the public-facing web server.
@@ -61,11 +63,34 @@ class WebServer:
self.last_api_access_time = datetime.now(pytz.UTC)
self.status = "OK"
print(bottle.request.forms.spot)
try:
# Reject if no spot
if not bottle.request.query.spot:
response.content_type = 'application/json'
response.status = 422
return json.dumps("Error - no 'spot' parameter provided", default=serialize_everything)
response.content_type = 'application/json'
response.set_header('Cache-Control', 'no-store')
return json.dumps("OK", default=serialize_everything)
# Read in the spot as JSON then convert to a Spot object
json_spot = json.loads(bottle.request.query.spot)
spot = Spot(**json_spot)
# Reject if no timestamp or dx_call
if not spot.time or not spot.dx_call:
response.content_type = 'application/json'
response.status = 422
return json.dumps("Error - 'time' and 'dx_call' must be provided as a minimum.", default=serialize_everything)
# infer missing data, and add it to our database.
spot.infer_missing()
self.spots.add(spot.guid, spot, expire=MAX_SPOT_AGE)
response.content_type = 'application/json'
response.set_header('Cache-Control', 'no-store')
return json.dumps("OK", default=serialize_everything)
except Exception:
response.content_type = 'application/json'
response.status = 422
return json.dumps("An error occurred parsing your spot. Check it is compliant with the API.", default=serialize_everything)
# Serve a templated page
def serve_template(self, template_name):