2020-08-14 11:23:55 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models import QuerySet
|
|
|
|
from rest_framework import authentication, exceptions, request, response, serializers, viewsets
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2020-10-22 10:58:32 +02:00
|
|
|
from ee.models.license import License, LicenseError
|
2020-08-14 11:23:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LicenseSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = License
|
|
|
|
fields = [
|
|
|
|
"created_at",
|
|
|
|
"plan",
|
|
|
|
"key",
|
|
|
|
"valid_until",
|
|
|
|
]
|
|
|
|
read_only_fields = ["created_at", "plan", "valid_until"]
|
|
|
|
|
|
|
|
|
|
|
|
class LicenseViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = License.objects.all()
|
|
|
|
serializer_class = LicenseSerializer
|
|
|
|
|
|
|
|
def get_queryset(self) -> QuerySet:
|
2020-09-28 13:21:03 +02:00
|
|
|
if getattr(settings, "MULTI_TENANCY", False):
|
2020-08-14 11:23:55 +02:00
|
|
|
return License.objects.none()
|
|
|
|
|
|
|
|
return super().get_queryset()
|
|
|
|
|
|
|
|
def create(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
|
|
|
|
try:
|
|
|
|
license = License.objects.create(key=request.data["key"])
|
|
|
|
except LicenseError as e:
|
|
|
|
return Response(data={"detail": e.detail, "code": e.code}, status=400)
|
|
|
|
|
|
|
|
return Response(LicenseSerializer(license, context={"request": request}).data)
|