Skip to main content

Create WTF-Form from your pony database entity

Project description

Pony-WTF is an integration library for Ponyorm and WTF-Forms.

Installation

pip install pony-wtf

Usage

# imports from pony-wtf
from pony_wtf import model_form
from pony_wtf.utils import get_attrs_dict


# declare database entities
class User(db.Entity):
    id = PrimaryKey(int, auto=True)
    email = Required(str, unique=True)
    age = Optional(int)

form_cls = model_form(User, )
form = form_cls()

def get_form_values(entity, form_data):
    """ Get form values and return them as dict """
    d = {}
    adict = get_attrs_dict(entity)
    for k in adict:
        if k in form_data:
            d[k] = form_data.get(k)
    return d


@app.route('/create_user', methods=['GET', 'POST'])
def create_user():
    form_cls = model_form(User)
    form = form_cls()  # instaniatte the class

    if request.method == 'POST' and form.validate_on_submit():
        data = get_form_values(User, form.data)
        with db_session:
            User(**data)
        flash("User Created", "success")
        return redirect(url_for("index"))
    return render_template('create_user.html',  form=form)

You can render the form as you prefer in the jinjaHTML. Ex: {# Use Flask-WTF and Flask-Bootstrap #} {{ wtf.quick_form(form) }}

Requirements

pony
pony_wtf
Flask_WTF
WTForms

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

[MIT]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pony_wtf-0.0.1.post3.tar.gz (8.5 kB view details)

Uploaded Source

File details

Details for the file pony_wtf-0.0.1.post3.tar.gz.

File metadata

  • Download URL: pony_wtf-0.0.1.post3.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for pony_wtf-0.0.1.post3.tar.gz
Algorithm Hash digest
SHA256 4a378a8e81ebdc08c0bca1f78a1c2872fada8edf2c809ad8c77e564892363019
MD5 b81709c645dbff03b74a5a937ced7ceb
BLAKE2b-256 ff7690e8a538ba25788cf39951035f5aa9ec58b5b973aaac8df26b21cfa56a12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page