# Table of Contents
<!-- vim-markdown-toc GFM -->

* [Overview](#overview)
* [Setting up the environment](#setting-up-the-environment)
    * [Install the requirements](#install-the-requirements)
    * [Setup the environment variables](#setup-the-environment-variables)
* [Database](#database)
    * [Workflow for updating database (new table/field/etc)](#workflow-for-updating-database-new-tablefieldetc)
    * [Initial DB Setup for the app](#initial-db-setup-for-the-app)
    * [Working with the database](#working-with-the-database)
        * [Adding to the database](#adding-to-the-database)
        * [Querying the database](#querying-the-database)
        * [Clear out the tables](#clear-out-the-tables)

<!-- vim-markdown-toc -->

# Overview
This repo contains a simple flasks application. The application provides a simple web GUI with routes and a template.


# Setting up the environment

## Install the requirements
```
pip install -r requirements.txt
```

## Setup the environment variables
The following can be added to a `.env` file in the foot of this directory as well

```bash
APP_ENVIRONMENT="PROD"
SECRET_KEY="myverysecretkey"
DATABASE_URL="sqlite:///app.db"
```



# Database

## Workflow for updating database (new table/field/etc)
1. Export the flask app: `export FLASK_APP=stockpyle.py`
2. Create/modify class for table in `app/models.py`
3. Create migration script: `flask db migrate -m "<class/table> table"`
4. Commit migration script to repo (for upgrading other environments)
5. Make the changes to the database: `flask db upgrade`


## Initial DB Setup for the app
```bash
flask db upgrade
```


## Working with the database

### Adding to the database
```python
u = User(username='john', email='john@example.com')
u.set_password('password')
db.session.add(u)

u = User(username='susan', email='susan@example.com')
u.set_password('password')
db.session.add(u)

db.session.commit()
```

### Querying the database
```python
# get user
u = User.query.get(1)
u
#<User john>

# get all users in reverse alphabetical order
User.query.order_by(User.username.desc()).all()
#[<User susan>, <User john>]
```

### Clear out the tables
```python
users = User.query.all()
for u in users:
    db.session.delete(u)

db.session.commit()
```