Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add wtforms #15

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ zip_safe = true

[options.extras_require]
contrib_click = click
contrib_wtforms =
wtforms
flask-wtf

[options.packages.find]
where = src
Expand Down
29 changes: 29 additions & 0 deletions src/easy_config/contrib/wtforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Test the Flask-WTF wrapper."""

import dataclasses
from typing import Type

from easy_config import EasyConfig
from flask_wtf import FlaskForm
from wtforms import BooleanField, IntegerField, StringField
from wtforms.validators import DataRequired

type_to_field = {
str: StringField,
int: IntegerField,
bool: BooleanField,
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can WTForms only handle these three datatypes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does other things.. this is not anywhere near complete (but at least is a proof-of-concept)



def form_from_config(cls: Type[EasyConfig]):
"""Build a Flask-WTF form based on the given EasyConfig class."""
attrs = {}

for field in dataclasses.fields(cls):
field_cls = type_to_field[field.type]
attribute = field_cls(field.name, validators=[DataRequired()])
if field.default is not dataclasses.MISSING:
attribute.default = field.default
attrs[field.name] = attribute

return type(f'{cls.NAME}Form', (FlaskForm,), attrs)
35 changes: 35 additions & 0 deletions tests/test_contrib_wtforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-

"""Test the Flask-WTF wrapper."""

import flask

from easy_config import EasyConfig
from easy_config.contrib.wtforms import form_from_config


def test_with_default():
"""Test building a form."""

class ExampleConfig(EasyConfig):
"""Example EasyConfig subclass to test with."""

FILES = None
NAME = 'MyProgram'

bool_1: bool
bool_2: bool = True
bool_3: bool = True

form_cls = form_from_config(ExampleConfig)
app = flask.Flask('test_app')

with app.app_context():
form = form_cls(csrf_enabled=False)
for field in form:
print(field)

fields = list(form)
assert 1 == len(fields)
field = fields[0]
assert str(field) == '<input id="number" name="number" required type="checkbox" value="y">'
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ deps =
pytest
extras =
contrib_click
contrib_wtforms
commands = coverage run --parallel -m pytest tests {posargs}
description = Run pytest tests with coverage.

Expand Down