2018-03-23 11:46:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-03-25 13:47:29 +00:00
|
|
|
import random
|
2022-12-02 12:28:49 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
2022-12-20 12:23:42 +00:00
|
|
|
from orgs.mixins.models import JMSOrgBaseModel
|
2022-12-06 03:03:14 +00:00
|
|
|
from .gateway import Gateway
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2021-07-26 08:32:01 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
2022-12-06 03:03:14 +00:00
|
|
|
__all__ = ['Domain']
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
class Domain(JMSOrgBaseModel):
|
2020-07-02 10:51:16 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Domain")
|
2020-07-02 10:51:16 +00:00
|
|
|
unique_together = [('org_id', 'name')]
|
2021-01-17 04:44:12 +00:00
|
|
|
ordering = ('name',)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2022-09-29 12:44:45 +00:00
|
|
|
def select_gateway(self):
|
|
|
|
return self.random_gateway()
|
|
|
|
|
2018-03-25 13:47:29 +00:00
|
|
|
def random_gateway(self):
|
2022-12-06 03:03:14 +00:00
|
|
|
gateways = [gw for gw in self.active_gateways if gw.is_connective]
|
|
|
|
if not gateways:
|
|
|
|
gateways = self.active_gateways
|
|
|
|
logger.warn(f'Gateway all bad. domain={self}, gateway_num={len(gateways)}.')
|
|
|
|
return random.choice(gateways)
|
2022-12-01 10:22:41 +00:00
|
|
|
|
2022-12-06 03:03:14 +00:00
|
|
|
@lazyproperty
|
|
|
|
def active_gateways(self):
|
|
|
|
return self.gateways.filter(is_active=True)
|
2021-07-26 08:32:01 +00:00
|
|
|
|
2022-12-01 10:22:41 +00:00
|
|
|
@lazyproperty
|
2022-12-06 03:03:14 +00:00
|
|
|
def gateways(self):
|
|
|
|
return self.get_gateway_queryset().filter(domain=self)
|
2022-12-01 10:22:41 +00:00
|
|
|
|
2022-12-06 03:03:14 +00:00
|
|
|
@classmethod
|
|
|
|
def get_gateway_queryset(cls):
|
|
|
|
return Gateway.objects.all()
|