<commit_before>import logging
from datetime import datetime

from sqlalchemy import Table, Column, ForeignKey, Integer, Boolean, or_
from sqlalchemy.orm import reconstructor, eagerload

import meta
import instance_filter as ifilter

from delegateable import Delegateable

log = logging.getLogger(__name__)


proposal_table = Table('proposal', meta.data,
    Column('id', Integer, ForeignKey('delegateable.id'), primary_key=True),
    Column('description_id', Integer, ForeignKey('page.id'), nullable=True),
    Column('adopt_poll_id', Integer, ForeignKey('poll.id'), nullable=True),
    Column('rate_poll_id', Integer, ForeignKey('poll.id'), nullable=True),
    Column('adopted', Boolean, default=False)
    )


class Proposal(Delegateable):

    def __init__(self, instance, label, creator):
        self.init_child(instance, label, creator)
        self._current_alternatives = None

    @reconstructor
    def _reconstruct(self):
        self._current_alternatives = None

    @property
    def selections(self):
        return [s for s in self._selections if not s.is_deleted()]

    @property
    def title(self):
        if self.description is None or self.description.head is None:
            return self.label
        return self.description.title

    @property
    def full_title(self):
        return self.title

    def is_adopt_polling(self):
        return (self.adopt_poll is not None) and \
            (not self.adopt_poll.has_ended())

    def is_mutable(self):
        return (not self.is_adopt_polling()) and (not self.adopted) and \
                (not self.instance.frozen)

    def has_implementation(self):
        from text import Text
        for selection in self.selections:
            selected = selection.selected
            if selected is not None and selected != Text.HEAD:
                return True
        return False

    def can_adopt(self):
        return not self.is_adopt_polling() \
            and self.instance.allow_adopt \
            and (not self.instance.use_norms or self.has_implementation()) \
            and not self.adopted

    def adopt(self, at_time=None):
        from text import Text
        if at_time is None:
            at_time = datetime.utcnow()
        if not self.is_adopt_polling():
            return
        for selection in self.selections:
            selected = selection.selected
            if selected is None or selected == Text.HEAD:
                continue
            if not selected in selection.page.variants:
                continue
            source_text = selection.page.variant_at(selected,
                                                    self.adopt_poll.begin_time)
            dest_text = Text.create(selection.page,
                                    Text.HEAD,
                                    source_text.user,
                                    source_text.title, source_text.text,
                                    parent=source_text,
                                    wiki=source_text.wiki)
            dest_text.create_time = at_time
        self.adopted = True
        meta.Session.commit()
        self.adopt_poll.end(at_time)
        meta.Session.flush()

    @classmethod
    def find(cls, id, instance_filter=True, include_deleted=False, full=False):
        try:
            q = meta.Session.query(Proposal)
            id = int(unicode(id).split('-', 1)[0])
            q = q.filter(Proposal.id == id)
            if full:
                q = q.options(eagerload(Proposal.comments))
                q = q.options(eagerload(Proposal.adopt_poll))
                q = q.options(eagerload(Proposal.rate_poll))
                q = q.options(eagerload(Proposal.taggings))
                q = q.options(eagerload(Proposal.parents))
            if ifilter.has_instance() and instance_filter:
                q = q.filter(Proposal.instance_id == ifilter.get_instance().id)
            if not include_deleted:
                q = q.filter(or_(Proposal.delete_time == None,
                                 Proposal.delete_time > datetime.utcnow()))
            return q.limit(1).first()
        except Exception, e:<commit_msg>Proposal adoption: Do not adopot the variants title<commit_after>import logging
from datetime import datetime

from sqlalchemy import Table, Column, ForeignKey, Integer, Boolean, or_
from sqlalchemy.orm import reconstructor, eagerload

import meta
import instance_filter as ifilter

from delegateable import Delegateable

log = logging.getLogger(__name__)


proposal_table = Table('proposal', meta.data,
    Column('id', Integer, ForeignKey('delegateable.id'), primary_key=True),
    Column('description_id', Integer, ForeignKey('page.id'), nullable=True),
    Column('adopt_poll_id', Integer, ForeignKey('poll.id'), nullable=True),
    Column('rate_poll_id', Integer, ForeignKey('poll.id'), nullable=True),
    Column('adopted', Boolean, default=False)
    )


class Proposal(Delegateable):

    def __init__(self, instance, label, creator):
        self.init_child(instance, label, creator)
        self._current_alternatives = None

    @reconstructor
    def _reconstruct(self):
        self._current_alternatives = None

    @property
    def selections(self):
        return [s for s in self._selections if not s.is_deleted()]

    @property
    def title(self):
        if self.description is None or self.description.head is None:
            return self.label
        return self.description.title

    @property
    def full_title(self):
        return self.title

    def is_adopt_polling(self):
        return (self.adopt_poll is not None) and \
            (not self.adopt_poll.has_ended())

    def is_mutable(self):
        return (not self.is_adopt_polling()) and (not self.adopted) and \
                (not self.instance.frozen)

    def has_implementation(self):
        from text import Text
        for selection in self.selections:
            selected = selection.selected
            if selected is not None and selected != Text.HEAD:
                return True
        return False

    def can_adopt(self):
        return not self.is_adopt_polling() \
            and self.instance.allow_adopt \
            and (not self.instance.use_norms or self.has_implementation()) \
            and not self.adopted

    def adopt(self, at_time=None):
        from text import Text
        if at_time is None:
            at_time = datetime.utcnow()
        if not self.is_adopt_polling():
            return
        for selection in self.selections:
            selected = selection.selected
            if selected is None or selected == Text.HEAD:
                continue
            if not selected in selection.page.variants:
                continue
            source_text = selection.page.variant_at(selected,
                                                    self.adopt_poll.begin_time)
            dest_text = Text.create(selection.page,
                                    Text.HEAD,
                                    source_text.user,
                                    selection.page.head.title,
                                    source_text.text,
                                    parent=source_text,
                                    wiki=source_text.wiki)
            dest_text.create_time = at_time
        self.adopted = True
        meta.Session.commit()
        self.adopt_poll.end(at_time)
        meta.Session.flush()

    @classmethod
    def find(cls, id, instance_filter=True, include_deleted=False, full=False):
        try:
            q = meta.Session.query(Proposal)
            id = int(unicode(id).split('-', 1)[0])
            q = q.filter(Proposal.id == id)
            if full:
                q = q.options(eagerload(Proposal.comments))
                q = q.options(eagerload(Proposal.adopt_poll))
                q = q.options(eagerload(Proposal.rate_poll))
                q = q.options(eagerload(Proposal.taggings))
                q = q.options(eagerload(Proposal.parents))
            if ifilter.has_instance() and instance_filter:
                q = q.filter(Proposal.instance_id == ifilter.get_instance().id)
            if not include_deleted:
                q = q.filter(or_(Proposal.delete_time == None,
                                 Proposal.delete_time > datetime.utcnow()))
            return q.limit(1).first()
        except Exception, e:
{'fraction_non_alphanumeric': 0.08928998326559885, 'fraction_numerical': 0.0007171886206072197, 'mean_word_length': 1.9649184975194898, 'pattern_counts': {'":': 0, '<': 3, '<?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': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5495412', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2249, 'n_words': 538}