2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime, timezone
|
2023-12-04 14:03:24 +01:00
|
|
|
from typing import Any
|
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2023-12-04 14:03:24 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
from typing_extensions import override
|
|
|
|
|
|
|
|
from corporate.lib.stripe import RemoteServerBillingSession
|
|
|
|
from scripts.lib.zulip_tools import TIMESTAMP_FORMAT
|
2024-05-24 16:49:56 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2023-12-04 14:03:24 +01:00
|
|
|
from zilencer.models import RemoteZulipServer
|
|
|
|
|
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2023-12-04 14:03:24 +01:00
|
|
|
help = "Assigns an existing RemoteZulipServer to the legacy plan"
|
|
|
|
|
|
|
|
@override
|
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
|
|
|
parser.add_argument(
|
|
|
|
"server_id",
|
|
|
|
type=int,
|
|
|
|
help="ID of the RemoteZulipServer to be assigned to the legacy plan",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"renewal_date",
|
|
|
|
type=str,
|
|
|
|
help="Billing cycle renewal date in the format YYYY-MM-DD-HH-MM-SS",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"end_date",
|
|
|
|
type=str,
|
|
|
|
help="Billing cycle end date in the format YYYY-MM-DD-HH-MM-SS",
|
|
|
|
)
|
|
|
|
|
|
|
|
@override
|
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
|
|
|
server_id = options["server_id"]
|
|
|
|
renewal_date_str = options.get("renewal_date")
|
|
|
|
if renewal_date_str is None:
|
|
|
|
renewal_date = timezone_now()
|
|
|
|
else:
|
2023-11-19 19:45:19 +01:00
|
|
|
renewal_date = datetime.strptime(renewal_date_str, TIMESTAMP_FORMAT).replace(
|
|
|
|
tzinfo=timezone.utc
|
2023-12-04 14:03:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
end_date_str = options.get("end_date")
|
|
|
|
if end_date_str is None:
|
|
|
|
raise ValueError("end_date must be provided")
|
|
|
|
|
2023-11-19 19:45:19 +01:00
|
|
|
end_date = datetime.strptime(end_date_str, TIMESTAMP_FORMAT).replace(tzinfo=timezone.utc)
|
2023-12-04 14:03:24 +01:00
|
|
|
|
|
|
|
server = RemoteZulipServer.objects.get(id=server_id)
|
2023-12-11 09:51:07 +01:00
|
|
|
self.migrate_customer_to_legacy_plan(server, renewal_date, end_date)
|
2023-12-04 14:03:24 +01:00
|
|
|
|
2023-12-11 09:51:07 +01:00
|
|
|
def migrate_customer_to_legacy_plan(
|
2023-12-04 14:03:24 +01:00
|
|
|
self,
|
|
|
|
server: RemoteZulipServer,
|
2023-11-19 19:45:19 +01:00
|
|
|
renewal_date: datetime,
|
|
|
|
end_date: datetime,
|
2023-12-04 14:03:24 +01:00
|
|
|
) -> None:
|
|
|
|
billing_schedule = RemoteServerBillingSession(server)
|
2023-12-11 09:51:07 +01:00
|
|
|
billing_schedule.migrate_customer_to_legacy_plan(renewal_date, end_date)
|