2020-12-04 19:54:27 +01:00
|
|
|
from typing import Any, List, Optional, cast
|
2020-08-14 11:23:55 +02:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from django.db import models
|
2020-12-04 19:54:27 +01:00
|
|
|
from django.utils import timezone
|
2020-08-14 11:23:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LicenseError(Exception):
|
|
|
|
"""Exception raised for licensing errors.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
code -- code of the exception
|
|
|
|
detail -- message of the exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, code, detail):
|
|
|
|
self.code = code
|
|
|
|
self.detail = detail
|
|
|
|
|
|
|
|
|
|
|
|
class LicenseManager(models.Manager):
|
2020-10-22 10:58:32 +02:00
|
|
|
def create(self, *args: Any, **kwargs: Any) -> "License":
|
2020-08-26 15:53:24 +02:00
|
|
|
validate = requests.post("https://license.posthog.com/licenses/activate", data={"key": kwargs["key"]})
|
2020-08-14 11:23:55 +02:00
|
|
|
resp = validate.json()
|
|
|
|
if not validate.ok:
|
|
|
|
raise LicenseError(resp["code"], resp["detail"])
|
|
|
|
|
2020-08-26 15:53:24 +02:00
|
|
|
kwargs["valid_until"] = resp["valid_until"]
|
|
|
|
kwargs["plan"] = resp["plan"]
|
2020-12-04 19:54:27 +01:00
|
|
|
return cast(License, super().create(*args, **kwargs))
|
2020-10-22 10:58:32 +02:00
|
|
|
|
2020-12-04 19:54:27 +01:00
|
|
|
def first_valid(self) -> Optional["License"]:
|
|
|
|
return cast(Optional[License], (self.filter(valid_until__gte=timezone.now()).first()))
|
2020-08-14 11:23:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class License(models.Model):
|
|
|
|
objects = LicenseManager()
|
2020-08-26 10:34:57 +02:00
|
|
|
|
2020-08-14 11:23:55 +02:00
|
|
|
created_at: models.DateTimeField = models.DateTimeField(auto_now_add=True)
|
|
|
|
plan: models.CharField = models.CharField(max_length=200)
|
|
|
|
valid_until: models.DateTimeField = models.DateTimeField()
|
|
|
|
key: models.CharField = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
ENTERPRISE_PLAN = "enterprise"
|
2021-03-27 01:38:13 +01:00
|
|
|
ENTERPRISE_FEATURES = ["zapier", "organizations_projects", "google_login", "dashboard_collaboration"]
|
2020-11-12 22:43:06 +01:00
|
|
|
PLANS = {
|
|
|
|
ENTERPRISE_PLAN: ENTERPRISE_FEATURES,
|
|
|
|
}
|
2020-12-04 19:54:27 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available_features(self) -> List[str]:
|
|
|
|
return self.PLANS.get(self.plan, [])
|