class DemoAllModel(models.Model): choice = StrategyClassField(registry=registry) multiple = MultipleStrategyClassField(registry=registry) custom = StrategyField(registry=registry1) custom_multiple = MultipleStrategyField(registry=registry1)
class DemoModel(models.Model): sender = StrategyClassField(registry=registry)
class DemoModelCallableDefault(models.Model): sender = StrategyClassField(registry=registry, null=True, default=cc )
class DemoModelNoRegistry(models.Model): klass = StrategyClassField(blank=True, null=True) instance = StrategyField(factory=factory, blank=True, null=True)
class DemoModelDefault(models.Model): sender = StrategyClassField(null=True, registry=registry, default='demoproject.demoapp.models.Sender1')
class DemoModelNone(models.Model): sender = StrategyClassField(registry=registry, null=True, blank=True)
class DemoCallableModel(models.Model): sender = StrategyClassField(registry=aa())
class DemoModelCallableDefault(models.Model): sender = StrategyClassField( registry=registry, null=True, default=lambda: 'demoproject.demoapp.models.Sender1')
class Service(MasterDataModel): name = models.CharField(max_length=100, help_text='unique service name', db_index=True, unique=True) description = models.TextField(blank=True, null=True) viewset = StrategyClassField(help_text='class FQN', unique=True, db_index=True) basename = models.CharField(max_length=200, help_text='viewset basename') suffix = models.CharField(max_length=200, help_text='url suffix') url_name = models.CharField(max_length=300, help_text='url name as per drf reverse') access = models.IntegerField(choices=[(k, v) for k, v in acl.ACL_LABELS.items()], default=acl.ACL_ACCESS_LOGIN, help_text="Required privileges") confidentiality = models.IntegerField(choices=acl.CONFIDENTIALITY_CHOICES, default=acl.CLASS_INTERNAL) hidden = models.BooleanField(default=False) cache_version = models.IntegerField(default=1) cache_ttl = models.CharField(default='1d', max_length=5) cache_key = models.CharField( max_length=1000, null=True, blank=True, help_text='Key used to invalidate service cache') source_model = models.ForeignKey( ContentType, models.CASCADE, blank=True, help_text="model used as primary datasource") linked_models = models.ManyToManyField( ContentType, related_name='+', blank=True, help_text="models that the service depends on") class Meta: ordering = ('name', ) verbose_name_plural = "Services" objects = ServiceManager() def invalidate_cache(self): Service.objects.invalidate_cache(id=self.pk) self.refresh_from_db() return self.cache_version def reset_cache(self, value=0): Service.objects.filter(id=self.pk).update(cache_version=value) self.refresh_from_db() def get_access_level(self): # administrators cannot go lower than coded value return max(self.access, self.viewset.default_acl) @cached_property def endpoint(self): try: return reverse(f'api:{self.basename}-list', args=['latest']) except Exception: pass @cached_property def display_name(self): return "{} ({})".format(self.viewset.__name__, self.viewset.source) def doc_url(self): base = '/api/+redoc/#operation/' path = self.suffix.replace('/', '_') return "{0}api_{1}_list".format(base, path) @cached_property def managed_model(self): try: return self.source_model.model_class() except TypeError: return None def save(self, force_insert=False, force_update=False, using=None, update_fields=None): if self.pk: try: v = self.viewset() model = v.get_queryset().model ct = ContentType.objects.get_for_model(model) self.linked_models.add(ct) except Exception as e: logger.exception(e) super().save(force_insert, force_update, using, update_fields) def __str__(self): return self.name