==============================================
Status: **Early Development, Unstable, Unpublished**.
Python Version: **2.7** (please contribute to `FormEncode Project`_ in order to make it 3.x compatible).
Installation
--------------
.. code-block:: bash
pip install pyramid_webforms
Example
-------------
Consider the following pyramid project structure:
.. code-block:: plain
my_pyramid_app/
modules/
signin/
__init__.py
forms.py
validators.py
views.py
__init__.py
templates/
signin.mako
__init__.py
Let's define a sign-in form, its fields, and validators.
.. code-block:: python
# my_pyramid_app/modules/signin/forms.py
from pyramid_webforms import Form
from my_pyramid_app.i18n import _
from . import validators
login_or_email = {
'type': 'text',
'title': _('Login or Email'),
'tip': _('Please enter your login or email that was used during your registration.'),
'size': 30,
'maxlength': 50,
'validator': validators.UserLoginOrEmail
}
password = {
'type': 'password',
'title': _('Password'),
'tip': _('A password can contain any character of any alphabet (minimum is 1, maximum is 64 characters). '
'For reliability we recommend using non-trivial and long passwords. Note that the case of '
'the letters matters.'),
'size': 30,
'maxlength': 64,
'validator': validators.UserPassword,
'value': '',
}
remember_me = {
'type': 'checkbox',
'title': _('Remember me'),
'tip': _('Set this checkbox if you want your current browser to keep '
'your session for further visits.'),
'selected': False,
'validator': validators.RememberUserSession
}
class SignInForm(Form):
# form attributes and metadata
_id_ = 'signin-form'
_submit_text_ = _('Sign in')
_alternate_url_ = {'name': 'support.account_access'}
_alternate_text_ = _("I cannot access my account")
_fieldsets_ = [
[['login_email', 'password', 'remember_me']]
]
# form fields
login_email = login_or_email
password = password
remember_me = remember_me
.. code-block:: python
# my_pyramid_app/modules/signin/validators.py
import re
import formencode
# logins are 3-16 characters long
USERLOGINS = re.compile('[A-Za-z0-9][-A-Za-z0-9]{1,14}[A-Za-z0-9]', re.IGNORECASE)
RememberUserSession = formencode.validators.Bool
class UserLogin(formencode.validators.Regex):
not_empty = True
strip = True
regex = USERLOGINS
class UserEmail(formencode.validators.Email):
not_empty = True
strip = True
max = 50
def _to_python(self, email, state):
email = super(UserEmail, self)._to_python(email, state)
return email.lower()
class UserLoginOrEmail(UserLogin):
def _to_python(self, value, state):
if '@' in value:
validator = UserEmail
else:
validator = UserLogin
value = validator.to_python(value, state)
return value
def validate_python(self, value, state):
pass
class UserPassword(formencode.validators.UnicodeString):
not_empty = True
max = 64
Now we can use our form in pyramid view callables.
.. code-block:: python
# my_pyramid_app/modules/signin/views.py
from pyramid.view import view_config
from .forms import SignInForm
class SignInView(object):
@view_config(route_name='session.signin', renderer='templates/signin.mako')
def signin_form(self):
request = self.request
if request.POST:
try:
form = SignInForm.validate(request)
except SignInForm.Invalid as error:
# redirect or error handling
pass
else:
# sign in user using form data
pass
return {'signin_form': forms.SignInForm()}
.. code-block:: mako
## my_pyramid_app/templates/signin.mako
${signin_form(request)}
Here are the key conceptual points:
- form fields are defined with plain dictionaries;
- the fields can be reused by any other module;
- each field record contains an assigned FormEncode-based validator;
- a form is defined with the simple declarative interface.
Configuration options
-----------------------
+---------------------------------------+------------+----------------------------------------------------------+
| Key | Type | Default |
+=======================================+============+==========================================================+
| pyramid_webforms.submit_tpl | str | pyramid_webforms:templates/submit_alternate.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.submit_alternate_tpl | str | pyramid_webforms:templates/submit_alternate.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.form_tpl | str | pyramid_webforms:templates/form.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.fieldset_tpl | str | pyramid_webforms:templates/fieldset.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.field_tpl | str | pyramid_webforms:templates/field.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.tooltip_tpl | str | pyramid_webforms:templates/tooltip.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.form_error_tpl | str | pyramid_webforms:templates/form_error.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
| pyramid_webforms.field_error_tpl | str | pyramid_webforms:templates/field_error.p_wf_mako |
+---------------------------------------+------------+----------------------------------------------------------+
See also
============
- `FormEncode Project`_
- `WebHelpers Project`_
.. _FormEncode Project: https://github.com/formencode/formencode
.. _WebHelpers Project: http://sluggo.scrapping.cc/python/WebHelpers/index.html
Metadata
Release files for pyramid_webforms 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyramid_webforms-0.0.2.tar.gz | 13.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyramid_webforms-0.0.2-py2.7.egg | Legacy Egg format | - | - | Details |
Total release size: 33.5 kB
Release files / pyramid_webforms-0.0.2.tar.gz
| Download URL | pyramid_webforms-0.0.2.tar.gz |
|---|---|
| Size | 13.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21fd81ae404405e970194fdb8e6fae6277ab7caf8a24b231df6318f379962130
|
|
BLAKE2b-256 checksum How to use checksums |
eb1f77999192a5bdd261b09692c28c96d686ba9a599c284816819aee0b5d2f1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pyramid_webforms-0.0.2-py2.7.egg
| Download URL | pyramid_webforms-0.0.2-py2.7.egg |
|---|---|
| Size | 19.9 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
78d911842700f54ae4b13f6e611042781e63d6f6410ed9bc616f5fb711a49e42
|
|
BLAKE2b-256 checksum How to use checksums |
1c31b068b6fb1a06f86ac4e0ae493648190e08d13781f80489184e92b5d0fb04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pyramid_webforms 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.