-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31cbc9a
commit c2ce050
Showing
11 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.apps import AppConfig | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class ApiConfig(AppConfig): | ||
""" | ||
Application settings for the `api` app, | ||
which is the app providing the REST API. | ||
Inherits from `AppConfig`. | ||
""" | ||
|
||
name = "opendrift_leeway_webgui.api" | ||
verbose_name = _("API") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
URL patterns for the Opendrift Leeway Webgui API | ||
""" | ||
from django.urls import include, path | ||
|
||
#: The namespace for this URL config (see :attr:`django.urls.ResolverMatch.app_name`) | ||
app_name = "api" | ||
|
||
#: The url patterns of this module (see :doc:`django:topics/http/urls`) | ||
urlpatterns = [ | ||
path("", include("opendrift_leeway_webgui.api.v1.urls", namespace="default")), | ||
path("v1/", include("opendrift_leeway_webgui.api.v1.urls", namespace="v1")), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
The first version of the Opendrift Leeway Webgui API. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from rest_framework import serializers | ||
|
||
from ...leeway.models import LeewaySimulation | ||
|
||
|
||
class LeewaySimulationSerializer(serializers.ModelSerializer): | ||
""" | ||
Serializer for the Leeway Simulations. Inherits from | ||
`serializers.ModelSerializer`. | ||
""" | ||
|
||
#: Show username instead of id | ||
username = serializers.ReadOnlyField(source="user.username") | ||
|
||
class Meta: | ||
""" | ||
Define model and the corresponding fields | ||
""" | ||
|
||
#: The model class for this serializer | ||
model = LeewaySimulation | ||
|
||
#: Exclude user field because the username is shown instead | ||
exclude = ["user"] | ||
|
||
#: Define fields which are shown when retrieving simulations, | ||
#: but cannot be set when creating new ones | ||
read_only_fields = [ | ||
"uuid", | ||
"img", | ||
"netcdf", | ||
"traceback", | ||
"simulation_started", | ||
"simulation_finished", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
URL patterns for the first version of the Opendrift Leeway Webgui API | ||
""" | ||
from django.urls import include, path | ||
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView | ||
from rest_framework.routers import DefaultRouter | ||
|
||
from . import views | ||
|
||
#: The namespace for this URL config (see :attr:`django.urls.ResolverMatch.app_name`) | ||
app_name = "v1" | ||
|
||
#: Router for dynamic url patterns | ||
router = DefaultRouter() | ||
router.register("simulations", views.LeewaySimulationViewSet, "simulations") | ||
|
||
#: The url patterns of this module (see :doc:`django:topics/http/urls`) | ||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
path("auth/", include("knox.urls")), | ||
path("schema/", SpectacularAPIView.as_view(), name="schema"), | ||
path( | ||
"docs/", | ||
SpectacularSwaggerView.as_view(url_name="api:v1:schema"), | ||
name="swagger-ui", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from rest_framework import mixins, viewsets | ||
from rest_framework.permissions import IsAuthenticated | ||
|
||
from .serializers import LeewaySimulationSerializer | ||
|
||
|
||
# pylint: disable=too-many-ancestors | ||
class LeewaySimulationViewSet( | ||
mixins.CreateModelMixin, | ||
mixins.ListModelMixin, | ||
mixins.RetrieveModelMixin, | ||
viewsets.GenericViewSet, | ||
): | ||
""" | ||
A viewset for simulations of the authenticated user, with the option to | ||
- Create new simulations | ||
- List all existing simulations | ||
- Retrieve a single simulation record | ||
""" | ||
|
||
#: Only enable this viewset for authenticated users | ||
permission_classes = (IsAuthenticated,) | ||
|
||
#: The serializer to use for simulations | ||
serializer_class = LeewaySimulationSerializer | ||
|
||
def get_queryset(self): | ||
""" | ||
Only return the simulations of the current user | ||
""" | ||
return self.request.user.leewaysimulation_set.all() | ||
|
||
def perform_create(self, serializer): | ||
""" | ||
Automatically set the user field on creation | ||
""" | ||
serializer.save(user=self.request.user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters