mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
992 B
38 lines
992 B
from django.contrib.contenttypes.fields import GenericRelation |
|
from django.db import models |
|
from django.db.models import OneToOneField |
|
|
|
from common.utils import lazyproperty |
|
from .models import LabeledResource |
|
|
|
__all__ = ['LabeledMixin'] |
|
|
|
|
|
class LabeledMixin(models.Model): |
|
labels = GenericRelation(LabeledResource, object_id_field='res_id', content_type_field='res_type') |
|
|
|
class Meta: |
|
abstract = True |
|
|
|
@classmethod |
|
def label_model(cls): |
|
pk_field = cls._meta.pk |
|
model = cls |
|
if isinstance(pk_field, OneToOneField): |
|
model = pk_field.related_model |
|
return model |
|
|
|
@lazyproperty |
|
def real(self): |
|
pk_field = self._meta.pk |
|
if isinstance(pk_field, OneToOneField): |
|
return getattr(self, pk_field.name) |
|
return self |
|
|
|
@property |
|
def res_labels(self): |
|
return self.real.labels |
|
|
|
@res_labels.setter |
|
def res_labels(self, value): |
|
self.real.labels.set(value, bulk=False)
|
|
|