0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-25 11:17:50 +01:00
posthog/ee/api/license.py
Tim Glaser 23f5918477
Add enterprise edition and licensing (#1390)
* Add enterprise edition and licensing

* Cypress test

* License wording change

* Fix typos
2020-08-14 11:23:55 +02:00

41 lines
1.2 KiB
Python

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
from ee.models import License
from ee.models.license import LicenseError
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:
if hasattr(settings, "MULTI_TENANCY"):
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)