2023-12-05 03:16:34 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericRelation
|
|
|
|
from django.db import models
|
2024-01-22 03:36:18 +00:00
|
|
|
from django.db.models import OneToOneField, Count
|
2023-12-05 03:16:34 +00:00
|
|
|
|
2024-01-16 06:55:43 +00:00
|
|
|
from common.utils import lazyproperty
|
2023-12-05 03:16:34 +00:00
|
|
|
from .models import LabeledResource
|
|
|
|
|
|
|
|
__all__ = ['LabeledMixin']
|
|
|
|
|
|
|
|
|
|
|
|
class LabeledMixin(models.Model):
|
2024-01-16 06:55:43 +00:00
|
|
|
labels = GenericRelation(LabeledResource, object_id_field='res_id', content_type_field='res_type')
|
2023-12-05 03:16:34 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2023-12-19 10:42:44 +00:00
|
|
|
|
2024-01-03 08:14:27 +00:00
|
|
|
@classmethod
|
|
|
|
def label_model(cls):
|
|
|
|
pk_field = cls._meta.pk
|
|
|
|
model = cls
|
|
|
|
if isinstance(pk_field, OneToOneField):
|
|
|
|
model = pk_field.related_model
|
|
|
|
return model
|
|
|
|
|
2024-01-16 06:55:43 +00:00
|
|
|
@lazyproperty
|
2024-01-03 08:14:27 +00:00
|
|
|
def real(self):
|
|
|
|
pk_field = self._meta.pk
|
|
|
|
if isinstance(pk_field, OneToOneField):
|
|
|
|
return getattr(self, pk_field.name)
|
|
|
|
return self
|
|
|
|
|
2023-12-19 10:42:44 +00:00
|
|
|
@property
|
2024-01-16 06:55:43 +00:00
|
|
|
def res_labels(self):
|
|
|
|
return self.real.labels
|
2023-12-19 10:42:44 +00:00
|
|
|
|
2024-01-16 06:55:43 +00:00
|
|
|
@res_labels.setter
|
|
|
|
def res_labels(self, value):
|
|
|
|
self.real.labels.set(value, bulk=False)
|
2024-01-22 03:36:18 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def filter_resources_by_labels(cls, resources, label_ids):
|
|
|
|
return cls._get_filter_res_by_labels_m2m_all(resources, label_ids)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_filter_res_by_labels_m2m_in(cls, resources, label_ids):
|
|
|
|
return resources.filter(label_id__in=label_ids)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_filter_res_by_labels_m2m_all(cls, resources, label_ids):
|
|
|
|
if len(label_ids) == 1:
|
|
|
|
return cls._get_filter_res_by_labels_m2m_in(resources, label_ids)
|
|
|
|
|
|
|
|
resources = resources.filter(label_id__in=label_ids) \
|
|
|
|
.values('res_id') \
|
|
|
|
.order_by('res_id') \
|
|
|
|
.annotate(count=Count('res_id', distinct=True)) \
|
|
|
|
.values('res_id', 'count') \
|
|
|
|
.filter(count=len(label_ids))
|
|
|
|
return resources
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_labels_filter_attr_q(cls, value, match):
|
|
|
|
resources = LabeledResource.objects.all()
|
|
|
|
if not value:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if match != 'm2m_all':
|
|
|
|
resources = cls._get_filter_res_by_labels_m2m_in(resources, value)
|
|
|
|
else:
|
|
|
|
resources = cls._get_filter_res_by_labels_m2m_all(resources, value)
|
|
|
|
res_ids = set(resources.values_list('res_id', flat=True))
|
|
|
|
return models.Q(id__in=res_ids)
|