<commit_before>from Acquisition import aq_inner
from Products.Five.browser import BrowserView
from plone.dexterity.interfaces import IDexterityFTI
from utils import dict2dynatree
from z3c.form.widget import SequenceWidget
from zope.dottedname.resolve import resolve
from zope.schema import TextLine
from zope.schema.interfaces import IList, ISet
from zope.schema.interfaces import IVocabularyFactory
import interfaces
import json
import z3c.form
import zope.component
import zope.interface


class FieldVocabDynatreeJsonView(BrowserView):

    def __call__(self):
        context = aq_inner(self.context)
        fieldname = self.request.get('fieldname')
        portal_type = self.request.get('portal_type')

        fti = zope.component.getUtility(IDexterityFTI, name=portal_type)
        schema = fti.lookupSchema()
        field = schema.get(fieldname)
        if field is None:
            for behaviorname in fti.behaviors:
                behavior = resolve(behaviorname)
                field = behavior.get(fieldname.split('.')[1])
                if field is not None:
                    break
        if IList.providedBy(field) or ISet.providedBy(field):
            vname = field.value_type.vocabularyName
        else:
            vname = field.vocabularyName
        factory = zope.component.getUtility(IVocabularyFactory, vname)
        tree = factory(context)
        leafsOnly = getattr(tree, 'leafsOnly', True)
        
        tree = dict2dynatree(
            self.context,
            tree,
            [],  # selected not needed here, this is done at js side
            leafsOnly,
            False
        )
        return json.dumps(tree)


class DynatreeWidget(SequenceWidget):
    """ A text field widget with a dynatree javascript vocabulary to determine
        the value.
    """
    zope.interface.implementsOnly(interfaces.IDynatreeWidget)
    klass = u'dynatree-widget'
    selectMode = 1
    minExpandLevel = 1
    autoCollapse = False
    leafsOnly = True # Not used
    showKey = False # Not used

    @property
    def item_value(self):
        # XXX figure out where to get actual value from, workaround follows:
        # imo this should be self.value, but this is None. Reason?
        # anyway needs refactoring
        # also look if this method shall be named displayValue (z3cform style)
        try:
            return self.field.get(self.context) or []
        except AttributeError:
            return []

    @property
    def widget_value(self):
        # Returns the value in a form useful for the edit widget, ie with
        # items separated by |
        value = self.item_value
        if not isinstance(value, basestring):
            value = '|'.join([_ for _ in value])
        return self.request.get(self.__name__, value)
    
    @property
    def display_value(self):
        # Returns the title of all values if the field is a string, or a list of titles otherwise.
        value = self.item_value
        if isinstance(value, basestring):
            return self.terms.getTermByToken(token).title
            
        return sorted([self.terms.getTermByToken(token).title for token in value])

    def extract(self, default=z3c.form.interfaces.NO_VALUE):
        """See z3c.form.interfaces.IWidget."""
        if self.name not in self.request:
            return []
        value = self.request.get(self.name, default)
        if not isinstance(value, basestring):
            raise ValueError('Expected string, got %s' % type(value))
        if value == default:
            return value
        value = value.split('|')
        for token in value:
            if token == self.noValueToken:
                continue
            try:
                self.terms.getTermByToken(token)
            except LookupError:
                # XXX TODO remove value from list instead of skipping all
                return default
        return value

    @property
    def field_name(self):
        return self.__name__
<commit_msg>Skip display of deleted values.<commit_after>from Acquisition import aq_inner
from Products.Five.browser import BrowserView
from plone.dexterity.interfaces import IDexterityFTI
from utils import dict2dynatree
from z3c.form.widget import SequenceWidget
from zope.dottedname.resolve import resolve
from zope.schema import TextLine
from zope.schema.interfaces import IList, ISet
from zope.schema.interfaces import IVocabularyFactory
import interfaces
import json
import z3c.form
import zope.component
import zope.interface


class FieldVocabDynatreeJsonView(BrowserView):

    def __call__(self):
        context = aq_inner(self.context)
        fieldname = self.request.get('fieldname')
        portal_type = self.request.get('portal_type')

        fti = zope.component.getUtility(IDexterityFTI, name=portal_type)
        schema = fti.lookupSchema()
        field = schema.get(fieldname)
        if field is None:
            for behaviorname in fti.behaviors:
                behavior = resolve(behaviorname)
                field = behavior.get(fieldname.split('.')[1])
                if field is not None:
                    break
        if IList.providedBy(field) or ISet.providedBy(field):
            vname = field.value_type.vocabularyName
        else:
            vname = field.vocabularyName
        factory = zope.component.getUtility(IVocabularyFactory, vname)
        tree = factory(context)
        leafsOnly = getattr(tree, 'leafsOnly', True)
        
        tree = dict2dynatree(
            self.context,
            tree,
            [],  # selected not needed here, this is done at js side
            leafsOnly,
            False
        )
        return json.dumps(tree)


class DynatreeWidget(SequenceWidget):
    """ A text field widget with a dynatree javascript vocabulary to determine
        the value.
    """
    zope.interface.implementsOnly(interfaces.IDynatreeWidget)
    klass = u'dynatree-widget'
    selectMode = 1
    minExpandLevel = 1
    autoCollapse = False
    leafsOnly = True # Not used
    showKey = False # Not used

    def _get_term(self, token):
        try:
            return self.terms.getTermByToken(token)
        except LookupError:
            return None
            
    @property
    def item_value(self):
        # XXX figure out where to get actual value from, workaround follows:
        # imo this should be self.value, but this is None. Reason?
        # anyway needs refactoring
        # also look if this method shall be named displayValue (z3cform style)
        try:
            return self.field.get(self.context) or []
        except AttributeError:
            return []

    @property
    def widget_value(self):
        # Returns the value in a form useful for the edit widget, ie with
        # items separated by |
        value = self.item_value
        if not isinstance(value, basestring):
            value = '|'.join([_ for _ in value])
        return self.request.get(self.__name__, value)
    
    @property
    def display_value(self):
        # Returns the title of all values if the field is a string, or a list of titles otherwise.
        value = self.item_value
        if isinstance(value, basestring):
            term = self.terms.getTermByToken(token)
            if term is None:
                return ''
            return term.title
            
        terms = [self._get_term(token) for token in value]
        return sorted(term.title for term in terms if term is not None)

    def extract(self, default=z3c.form.interfaces.NO_VALUE):
        """See z3c.form.interfaces.IWidget."""
        if self.name not in self.request:
            return []
        value = self.request.get(self.name, default)
        if not isinstance(value, basestring):
            raise ValueError('Expected string, got %s' % type(value))
        if value == default:
            return value
        value = value.split('|')
        for token in value:
            if token == self.noValueToken:
                continue
            try:
                self.terms.getTermByToken(token)
            except LookupError:
                # XXX TODO remove value from list instead of skipping all
                return default
        return value

    @property
    def field_name(self):
        return self.__name__

{'fraction_non_alphanumeric': 0.07448991911143306, 'fraction_numerical': 0.002414584087890861, 'mean_word_length': 2.1775987725354815, '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': '2473936', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2236, 'n_words': 680}