Skip to content

Commit

Permalink
WIP: licensing
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Mar 15, 2024
1 parent 2ffe8e9 commit c92e824
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
18 changes: 15 additions & 3 deletions api/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
from organisations.subscriptions.exceptions import (
SubscriptionDoesNotSupportSeatUpgrade,
)
from organisations.subscriptions.licensing.licensing import (
licence_file_exists,
read_license_file,
)
from organisations.subscriptions.metadata import BaseSubscriptionMetadata
from organisations.subscriptions.xero.metadata import XeroSubscriptionMetadata
from users.utils.mailer_lite import MailerLite
Expand Down Expand Up @@ -378,10 +382,18 @@ def _get_subscription_metadata_for_self_hosted(self) -> BaseSubscriptionMetadata
if not is_enterprise():
return FREE_PLAN_SUBSCRIPTION_METADATA

if licence_file_exists():
licence_information = read_license_file()
return BaseSubscriptionMetadata(
seats=licence_information.get_remaining_seats(),
projects=licence_information.get_remaining_projects(),
)

return BaseSubscriptionMetadata(
seats=self.max_seats,
api_calls=self.max_api_calls,
projects=None,
seats=MAX_SEATS_IN_FREE_PLAN,
# TODO: this should probably just be None, we don't restrict API calls on prem
api_calls=MAX_API_CALLS_IN_FREE_PLAN,
projects=settings.MAX_PROJECTS_IN_FREE_PLAN,
)

def add_single_seat(self):
Expand Down
1 change: 1 addition & 0 deletions api/organisations/subscriptions/licensing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: split this into a private package?
31 changes: 31 additions & 0 deletions api/organisations/subscriptions/licensing/licensing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
from datetime import datetime

from pydantic import BaseModel

from organisations.models import UserOrganisation
from projects.models import Project


class LicenceInformation(BaseModel):
licensee: str
plan_id: str
expiry_date: datetime

# TODO: should these live in a nested object?
num_seats: int
num_projects: int # TODO: what about Flagsmith on Flagsmith project?

def get_remaining_seats(self) -> int:
return self.num_seats - UserOrganisation.objects.count()

def get_remaining_projects(self) -> int:
return self.num_projects - Project.objects.count()


def licence_file_exists(licence_file_path: str = "") -> bool:
return os.path.isfile(licence_file_path)


def read_license_file(licence_file_path: str = "") -> LicenceInformation:
return LicenceInformation.parse_file(licence_file_path)
4 changes: 2 additions & 2 deletions api/organisations/subscriptions/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class BaseSubscriptionMetadata:
def __init__(
self,
seats: int = 0,
api_calls: int = 0,
api_calls: typing.Optional[int] = None,
projects: typing.Optional[int] = None,
chargebee_email=None,
chargebee_email: str = None,
):
self.seats = seats
self.api_calls = api_calls
Expand Down

0 comments on commit c92e824

Please sign in to comment.