Using pyramid + pyramid_simpleform + formencode + sqlalchemy ?

57 views
Skip to first unread message

André Prado

unread,
Apr 8, 2018, 4:50:41 PM4/8/18
to pylons-...@googlegroups.com
Hi folks,

I want to make a schema + a form connecting to SQLAlchemy and be able to identify if an username is unique and emails are unique. I am using these as a base:


For example,

class Signup(formencode.Schema):
    """Form fields to render and validate for signup."""

    allow_extra_fields=True
    filter_extra_fields=True
    username = UniqueUsername(not_empty=True)
    email = UniqueEmail(resolve_domain=False, not_empty=True)
    password = Password(not_empty=True)
    confirm = Password(not_empty=True)
    chained_validators = [
        validators.FieldsMatch(
            'password',
            'confirm'
        )
    ]


In the example from simpleauth he has a function called get_existing_user that UniqueUsername is calling in the _validate_python method.

However the get_existing_user is accessing a global SQLAlchemy session which is always exposed: https://github.com/thruflo/pyramid_basemodel/blob/master/src/pyramid_basemodel/__init__.py#L54

I wanted to use what the current Pyramid documentation suggests, which is, having a request.dbsession that you propagate through your code instead of a global variable. 



Is there any way to make pyramid_simpleform / formencode receive the request.dbsession? 

I couldn't figure out from the docs, tried some parameters like state and extra but no juice. In the end I did something like: 

from pyramid.threadlocal import get_current_request
request=get_current_request().dbsession

Which the Pyramid docs tells not to do and I don't want to.

Thanks!



--
Atenciosamente/Regards
André Castelan Prado

Michael Merickel

unread,
Apr 8, 2018, 6:30:08 PM4/8/18
to Pylons
Validation libraries tend to have a way to pass some user-defined state down through the validators. It looks like in formencode you can do this as well via the state argument [1]. You would likely want to pass a dict down containing either the request or the dbsession and then your validators can access that attribute of the state.


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAG%3D2wZeJ3SbCmFbfhdOvPE1aJ9f%3DPYrF8YAx%2BN5A7fWd%3DFhsMg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

André Prado

unread,
Apr 9, 2018, 1:53:27 AM4/9/18
to pylons-...@googlegroups.com
Yep that did the trick. I believe I was trying to pass a dict instead of an object last night when I hit the error. Oh well...

Thanks a bunch!

To post to this group, send email to pylons-...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages