corporate: Replace unused accessors with database constraint.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-05-29 20:12:51 -07:00 committed by Tim Abbott
parent 2db9c0bc21
commit 19dee5450d
2 changed files with 33 additions and 15 deletions

View File

@ -0,0 +1,24 @@
# Generated by Django 4.2.1 on 2023-05-30 03:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
(
"corporate",
"0017_rename_exempt_from_from_license_number_check_customer_exempt_from_license_number_check",
),
]
operations = [
migrations.AddConstraint(
model_name="customer",
constraint=models.CheckConstraint(
check=models.Q(
("realm__isnull", False), ("remote_server__isnull", False), _connector="XOR"
),
name="cloud_xor_self_hosted",
),
),
]

View File

@ -3,7 +3,7 @@ from typing import Any, Dict, Optional, Union
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import CASCADE
from django.db.models import CASCADE, Q
from zerver.models import Realm, UserProfile
from zilencer.models import RemoteZulipServer
@ -28,23 +28,17 @@ class Customer(models.Model):
# they purchased.
exempt_from_license_number_check = models.BooleanField(default=False)
class Meta:
constraints = [
models.CheckConstraint(
check=Q(realm__isnull=False) ^ Q(remote_server__isnull=False),
name="cloud_xor_self_hosted",
)
]
def __str__(self) -> str:
return f"{self.realm!r} {self.stripe_customer_id}"
@property
def is_self_hosted(self) -> bool:
is_self_hosted = self.remote_server is not None
if is_self_hosted:
assert self.realm is None
return is_self_hosted
@property
def is_cloud(self) -> bool:
is_cloud = self.realm is not None
if is_cloud:
assert self.remote_server is None
return is_cloud
def get_customer_by_realm(realm: Realm) -> Optional[Customer]:
return Customer.objects.filter(realm=realm).first()