Skip to content

Commit f72844b

Browse files
committed
Add a API URL to retrieve basic subscription details for the logged in user
1 parent 7fb503f commit f72844b

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from qfieldcloud.subscription.models import CurrentSubscription
2+
from rest_framework import serializers
3+
4+
5+
class CurrentSubscriptionSerializer(serializers.ModelSerializer):
6+
storage_used_bytes = serializers.SerializerMethodField()
7+
active_plan_display_name = serializers.SerializerMethodField()
8+
9+
def get_storage_used_bytes(self, obj):
10+
return obj.account.storage_used_bytes
11+
12+
def get_active_plan_display_name(self, obj):
13+
return obj.plan.display_name if obj.plan else None
14+
15+
class Meta:
16+
model = CurrentSubscription
17+
fields = (
18+
"storage_used_bytes",
19+
"active_storage_total_bytes",
20+
"active_plan_display_name",
21+
"active_until",
22+
)
23+
read_only_fields = (
24+
"storage_used_bytes",
25+
"active_storage_total_bytes",
26+
"active_plan_display_name",
27+
"active_until",
28+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from rest_framework.generics import RetrieveAPIView
2+
from rest_framework.permissions import IsAuthenticated
3+
from rest_framework.response import Response
4+
5+
from qfieldcloud.subscription.serializers import CurrentSubscriptionSerializer
6+
7+
8+
class CurrentSubscriptionView(RetrieveAPIView):
9+
"""Read current subscription information including storage details.
10+
11+
Accepts nothing, returns the current subscription information.
12+
"""
13+
14+
serializer_class = CurrentSubscriptionSerializer
15+
permission_classes = (IsAuthenticated,)
16+
17+
def get_object(self):
18+
return self.request.user.useraccount.current_subscription
19+
20+
def get(self, request):
21+
return Response(
22+
CurrentSubscriptionSerializer(
23+
self.request.user.useraccount.current_subscription
24+
).data
25+
)

docker-app/qfieldcloud/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
SpectacularSwaggerView,
2929
)
3030
from qfieldcloud.authentication import views as auth_views
31+
from qfieldcloud.subscription import views as subscription_views
3132
from qfieldcloud.core.views import files_views
3233
from rest_framework import permissions
3334

@@ -75,6 +76,10 @@
7576
path("api/v1/auth/token/", auth_views.LoginView.as_view()),
7677
path("api/v1/auth/user/", auth_views.UserView.as_view()),
7778
path("api/v1/auth/logout/", auth_views.LogoutView.as_view()),
79+
path(
80+
"api/v1/subscription/current/",
81+
subscription_views.CurrentSubscriptionView.as_view(),
82+
),
7883
path("api/v1/", include("qfieldcloud.core.urls")),
7984
path("auth/", include("rest_framework.urls")),
8085
path("accounts/", include("allauth.urls")),

0 commit comments

Comments
 (0)