Esempio n. 1
0
from zope.schema.interfaces import IChoice
from zope.schema.interfaces import IList
from zope.schema import Choice
from zope.schema import List
from zope.schema.interfaces import IFromUnicode
from plone.supermodel.exportimport import ChoiceHandler
from plone.supermodel.exportimport import BaseHandler


class IRadiobutton(IChoice):
    pass


@implementer(IRadiobutton, IFromUnicode)
class Radiobutton(Choice):
    pass


class ICheckbox(IList):
    pass


@implementer(ICheckbox, IFromUnicode)
class Checkbox(List):
    pass


# plone.supermodel export/import handler
RadiobuttonHandler = ChoiceHandler(Radiobutton)
CheckboxHandler = BaseHandler(Checkbox)
Esempio n. 2
0
class TestChoiceHandling(unittest.TestCase):
   
    def setUp(self):
        configure()
        self.handler = ChoiceHandler(schema.Choice)

    def _choice(self):
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t) for t in (u'a', u'b', u'c')]
            )
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element><element>b</element><element>c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_empty(self):
        # add an empty string term to vocabulary
        vocab = SimpleVocabulary([SimpleTerm(t, title=t) for t in (u'a', u'')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element>'\
            '<element></element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_term_titles(self):
        # two terms with distinct titles, one with same as value:
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t.upper()) for t in (u'a', u'b')] +
            [SimpleTerm(u'c', title=u'c')],
            )
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element key="a">A</element>'\
            '<element key="b">B</element>'\
            '<element key="c">c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)
 
    def test_choice_serialized(self):
        field, expected = self._choice()
        el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
        self.assertEquals(etree.tostring(el), expected)
        # now with an empty string term in vocab:
        field, expected = self._choice_with_empty()
        el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
        self.assertEquals(etree.tostring(el), expected)
        # now with terms that have titles:
        field, expected = self._choice_with_term_titles()
        el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
        self.assertEquals(etree.tostring(el), expected)

    def test_choice_parsing(self):
        _termvalues = lambda vocab: tuple((t.value, t.title) for t in vocab)
        cases = (
            self._choice(),
            self._choice_with_empty(),
            self._choice_with_term_titles(),
            )
        for field, expected in cases:
            el = etree.fromstring(expected)
            imported_field = self.handler.read(el)
            self.assertEquals(
                _termvalues(imported_field.vocabulary),
                _termvalues(field.vocabulary),
                )
Esempio n. 3
0
 def setUp(self):
     configure()
     self.handler = ChoiceHandler(schema.Choice)
Esempio n. 4
0
class TestChoiceHandling(unittest.TestCase):

    def setUp(self):
        configure()
        self.handler = ChoiceHandler(schema.Choice)

    def _choice(self):
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t) for t in (u'a', u'b', u'c')]
            )
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element><element>b</element><element>c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_empty(self):
        # add an empty string term to vocabulary
        vocab = SimpleVocabulary([SimpleTerm(t, title=t) for t in (u'a', u'')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element>'\
            '<element></element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_term_titles(self):
        # two terms with distinct titles, one with same as value:
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t.upper()) for t in (u'a', u'b')] +
            [SimpleTerm(u'c', title=u'c')],
            )
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element key="a">A</element>'\
            '<element key="b">B</element>'\
            '<element key="c">c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_term_titles_and_ns(self):
        # two terms with distinct titles, one with same as value:
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t.upper()) for t in (u'a', u'b')] +
            [SimpleTerm(u'c', title=u'c')],
            )
        expected = '<field name="myfield" type="zope.schema.Choice"'\
            '      xmlns="http://namespaces.plone.org/supermodel/schema">'\
            '<values>'\
            '<element key="a">A</element>'\
            '<element key="b">B</element>'\
            '<element key="c">c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def test_choice_serialized(self):
        """ Tests a regular choice, one with empty string term in vocab,
        and another with terms that have titles
        """
        choice = self._choice()
        choice_with_empty = self._choice_with_empty()
        choice_with_term_titles = self._choice_with_term_titles()
        for case in (choice, choice_with_empty, choice_with_term_titles):
            field, expected = case
            expected = six.binary_type(expected) if six.PY2 \
                else six.binary_type(expected, encoding='latin-1')
            el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
            self.assertEquals(etree.tostring(el), expected)

    def test_choice_parsing(self):
        def _termvalues(vocab):
            return tuple((t.value, t.title) for t in vocab)
        cases = (
            self._choice(),
            self._choice_with_empty(),
            self._choice_with_term_titles(),
            self._choice_with_term_titles_and_ns(),
        )
        for field, expected in cases:
            el = etree.fromstring(expected)
            imported_field = self.handler.read(el)
            self.assertEqual(
                _termvalues(imported_field.vocabulary),
                _termvalues(field.vocabulary),
            )
Esempio n. 5
0
 def setUp(self):
     configure()
     self.handler = ChoiceHandler(schema.Choice)
Esempio n. 6
0
class TestChoiceHandling(unittest.TestCase):
    def setUp(self):
        configure()
        self.handler = ChoiceHandler(schema.Choice)

    def _choice(self):
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t) for t in (u'a', u'b', u'c')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element><element>b</element><element>c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_empty(self):
        # add an empty string term to vocabulary
        vocab = SimpleVocabulary([SimpleTerm(t, title=t) for t in (u'a', u'')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element>'\
            '<element></element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_term_titles(self):
        # two terms with distinct titles, one with same as value:
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t.upper())
             for t in (u'a', u'b')] + [SimpleTerm(u'c', title=u'c')], )
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element key="a">A</element>'\
            '<element key="b">B</element>'\
            '<element key="c">c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_term_titles_and_ns(self):
        # two terms with distinct titles, one with same as value:
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t.upper())
             for t in (u'a', u'b')] + [SimpleTerm(u'c', title=u'c')], )
        expected = '<field name="myfield" type="zope.schema.Choice"'\
            '      xmlns="http://namespaces.plone.org/supermodel/schema">'\
            '<values>'\
            '<element key="a">A</element>'\
            '<element key="b">B</element>'\
            '<element key="c">c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_integers(self):
        vocab = SimpleVocabulary([SimpleTerm(1, title=u'One')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element key="1">One</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def test_choice_serialized(self):
        """ Tests a regular choice, one with empty string term in vocab,
        and another with terms that have titles
        """
        choice = self._choice()
        choice_with_empty = self._choice_with_empty()
        choice_with_term_titles = self._choice_with_term_titles()
        choice_with_integers = self._choice_with_integers()
        for case in (choice, choice_with_empty, choice_with_term_titles,
                     choice_with_integers):
            field, expected = case
            expected = six.binary_type(expected) if six.PY2 \
                else six.binary_type(expected, encoding='latin-1')
            el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
            self.assertEquals(etree.tostring(el), expected)

    def test_choice_parsing(self):
        def _termvalues(vocab):
            return tuple((t.value, t.title) for t in vocab)

        cases = (
            self._choice(),
            self._choice_with_empty(),
            self._choice_with_term_titles(),
            self._choice_with_term_titles_and_ns(),
        )
        for field, expected in cases:
            el = etree.fromstring(expected)
            imported_field = self.handler.read(el)
            self.assertEqual(
                _termvalues(imported_field.vocabulary),
                _termvalues(field.vocabulary),
            )
Esempio n. 7
0
class TestChoiceHandling(unittest.TestCase):
    def setUp(self):
        configure()
        self.handler = ChoiceHandler(schema.Choice)

    def _choice(self):
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t) for t in (u'a', u'b', u'c')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element><element>b</element><element>c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_empty(self):
        # add an empty string term to vocabulary
        vocab = SimpleVocabulary([SimpleTerm(t, title=t) for t in (u'a', u'')])
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element>a</element>'\
            '<element></element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def _choice_with_term_titles(self):
        # two terms with distinct titles, one with same as value:
        vocab = SimpleVocabulary(
            [SimpleTerm(t, title=t.upper())
             for t in (u'a', u'b')] + [SimpleTerm(u'c', title=u'c')], )
        expected = '<field name="myfield" type="zope.schema.Choice">'\
            '<values>'\
            '<element key="a">A</element>'\
            '<element key="b">B</element>'\
            '<element key="c">c</element>'\
            '</values>'\
            '</field>'
        return (schema.Choice(vocabulary=vocab), expected)

    def test_choice_serialized(self):
        field, expected = self._choice()
        el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
        self.assertEquals(etree.tostring(el), expected)
        # now with an empty string term in vocab:
        field, expected = self._choice_with_empty()
        el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
        self.assertEquals(etree.tostring(el), expected)
        # now with terms that have titles:
        field, expected = self._choice_with_term_titles()
        el = self.handler.write(field, 'myfield', 'zope.schema.Choice')
        self.assertEquals(etree.tostring(el), expected)

    def test_choice_parsing(self):
        _termvalues = lambda vocab: tuple((t.value, t.title) for t in vocab)
        cases = (
            self._choice(),
            self._choice_with_empty(),
            self._choice_with_term_titles(),
        )
        for field, expected in cases:
            el = etree.fromstring(expected)
            imported_field = self.handler.read(el)
            self.assertEquals(
                _termvalues(imported_field.vocabulary),
                _termvalues(field.vocabulary),
            )
Esempio n. 8
0
            rel_list = None

        if not rel_list:
            return []

        resolved_list = []
        for rel in rel_list:
            if rel.isBroken():
                # XXX: should log or take action here
                continue
            resolved_list.append(rel.to_object)
        return resolved_list

    def set(self, value):
        """Sets the relationship target"""
        value = value or []
        new_relationships = []
        intids = getUtility(IIntIds)
        for item in value:
            # otherwise create one
            to_id = intids.getId(item)
            new_relationships.append(RelationValue(to_id))
        super(RelationListDataManager, self).set(new_relationships)


# plone.supermodel schema import/export handlers

RelationChoiceHandler = ChoiceHandler(RelationChoice)
RelationListHandler = BaseHandler(RelationList)