예제 #1
0
def get_serializer(definition_schema, context=None):
    """
    Return a FormidableSerializer instance, including eventual context.
    """
    from formidable.serializers import FormidableSerializer
    serializer = FormidableSerializer(data=definition_schema)
    # pass context to serializer so we can use it during data validation
    serializer.context.update(context or {})  # If None, will default to {}
    return serializer
예제 #2
0
    def from_json(definition_schema, **kwargs):
        """
        Proxy static method to create an instance of ``Formidable`` more
        easily with a given ``definition_schema``.

        :params definition_schema: Schema in JSON/dict

        >>> Formidable.from_json(definition_schema)
        <Formidable: Formidable object>

        """
        from formidable.serializers import FormidableSerializer

        serializer = FormidableSerializer(data=definition_schema)
        if serializer.is_valid():
            return serializer.save(**kwargs)

        raise ValidationError(serializer.errors)
예제 #3
0
 def test_empty_string_defaults(self):
     schema = {
         "label":
         "test",
         "description":
         "test",
         "fields": [{
             "slug":
             "custom-type-id",
             "label":
             "Custom field",
             "placeholder":
             None,
             "description":
             None,
             "defaults": [],
             "multiple":
             False,
             "values": [],
             "required":
             False,
             "disabled":
             False,
             "isVisible":
             True,
             "type_id":
             "text",
             "validations": [],
             "accesses": [{
                 "id": "field-access868",
                 "level": "EDITABLE",
                 "access_id": "padawan"
             }]
         }]
     }
     # to make sure we know the use-case we want to test.
     schema['fields'][0]['defaults'] = [""]
     serializer = FormidableSerializer(data=schema)
     assert serializer.is_valid(), serializer.errors
    def test_create_description_via_serializer(self):
        _form_data = copy.deepcopy(form_data)
        _form_data['description'] = XSS

        serializer = FormidableSerializer(data=_form_data)
        serializer.is_valid()
        serializer.save()
        formidable = serializer.instance
        assert formidable.description == XSS_RESULT
    def test_create_label_via_serializer(self):
        _form_data = copy.deepcopy(form_data)
        _form_data['label'] = XSS

        serializer = FormidableSerializer(data=_form_data)
        serializer.is_valid()
        serializer.save()
        formidable = serializer.instance
        assert formidable.label == XSS_RESULT
    def test_create_field_placeholder_via_serializer(self):
        _form_data = copy.deepcopy(form_data)
        _form_data['fields'][0]['placeholder'] = XSS

        serializer = FormidableSerializer(data=_form_data)
        serializer.is_valid()
        serializer.save()
        formidable = serializer.instance
        field = formidable.fields.first()
        assert field.placeholder == XSS_RESULT
예제 #7
0
 def test_context_in_field_serializer(self):
     # backup a serializer
     backup_serializer = self.field_register[self.custom_type_id]
     # mock a field serializer
     mocked_serializer = mock.MagicMock()
     self.field_register[self.custom_type_id] = mock.MagicMock(
         return_value=mocked_serializer)
     # test serializer with an empty context
     serializer = FormidableSerializer(data=self.schema)
     serializer.is_valid()
     self.assertEqual(mocked_serializer.custom_context, {})
     # test serializer with context
     serializer = FormidableSerializer(data=self.schema,
                                       context={'test': 'context'})
     serializer.is_valid()
     self.assertEqual(mocked_serializer.custom_context, {'test': 'context'})
     # remove mock and revert serializer
     self.field_register[self.custom_type_id] = backup_serializer
    def test_create_field_defaults_via_serializer(self):
        _form_data = copy.deepcopy(form_data)
        _form_data['fields'][0]['defaults'] = [XSS]

        serializer = FormidableSerializer(data=_form_data)
        serializer.is_valid()
        serializer.save()
        formidable = serializer.instance
        field = formidable.fields.first()
        default = field.defaults.first()
        assert default.value == XSS_RESULT
    def test_create_field_description_via_serializer(self):
        _form_data = copy.deepcopy(form_data)
        _form_data['fields'][0]['description'] = XSS

        serializer = FormidableSerializer(data=_form_data)
        serializer.is_valid()
        serializer.save()
        formidable = serializer.instance
        field = formidable.fields.first()
        # For historical reasons, help_text is mapped to description
        assert field.help_text == XSS_RESULT
    def test_create_field_via_serializer(self):
        _form_data = copy.deepcopy(form_data)
        BASE_INSTRUCTIONS = "<p>Instructions to fill the form</p>\n"
        _form_data['fields'][0] = {
            "validations": [],
            "slug": "instructions",
            "description": BASE_INSTRUCTIONS + XSS,
            "placeholder": None,
            "type_id": "help_text",
            "defaults": [],
            "accesses": []
        }

        serializer = FormidableSerializer(data=_form_data)
        serializer.is_valid()
        serializer.save()
        formidable = serializer.instance
        field = formidable.fields.first()
        assert field.help_text == BASE_INSTRUCTIONS + XSS_RESULT
예제 #11
0
 def test_custom_field_serialize(self):
     serializer = FormidableSerializer(data=self.schema)
     assert serializer.is_valid()
     serializer.save()
     # get field instance
     instance = serializer.instance
     custom_field = serializer.instance.fields.first()
     # test field instance
     self.assertIn('meta_info', custom_field.parameters)
     self.assertEqual(custom_field.parameters['meta_info'], "meta")
     self.assertIn('some_another_data', custom_field.parameters)
     self.assertEqual(custom_field.parameters['some_another_data'],
                      "some_another_data")
     # get serialized data
     data = FormidableSerializer(serializer.instance).data['fields'][0]
     # test serialized data
     self.assertIn('meta_info', data)
     self.assertIn('some_another_data', data)
     self.assertIn('parameters', data)
     # remove instance
     instance.delete()
예제 #12
0
    def test_custom_field_serialize_contextserializer(self):
        # Create a Formidable form using the schema
        serializer = FormidableSerializer(data=self.schema)
        assert serializer.is_valid()
        serializer.save()
        # get form instance
        instance = serializer.instance

        # Convert it into a "padawan" form
        serializer = ContextFormSerializer(instance,
                                           context={'role': 'padawan'})

        # get field instance
        fields = serializer['fields'].value
        custom_field = fields[0]
        # test field instance
        self.assertIn('meta_info', custom_field['parameters'])
        self.assertEqual(custom_field["parameters"]['meta_info'], "meta")
        self.assertIn('some_another_data', custom_field["parameters"])
        self.assertEqual(custom_field["parameters"]['some_another_data'],
                         "some_another_data")

        # remove instance
        instance.delete()
예제 #13
0
 def to_json(self):
     from formidable.serializers import FormidableSerializer
     json_data = FormidableSerializer(self).data
     return json_data