Exploring the Flask web framework in Python

The Application

The application consists of the forms, models, and views in the app.py file (Listing 2). The first step is to import the necessary modules (lines  1 to 9). The Flask extensions mentioned above, such as SQL Alchemy [11] and Bootstrap [9], help to manage the database. As of line  11, the script creates several objects.

Listing 2

Import and Object Building

 

Models

The database consists of the two tables, users and ideas. For users, the application only stores a hash of the password in the users class (Listing  3) and checks it later. The application does not store passwords in clear text but generates a secure hash via the werkzeug.security.generate_password_hash() feature and verifies hashes with werkzeug.security.check_password_hash(). Werkzeug, a central component of Flask, provides both of these features. The Idea class (Listing 4) takes care of the ideas. Idea contains a reference to the user who created the idea, so Geistesblitze only shows the user their own flashes of genius next time up.

Listing 3

User Management

 

Listing 4

Collecting Ideas

 

Checking Forms

WT-Forms [13] generates three forms. The form framework defines the forms and also takes care of their validation.

Thanks to the WT-Forms RegisterForm (Listing 5), WT-Forms does not just check whether the two given passwords are the same, but also whether the username already exists in the database. The minimum length of the user name and password could be checked in this way, once the user is registered. Similar content validation checks are performed by the LoginForm and AddIdeaForm classes.

Listing 5

Form Validation

 

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Nix and NixOS

    NixOS and the Nix package manager offer a promising new approach to the challenge of managing packages in Linux.

  • Password Tools

    Create secure passwords with the help of a password generator and check for quality at the same time.

  • Pass Password Manager

    Pass is a simple shell script that helps you manage and synchronize passwords using Git.

  • Open-Xchange and Zarafa

    The Open-Xchange and Zarafa groupware systems can tap into the APIs of Facebook, Twitter, and Xing, but you need different tactics for each service – keep in mind that the information yield is sometimes quite meager.

  • Automated Web Logins

    Automated web logins with command-line tools and Selenium ensure you don't miss scheduling an activity.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News