Skip to content

Commit 6dfcc37

Browse files
committed
Add GeoJSON and improve API
1 parent d2ddf79 commit 6dfcc37

16 files changed

Lines changed: 508 additions & 87 deletions

opendrift_leeway_webgui/api/v1/serializers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ class LeewaySimulationSerializer(serializers.ModelSerializer):
1313
#: Show username instead of id
1414
username = serializers.ReadOnlyField(source="user.username")
1515

16+
#: Expose the model's error property (last line of traceback)
17+
error = serializers.ReadOnlyField()
18+
19+
#: True once simulation_finished is set
20+
completed = serializers.SerializerMethodField()
21+
22+
def get_completed(self, obj):
23+
"""
24+
:param obj: The simulation instance
25+
:type obj: ~opendrift_leeway_webgui.leeway.models.LeewaySimulation
26+
:rtype: bool
27+
"""
28+
return obj.simulation_finished is not None
29+
1630
class Meta:
1731
"""
1832
Define model and the corresponding fields
@@ -24,15 +38,27 @@ class Meta:
2438
#: Exclude user field because the username is shown instead
2539
exclude = ["user"]
2640

41+
#: Set example values for drf-spectacular Swagger documentation
42+
extra_kwargs = {
43+
"duration": {"default": 12, "help_text": "Length of simulation in hours."},
44+
"radius": {
45+
"default": 1000,
46+
"help_text": "Radius in meters for distributing particles around start coordinates.",
47+
},
48+
}
49+
2750
#: Define fields which are shown when retrieving simulations,
2851
#: but cannot be set when creating new ones
2952
read_only_fields = [
3053
"uuid",
3154
"img",
3255
"netcdf",
56+
"geojson",
3357
"traceback",
3458
"simulation_started",
3559
"simulation_finished",
60+
"error",
61+
"completed",
3662
]
3763

3864
def create(self, validated_data):

opendrift_leeway_webgui/api/v1/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class LeewaySimulationViewSet(
2525
#: The serializer to use for simulations
2626
serializer_class = LeewaySimulationSerializer
2727

28+
#: Use UUID as the lookup field instead of the integer primary key
29+
lookup_field = "uuid"
30+
2831
def get_queryset(self):
2932
"""
3033
Only return the simulations of the current user

opendrift_leeway_webgui/core/asgi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
from django.core.asgi import get_asgi_application
1111

12-
os.environ.setdefault(
13-
"DJANGO_SETTINGS_MODULE", "opendrift_leeway_webgui.core.settings"
14-
)
12+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "opendrift_leeway_webgui.core.settings")
1513

1614
# Read config from config file
1715
config = configparser.ConfigParser(interpolation=None)

opendrift_leeway_webgui/core/settings.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
x.strip() for x in os.environ.get("LEEWAY_ALLOWED_HOSTS", "").split(",") if x
5353
]
5454

55-
CSRF_TRUSTED_ORIGINS = [
56-
f"https://{host}" for host in ALLOWED_HOSTS
57-
]
55+
CSRF_TRUSTED_ORIGINS = [f"https://{host}" for host in ALLOWED_HOSTS]
5856

5957
#: Enabled applications (see :setting:`django:INSTALLED_APPS`)
6058
INSTALLED_APPS = [
@@ -374,5 +372,9 @@
374372
#: (see :setting:`django:EMAIL_USE_SSL`)
375373
EMAIL_USE_SSL = bool(strtobool(os.environ.get("LEEWAY_EMAIL_USE_SSL", "False")))
376374

377-
COPERNICUSMARINE_SERVICE_USERNAME = os.environ.get("LEEWAY_COPERNICUSMARINE_SERVICE_USERNAME")
378-
COPERNICUSMARINE_SERVICE_PASSWORD = os.environ.get("LEEWAY_COPERNICUSMARINE_SERVICE_PASSWORD")
375+
COPERNICUSMARINE_SERVICE_USERNAME = os.environ.get(
376+
"LEEWAY_COPERNICUSMARINE_SERVICE_USERNAME"
377+
)
378+
COPERNICUSMARINE_SERVICE_PASSWORD = os.environ.get(
379+
"LEEWAY_COPERNICUSMARINE_SERVICE_PASSWORD"
380+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 5.2.7 on 2026-02-20 17:35
2+
3+
import opendrift_leeway_webgui.leeway.models
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [("leeway", "0008_leewaysimulation_traceback")]
10+
11+
operations = [
12+
migrations.AddField(
13+
model_name="leewaysimulation",
14+
name="geojson",
15+
field=models.FileField(
16+
null=True,
17+
storage=opendrift_leeway_webgui.leeway.models.simulation_storage,
18+
upload_to="",
19+
verbose_name="GeoJSON of simulated trajectories",
20+
),
21+
)
22+
]

opendrift_leeway_webgui/leeway/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class LeewaySimulation(models.Model):
4141
netcdf = models.FileField(
4242
null=True, storage=simulation_storage, verbose_name=_("NetCDF file")
4343
)
44+
geojson = models.FileField(
45+
null=True, storage=simulation_storage, verbose_name=_("GeoJSON of simulated trajectories")
46+
)
4447
traceback = models.TextField(blank=True, verbose_name=_("traceback"))
4548

4649
@property

0 commit comments

Comments
 (0)