Skip to content

klazich/catalog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Catalog App

A simple web app showcasing flask with back-end SQLite database integration.

Table of Contents

Installation/Requirements

Requirements

Catalog App will work in Python 2 and Python 3. But the optional data faking functions in module catalog/populate.py will not work in Python 2.

Install/Setup

  1. Clone the Repository

    ➜ git clone https://github.com/klazich/catalog.git project
    ➜ cd project
    
  2. Install Packages

    ➜ pip install Flask Flask-WTF Flask-Restless SQLAlchemy requests_oauthlib
    

    If you are using Python 3 and want to use the apps database populating features, install mimesis as well...

     ➜ pip3 install Flask Flask-WTF Flask-Restless SQLAlchemy requests_oauthlib mimesis
    

    or with requirements.txt...

    ➜ pip install -r requirements.txt
    
  3. Start the Server

    The flask app will load the "development" configs by default (see config.py). The development config turns on debug. Load different flask configs by setting the FLASK_CONFIG environment variable to dev, test, or prod.

    ➜ export FLASK_CONFIG=prod
    

    or with a python script...

    >>> import os
    >>> os.environ['FLASK_CONFIG'] = 'prod'  # set Flask to load production configurations
    

    to start the server enter...

    ➜ python run.py
    

    then open up a browser to http://localhost:5000/

Populating the Database

Using Mimesis and helper functions from catalog/populate.py we can seed the database with fake data. This is useful for testing as well as demonstration purposes.

catalog.populate requires Python 3 or later. If you try to import the module with Python 2 the app will raise a AssertionError. The populate functions require the Mimesis package and Mimesis will fail on import with Python 2.

To populate all the tables in the database use populate_db.

➜ python3

>>> from catalog.populate import populate_db
>>> populate_db()

Note - Be aware that populate_db will call drop_db which will drop all the database tables.

  • populate_db() will create the Item, Category and User tables (catalog/models.py) and populate them with simulated data using the other function is the database module.

Use init_db and drop_db to create and drop tables, respectively.

➜ python3

>>> from catalog.populate import init_db, drop_db
>>> init_db()  # to create database tables
>>> drop_db()  # to drop all tables in database
  • init_db() will create all tables found in the metadata if they are not already created.
  • drop_db() will drop all tables in the database.

To populate individual tables use populate_users(n), populate_categories() and populate_items(n).

➜ python3

>>> from catalog.populate import populate_users, populate_categories, populate_items
>>> populate_users(10)     # add 10 User objects to database
>>> populate_categories()  # add Category objects to database
>>> populate_items(90)     # add 90 Item objects to database
  • populate_users(n) will create and commit n users to the database (defaults to n=100).
  • populate_categories() will create and commit the simulated categories.
  • populate_items(n) will create and commit n items to the database (defaults to n=600).

URL Table

Request URL Path View Function/Flask Extension
/catalog catalog.index() Renders site index, listing categories
/auth/login auth.login() Renders the login page
/auth/logout auth.logout() Logs out user and redirects to referrer
/auth/{provider} auth.oauth2_authorize(provider) Initiates user authentication request to provider OAuth2 service
/auth/callback auth.oauth2_callback() Handles the callback from provider OAuth2 service and redirects to auth.login referrer
/category/{id} category.read(id) Renders a list of items from category with id (extends catalog.index)
/item/new item.create() Renders the ItemForm for item creation
/item/{id} item.read(id) Renders a summary of an item with id (extends category.read)
/item/{id}/update item.update(id) Renders the ItemForm for item with id updates
/item/{id}/delete item.delete(id) Removes item with id from database
/api/categories Flask-Restless Returns a list of all category objects in JSON format
/api/categories/{id} Flask-Restless Returns an individual category object with id in JSON format
/api/items Flask-Restless Returns a list of all item objects in JSON format
/api/items/{id} Flask-Restless Returns an individual item object with id in JSON format

TODO

  • Move the populate database functions into a cli.

About

Udacity FSND "Build an Item Catalog" Project

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published