Python werkzeug.datastructures.MultiDict() Examples

The following are 30 code examples of werkzeug.datastructures.MultiDict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module werkzeug.datastructures , or try the search function .
Example #1
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_not_member(self):
        Ownable, OwnableForm = self.factory()
        member = Member(user=UserFactory(), role='editor')
        org = OrganizationFactory(members=[member])

        login_user(UserFactory())

        form = OwnableForm(MultiDict({
            'organization': str(org.id)
        }))

        self.assertEqual(form.organization.data, org)

        self.assertFalse(form.validate())
        self.assertIn('organization', form.errors)
        self.assertEqual(len(form.errors['organization']), 1) 
Example #2
Source File: formparser.py    From jbox with MIT License 6 votes vote down vote up
def __init__(self, stream_factory=None, charset='utf-8', errors='replace',
                 max_form_memory_size=None, cls=None, buffer_size=64 * 1024):
        self.stream_factory = stream_factory
        self.charset = charset
        self.errors = errors
        self.max_form_memory_size = max_form_memory_size
        if stream_factory is None:
            stream_factory = default_stream_factory
        if cls is None:
            cls = MultiDict
        self.cls = cls

        # make sure the buffer size is divisible by four so that we can base64
        # decode chunk by chunk
        assert buffer_size % 4 == 0, 'buffer size has to be divisible by 4'
        # also the buffer size has to be at least 1024 bytes long or long headers
        # will freak out the system
        assert buffer_size >= 1024, 'buffer size has to be at least 1KB'

        self.buffer_size = buffer_size 
Example #3
Source File: formparser.py    From lambda-packs with MIT License 6 votes vote down vote up
def __init__(self, stream_factory=None, charset='utf-8', errors='replace',
                 max_form_memory_size=None, cls=None, buffer_size=64 * 1024):
        self.charset = charset
        self.errors = errors
        self.max_form_memory_size = max_form_memory_size
        self.stream_factory = default_stream_factory if stream_factory is None else stream_factory
        self.cls = MultiDict if cls is None else cls

        # make sure the buffer size is divisible by four so that we can base64
        # decode chunk by chunk
        assert buffer_size % 4 == 0, 'buffer size has to be divisible by 4'
        # also the buffer size has to be at least 1024 bytes long or long headers
        # will freak out the system
        assert buffer_size >= 1024, 'buffer size has to be at least 1KB'

        self.buffer_size = buffer_size 
Example #4
Source File: reqparse.py    From picoCTF with MIT License 6 votes vote down vote up
def source(self, request):
        """
        Pulls values off the request in the provided location
        :param request: The flask request object to parse arguments from
        """
        if isinstance(self.location, six.string_types):
            value = getattr(request, self.location, MultiDict())
            if callable(value):
                value = value()
            if value is not None:
                return value
        else:
            values = MultiDict()
            for l in self.location:
                value = getattr(request, l, None)
                if callable(value):
                    value = value()
                if value is not None:
                    values.update(value)
            return values

        return MultiDict() 
Example #5
Source File: test_model_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_valid_multidict(self):
        expected_target = Target.objects.create()
        model = self.model()
        form = self.form(MultiDict({
            'name': 'test',
            'target-class': 'Target',
            'target-id': str(expected_target.pk)
        }))

        form.validate()
        assert form.errors == {}

        form.populate_obj(model)

        assert isinstance(model.target, Target)
        assert model.target == expected_target 
Example #6
Source File: views.py    From flask-security with MIT License 6 votes vote down vote up
def send_login():
    """View function that sends login instructions for passwordless login"""

    form_class = _security.passwordless_login_form

    if request.is_json:
        form = form_class(MultiDict(request.get_json()), meta=suppress_form_csrf())
    else:
        form = form_class(meta=suppress_form_csrf())

    if form.validate_on_submit():
        send_login_instructions(form.user)
        if not _security._want_json(request):
            do_flash(*get_message("LOGIN_EMAIL_SENT", email=form.user.email))

    if _security._want_json(request):
        return base_render_json(form)

    return _security.render_template(
        config_value("SEND_LOGIN_TEMPLATE"), send_login_form=form, **_ctx("send_login")
    ) 
Example #7
Source File: views.py    From flask-security with MIT License 6 votes vote down vote up
def forgot_password():
    """View function that handles a forgotten password request."""

    form_class = _security.forgot_password_form

    if request.is_json:
        form = form_class(MultiDict(request.get_json()), meta=suppress_form_csrf())
    else:
        form = form_class(meta=suppress_form_csrf())

    if form.validate_on_submit():
        send_reset_password_instructions(form.user)
        if not _security._want_json(request):
            do_flash(*get_message("PASSWORD_RESET_REQUEST", email=form.user.email))

    if _security._want_json(request):
        return base_render_json(form, include_user=False)

    return _security.render_template(
        config_value("FORGOT_PASSWORD_TEMPLATE"),
        forgot_password_form=form,
        **_ctx("forgot_password")
    ) 
Example #8
Source File: test_current_user_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_other_user_admin(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        admin = AdminFactory()

        login_user(admin)

        form = OwnableForm(MultiDict({
            'owner': str(user.id)
        }))

        self.assertEqual(form.owner.data, user)

        form.validate()
        self.assertEqual(form.errors, {})

        ownable = Ownable()
        form.populate_obj(ownable)
        self.assertEqual(ownable.owner, user) 
Example #9
Source File: test_current_user_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_other_user(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        other = UserFactory()

        login_user(user)

        form = OwnableForm(MultiDict({
            'owner': str(other.id)
        }))

        self.assertEqual(form.owner.data, other)

        form.validate()
        self.assertIn('owner', form.errors)
        self.assertEqual(len(form.errors['owner']), 1) 
Example #10
Source File: test_current_user_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_valid_user_self(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()

        login_user(user)

        form = OwnableForm(MultiDict({
            'owner': str(user.id)
        }))

        self.assertEqual(form.owner.data, user)

        form.validate()
        self.assertEqual(form.errors, {})

        ownable = Ownable()
        form.populate_obj(ownable)
        self.assertEqual(ownable.owner, user) 
Example #11
Source File: test_nested_model_list_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def factory(self, data=None, instance=None, id=True, sub=False, **kwargs):
        if sub:
            nested_form = NestedFormWithSub
        elif id:
            nested_form = NestedFormWithId
        else:
            nested_form = NestedForm

        class FakeForm(ModelForm):
            model_class = Fake
            name = fields.StringField()
            nested = fields.NestedModelList(nested_form, **kwargs)

        if isinstance(data, MultiDict):
            return FakeForm(data, obj=instance, instance=instance)
        else:
            return FakeForm.from_json(data, obj=instance, instance=instance) 
Example #12
Source File: test_daterange_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_valid_dates(self):
        Fake, FakeForm = self.factory()
        start = date.today() - timedelta(days=1)
        end = date.today()
        expected = ' - '.join([to_iso_date(start), to_iso_date(end)])

        fake = Fake()
        form = FakeForm(MultiDict({
            'daterange': expected
        }))

        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(fake)

        self.assertEqual(fake.daterange, db.DateRange(start=start, end=end)) 
Example #13
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_initial_owner_and_no_data_provided(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        ownable = Ownable(owner=user)

        form = OwnableForm(MultiDict({}), obj=ownable)

        self.assertEqual(form.owner.data, user)

        login_user(user)
        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(ownable)
        self.assertEqual(ownable.owner, user)
        self.assertIsNone(ownable.organization) 
Example #14
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_initial_org_and_no_data_provided(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        org = OrganizationFactory(members=[Member(user=user, role='editor')])
        ownable = Ownable(organization=org)

        form = OwnableForm(MultiDict({}), obj=ownable)

        self.assertEqual(form.organization.data, org)

        login_user(user)
        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(ownable)
        self.assertIsNone(ownable.owner)
        self.assertEqual(ownable.organization, org) 
Example #15
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_valid_organization(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        member = Member(user=user, role='editor')
        org = OrganizationFactory(members=[member])

        login_user(user)

        form = OwnableForm(MultiDict({
            'organization': str(org.id)
        }))

        self.assertEqual(form.owner.data, user)
        self.assertEqual(form.organization.data, org)

        form.validate()
        self.assertEqual(form.errors, {})

        self.assertIsNone(form.owner.data)
        self.assertEqual(form.organization.data, org)

        ownable = Ownable()
        form.populate_obj(ownable)
        self.assertIsNone(ownable.owner)
        self.assertEqual(ownable.organization, org) 
Example #16
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_admin_can_set_organization(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        org = OrganizationFactory()
        ownable = Ownable(owner=user)

        login_user(AdminFactory())

        form = OwnableForm(MultiDict({
            'organization': str(org.id)
        }), obj=ownable)

        self.assertEqual(form.owner.data, user)
        self.assertEqual(form.organization.data, org)

        self.assertTrue(form.validate())
        self.assertEqual(form.errors, {})

        self.assertIsNone(form.owner.data)
        self.assertEqual(form.organization.data, org)

        form.populate_obj(ownable)
        self.assertIsNone(ownable.owner)
        self.assertEqual(ownable.organization, org) 
Example #17
Source File: formparser.py    From RSSNewsGAE with Apache License 2.0 6 votes vote down vote up
def __init__(self, stream_factory=None, charset='utf-8', errors='replace',
                 max_form_memory_size=None, cls=None, buffer_size=64 * 1024):
        self.charset = charset
        self.errors = errors
        self.max_form_memory_size = max_form_memory_size
        self.stream_factory = default_stream_factory if stream_factory is None else stream_factory
        self.cls = MultiDict if cls is None else cls

        # make sure the buffer size is divisible by four so that we can base64
        # decode chunk by chunk
        assert buffer_size % 4 == 0, 'buffer size has to be divisible by 4'
        # also the buffer size has to be at least 1024 bytes long or long headers
        # will freak out the system
        assert buffer_size >= 1024, 'buffer size has to be at least 1KB'

        self.buffer_size = buffer_size 
Example #18
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_admin_can_set_owner(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        org = OrganizationFactory()
        ownable = Ownable(organization=org)

        login_user(AdminFactory())

        form = OwnableForm(MultiDict({
            'owner': str(user.id)
        }), obj=ownable)

        self.assertEqual(form.owner.data, user)
        self.assertEqual(form.organization.data, org)

        self.assertTrue(form.validate())
        self.assertEqual(form.errors, {})

        self.assertEqual(form.owner.data, user)
        self.assertIsNone(form.organization.data)

        form.populate_obj(ownable)
        self.assertEqual(ownable.owner, user)
        self.assertIsNone(ownable.organization) 
Example #19
Source File: views.py    From flask-security with MIT License 6 votes vote down vote up
def send_confirmation():
    """View function which sends confirmation instructions."""

    form_class = _security.send_confirmation_form

    if request.is_json:
        form = form_class(MultiDict(request.get_json()), meta=suppress_form_csrf())
    else:
        form = form_class(meta=suppress_form_csrf())

    if form.validate_on_submit():
        send_confirmation_instructions(form.user)
        if not _security._want_json(request):
            do_flash(*get_message("CONFIRMATION_REQUEST", email=form.user.email))

    if _security._want_json(request):
        return base_render_json(form)

    return _security.render_template(
        config_value("SEND_CONFIRMATION_TEMPLATE"),
        send_confirmation_form=form,
        **_ctx("send_confirmation")
    ) 
Example #20
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_initial_and_both_member(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        org = OrganizationFactory(members=[Member(user=user, role='editor')])
        neworg = OrganizationFactory(
            members=[Member(user=user, role='editor')])
        ownable = Ownable(organization=org)

        form = OwnableForm(MultiDict({
            'organization': str(neworg.id)
        }), obj=ownable)

        self.assertEqual(form.organization.data, neworg)

        login_user(user)
        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(ownable)
        self.assertIsNone(ownable.owner)
        self.assertEqual(ownable.organization, neworg) 
Example #21
Source File: test_publish_as_field.py    From udata with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_with_initial_and_not_member(self):
        Ownable, OwnableForm = self.factory()
        user = UserFactory()
        org = OrganizationFactory(members=[Member(user=user, role='editor')])
        neworg = OrganizationFactory()
        ownable = Ownable(organization=org)

        form = OwnableForm(MultiDict({
            'organization': str(neworg.id)
        }), obj=ownable)

        self.assertEqual(form.organization.data, neworg)

        login_user(user)
        form.validate()
        self.assertIn('organization', form.errors)
        self.assertEqual(len(form.errors['organization']), 1) 
Example #22
Source File: test_model_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_identifier_only_multidict(self):
        expected_target = Target.objects.create()
        model = self.model()
        form = self.form(MultiDict({
            'name': 'test',
            'target-id': str(expected_target.pk),
        }))

        form.validate()
        assert form.errors == {}

        form.populate_obj(model)

        assert isinstance(model.target, Target)
        assert model.target == expected_target 
Example #23
Source File: globals.py    From quart with MIT License 5 votes vote down vote up
def form(self) -> MultiDict:
        return sync_with_context(self._get_current_object().form) 
Example #24
Source File: test_model_list_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_multiple_valid_data(self):
        nesteds = [Nested.objects.create(name=faker.name()) for _ in range(3)]
        ids = [str(n.id) for n in nesteds]
        fake = Fake()
        form = FakeForm(MultiDict({'nested': ','.join(ids)}))

        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(fake)

        self.assertEqual(len(fake.nested), len(nesteds))
        self.assertIsInstance(fake.nested[0], Nested)
        self.assertEqual(fake.nested, nesteds) 
Example #25
Source File: test_reference_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_valid_form_data_id_only(self):
        nested = Nested.objects.create(name=faker.name())
        form = WithGenericChoicesForm(MultiDict({
            'nested': str(nested.id)
        }))

        form.validate()
        self.assertIn('nested', form.errors)
        self.assertEqual(len(form.errors['nested']), 1) 
Example #26
Source File: test_uuid_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_valid_uuid_string(self):
        Fake, FakeForm = self.factory()

        fake = Fake()
        id = uuid.uuid4()
        form = FakeForm(MultiDict({
            'id': str(id)
        }))

        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(fake)

        self.assertEqual(fake.id, id) 
Example #27
Source File: test_uuid_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_invalid_uuid_string(self):
        Fake, FakeForm = self.factory()

        form = FakeForm(MultiDict({'id': 'not-an-uuid'}))

        form.validate()
        self.assertIn('id', form.errors)
        self.assertEqual(len(form.errors['id']), 1) 
Example #28
Source File: test_form_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def factory(self, data=None, instance=None, id=True, **kwargs):
        if id:
            nested_form = NestedFormWithId
        else:
            nested_form = NestedForm

        class FakeForm(ModelForm):
            model_class = Fake
            name = fields.StringField()
            nested = fields.FormField(nested_form, **kwargs)

        if isinstance(data, MultiDict):
            return FakeForm(data, obj=instance, instance=instance)
        else:
            return FakeForm.from_json(data, obj=instance, instance=instance) 
Example #29
Source File: test_reference_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_valid_form_data_id_only(self):
        nested = Nested.objects.create(name=faker.name())
        model = WithReference()
        form = WithReferenceForm(MultiDict({
            'nested': str(nested.id)
        }))

        form.validate()
        self.assertEqual(form.errors, {})
        self.assertEqual(form.nested.data, nested)

        form.populate_obj(model)

        self.assertIsInstance(model.nested, Nested)
        self.assertEqual(model.nested, nested) 
Example #30
Source File: test_dict_field.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_with_valid_data(self):
        Fake, FakeForm = self.factory()

        now = datetime.now()
        today = date.today()

        fake = Fake()
        form = FakeForm(MultiDict({'raw': {
            'integer': 42,
            'float': 42.0,
            'string': 'value',
            'datetime': now,
            'date': today,
            'bool': True,
            'dict': {'key': 'value'}
        }}))

        form.validate()
        assert form.errors == {}

        form.populate_obj(fake)

        assert fake.raw == {
            'integer': 42,
            'float': 42.0,
            'string': 'value',
            'datetime': now,
            'date': today,
            'bool': True,
            'dict': {'key': 'value'}
        }