<commit_before>from django.db import models
from django.db.models import Q
from django.db.models.loading import get_model

from cyder.base.eav.constants import ATTRIBUTE_INVENTORY
from cyder.base.eav.fields import EAVAttributeField
from cyder.base.eav.models import Attribute, EAVBase
from cyder.base.mixins import ObjectUrlMixin
from cyder.base.models import BaseModel
from cyder.base.helpers import get_display
from cyder.core.system.validators import validate_no_spaces


class System(BaseModel, ObjectUrlMixin):
    name = models.CharField(max_length=255, unique=False,
                            validators=[validate_no_spaces])

    search_fields = ('name',)
    display_fields = ('name',)

    def __str__(self):
        return get_display(self)

    class Meta:
        app_label = 'cyder'
        db_table = 'system'

    @staticmethod
    def filter_by_ctnr(ctnr, objects=None):
        objects = objects or System.objects
        DynamicInterface = get_model('cyder', 'dynamicinterface')
        StaticInterface = get_model('cyder', 'staticinterface')
        dynamic_query = DynamicInterface.objects.filter(
            ctnr=ctnr).values_list('system')
        static_query = StaticInterface.objects.filter(
            ctnr=ctnr).values_list('system')
        return objects.filter(Q(id__in=static_query) | Q(id__in=dynamic_query))

    def check_in_ctnr(self, ctnr):
        ctnrs = self.staticinterface_set.values_list('ctnr', flat=True)
        ctnrs += self.dynamicinterface_set.values_list('ctnr', flat=True)
        return ctnr.pk in ctnrs

    def details(self):
        """For tables."""
        data = super(System, self).details()
        data['data'] = [
            ('Name', 'name', self),
        ]
        return data

    def delete(self):
        DynamicInterface = get_model('cyder', 'dynamicinterface')
        for interface in DynamicInterface.objects.filter(system=self):
            interface.delete(**{'delete_system': False})
        StaticInterface = get_model('cyder', 'staticinterface')
        for interface in StaticInterface.objects.filter(system=self):
            interface.delete(**{'delete_system': False})
        super(System, self).delete()

    @staticmethod
    def eg_metadata():
        """EditableGrid metadata."""
        return {'metadata': [
            {'name': 'name', 'datatype': 'string', 'editable': True},
        ]}


class SystemAV(EAVBase):
    class Meta(EAVBase.Meta):
        app_label = 'cyder'
        db_table = 'system_av'


    entity = models.ForeignKey(System)
    attribute = EAVAttributeField(Attribute,
        type_choices=(ATTRIBUTE_INVENTORY,))
<commit_msg>more efficiently test system-ctnr membership<commit_after>from django.db import models
from django.db.models import Q
from django.db.models.loading import get_model

from cyder.base.eav.constants import ATTRIBUTE_INVENTORY
from cyder.base.eav.fields import EAVAttributeField
from cyder.base.eav.models import Attribute, EAVBase
from cyder.base.mixins import ObjectUrlMixin
from cyder.base.models import BaseModel
from cyder.base.helpers import get_display
from cyder.core.system.validators import validate_no_spaces


class System(BaseModel, ObjectUrlMixin):
    name = models.CharField(max_length=255, unique=False,
                            validators=[validate_no_spaces])

    search_fields = ('name',)
    display_fields = ('name',)

    def __str__(self):
        return get_display(self)

    class Meta:
        app_label = 'cyder'
        db_table = 'system'

    @staticmethod
    def filter_by_ctnr(ctnr, objects=None):
        objects = objects or System.objects
        DynamicInterface = get_model('cyder', 'dynamicinterface')
        StaticInterface = get_model('cyder', 'staticinterface')
        dynamic_query = DynamicInterface.objects.filter(
            ctnr=ctnr).values_list('system')
        static_query = StaticInterface.objects.filter(
            ctnr=ctnr).values_list('system')
        return objects.filter(Q(id__in=static_query) | Q(id__in=dynamic_query))

    def check_in_ctnr(self, ctnr):
        return (self.staticinterface_set.filter(ctnr=ctnr).exists() or
                self.dynamicinterface_set.filter(ctnr=ctnr).exists())

    def details(self):
        """For tables."""
        data = super(System, self).details()
        data['data'] = [
            ('Name', 'name', self),
        ]
        return data

    def delete(self):
        DynamicInterface = get_model('cyder', 'dynamicinterface')
        for interface in DynamicInterface.objects.filter(system=self):
            interface.delete(**{'delete_system': False})
        StaticInterface = get_model('cyder', 'staticinterface')
        for interface in StaticInterface.objects.filter(system=self):
            interface.delete(**{'delete_system': False})
        super(System, self).delete()

    @staticmethod
    def eg_metadata():
        """EditableGrid metadata."""
        return {'metadata': [
            {'name': 'name', 'datatype': 'string', 'editable': True},
        ]}


class SystemAV(EAVBase):
    class Meta(EAVBase.Meta):
        app_label = 'cyder'
        db_table = 'system_av'


    entity = models.ForeignKey(System)
    attribute = EAVAttributeField(Attribute,
        type_choices=(ATTRIBUTE_INVENTORY,))

{'fraction_non_alphanumeric': 0.11281759575274934, 'fraction_numerical': 0.0011376564277588168, 'mean_word_length': 3.1699604743083003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5276460', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1510, 'n_words': 316}