views.py

The views module handles the routing for all recipe viewing and editing related pages. Flask uses decorators to define the routes, and the decorated function handles the POST or GET as applicable.

Routing Functions

POST /form_edit_photo

Updates the image in the database

Parameters
  • rid – recipe id

  • file – image as files object

GET /form_edit_photo

Displays the edit photo template

Returns

render template form_edit_photo.html

POST /form_edit

Updates recipe information in the database

Parameters
  • cmd_delete – ‘1’ if deleting the recipe

  • conf – ‘confirm’ if delete is confirmed

  • ID – recipe id

  • title – recipe title

  • ingr – recipe ingredients

  • dirs – recipe directions

  • cat – recipe category

  • book_id – book id that contains recipe

  • new_book_name – optional name of a new book

  • new_book_desc – optional description of new book

GET /form_edit

Displays the edit form

Returns

render template form_edit.html

POST /form_add

Adds a new recipe to the database

Parameters
  • title – title

  • ingr – ingredients

  • dir – directions

  • category – recipe category

  • book_id – book id

  • new_book_name – optional name of a new book

  • new_book_desc – optional description of new book

GET /form_add

Displays the recipe add form

Returns

render template form_add.html

Displays the search page for all recipes in database

Returns

render template search.html

GET /open

Displays book title, description, author and the recipes contained therein

Returns

render template open_book.html

GET /

Displays the home page

Returns

render template index.html

GET /display/(int: rid)

Displays a recipe

Parameters
  • rid – recipe id as int

Returns

render template display.html

GET /static/(path: filename)

Function used internally to send static files from the static folder to the browser.

New in version 0.5.

Session Helper Functions

These assist with session handling. Note that since views is a blueprint stored in the views directory (views/views.py), the function prototypes have the format views.views.<function>

views.views.session_email()

Retrieves ‘username’ from the session, which is the user’s email address

Returns

user email (str) if logged in, else returns None

views.views.session_fullname()

Retrieves the user’s full name (first last) from the session

Returns

User’s full name (str) if logged in else returns None

views.views.session_user_id()

Retrieves the user’s ID number. This is the same number as used in the users table of the database

Returns

user id (int)


Example usage

user_email = session_email()
full_name = session_fullname()
user_id = session_user_id()
# If we are not logged in (username is None) then redirect the user to login
if not (user_email and user_id):
    flash('Please login to add a new recipe')
    return flask.redirect(url_for('auth.login'))