Skip to content

Commit

Permalink
corporate: Replace unused accessors with database constraint.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and timabbott committed May 31, 2023
1 parent 2db9c0b commit 19dee54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
24 changes: 24 additions & 0 deletions corporate/migrations/0018_customer_cloud_xor_self_hosted.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
24 changes: 9 additions & 15 deletions corporate/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand All @@ -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()
Expand Down

0 comments on commit 19dee54

Please sign in to comment.