<commit_before>from .. import db
from .. models import Rating

class OptionAssociation(db.Model):
    """
    Association between a resource and a descriptor with an index for the
    value of the option.
    """
    __tablename__ = 'option_associations'
    resource_id = db.Column(db.Integer, db.ForeignKey('resources.id'),
                            primary_key=True)
    descriptor_id = db.Column(db.Integer, db.ForeignKey('descriptors.id'),
                              primary_key=True)
    option = db.Column(db.Integer, primary_key=True)
    resource = db.relationship('Resource',
                               back_populates='option_descriptors')
    descriptor = db.relationship('Descriptor',
                                 back_populates='option_resources')

    def __repr__(self):
        return '%s: %s' % (self.descriptor.name,
                           self.descriptor.values[self.option])


class TextAssociation(db.Model):
    """
    Association between a resource and a descriptor with a text field for the
    value of the descriptor.
    """
    __tablename__ = 'text_associations'
    resource_id = db.Column(db.Integer, db.ForeignKey('resources.id'),
                            primary_key=True)
    descriptor_id = db.Column(db.Integer, db.ForeignKey('descriptors.id'),
                              primary_key=True)
    text = db.Column(db.String(64))
    resource = db.relationship('Resource', back_populates='text_descriptors')
    descriptor = db.relationship('Descriptor', back_populates='text_resources')

    def __repr__(self):
        return '%s: %s' % (self.descriptor.name, self.text)


class Descriptor(db.Model):
    """
    Schema for descriptors that contain the name and values for an
    attribute of a resource.
    """
    __tablename__ = 'descriptors'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True)
    values = db.Column(db.PickleType)
    is_searchable = db.Column(db.Boolean)
    text_resources = db.relationship(
        'TextAssociation',
        back_populates='descriptor',
        cascade='save-update, merge, delete, delete-orphan'
    )
    option_resources = db.relationship(
        'OptionAssociation',
        back_populates='descriptor',
        cascade='save-update, merge, delete, delete-orphan'
    )

    def __repr__(self):
        return '<Descriptor \'%s\'>' % self.name

class RequiredOptionDescriptor(db.Model):
    __tablename__ = 'required_option_descriptor'
    id = db.Column(db.Integer, primary_key=True)
    descriptor_id = db.Column(db.Integer, db.ForeignKey('descriptors.id'));
    @staticmethod
    def insert_required_option_descriptor():
        required_option_descriptor = RequiredOptionDescriptor(descriptor_id=-1)
        db.session.add(required_option_descriptor)
        db.session.commit()


class Resource(db.Model):
    """
    Schema for resources with relationships to descriptors.
    """<commit_msg>remove foreign key from required descriptor<commit_after>from .. import db
from .. models import Rating

class OptionAssociation(db.Model):
    """
    Association between a resource and a descriptor with an index for the
    value of the option.
    """
    __tablename__ = 'option_associations'
    resource_id = db.Column(db.Integer, db.ForeignKey('resources.id'),
                            primary_key=True)
    descriptor_id = db.Column(db.Integer, db.ForeignKey('descriptors.id'),
                              primary_key=True)
    option = db.Column(db.Integer, primary_key=True)
    resource = db.relationship('Resource',
                               back_populates='option_descriptors')
    descriptor = db.relationship('Descriptor',
                                 back_populates='option_resources')

    def __repr__(self):
        return '%s: %s' % (self.descriptor.name,
                           self.descriptor.values[self.option])


class TextAssociation(db.Model):
    """
    Association between a resource and a descriptor with a text field for the
    value of the descriptor.
    """
    __tablename__ = 'text_associations'
    resource_id = db.Column(db.Integer, db.ForeignKey('resources.id'),
                            primary_key=True)
    descriptor_id = db.Column(db.Integer, db.ForeignKey('descriptors.id'),
                              primary_key=True)
    text = db.Column(db.String(64))
    resource = db.relationship('Resource', back_populates='text_descriptors')
    descriptor = db.relationship('Descriptor', back_populates='text_resources')

    def __repr__(self):
        return '%s: %s' % (self.descriptor.name, self.text)


class Descriptor(db.Model):
    """
    Schema for descriptors that contain the name and values for an
    attribute of a resource.
    """
    __tablename__ = 'descriptors'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True)
    values = db.Column(db.PickleType)
    is_searchable = db.Column(db.Boolean)
    text_resources = db.relationship(
        'TextAssociation',
        back_populates='descriptor',
        cascade='save-update, merge, delete, delete-orphan'
    )
    option_resources = db.relationship(
        'OptionAssociation',
        back_populates='descriptor',
        cascade='save-update, merge, delete, delete-orphan'
    )

    def __repr__(self):
        return '<Descriptor \'%s\'>' % self.name

class RequiredOptionDescriptor(db.Model):
    __tablename__ = 'required_option_descriptor'
    id = db.Column(db.Integer, primary_key=True)
    descriptor_id = db.Column(db.Integer);
    @staticmethod
    def insert_required_option_descriptor():
        required_option_descriptor = RequiredOptionDescriptor(descriptor_id=-1)
        db.session.add(required_option_descriptor)
        db.session.commit()


class Resource(db.Model):
    """
    Schema for resources with relationships to descriptors.
    """
{'fraction_non_alphanumeric': 0.10937764965236561, 'fraction_numerical': 0.0016957775139901645, 'mean_word_length': 2.812540400775695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6162970', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1632, 'n_words': 348}