This is the WMACM hardware checkout system
Built with Flask, SQLAlchemy, Flask-WTF, Flask-Dance, and Flask-Login
A number of environment variables are required to run this application:
The URL of the database we should connect to. See the SQLAlchemy Docs on Database URLs
Defaults to sqlite:///test.db
for testing purposes. Should use a real DB
in production.
A long random string of bytes used for securing session cookies. See the Flask documentation on sessions and SECRET_KEY
The directory under which all pages are hosted. If you want to host
this application at the root directory, you should leave this variable unset.
E.g. if you wanted
to run this on example.com/hardware
you would set BASEURL
to /hardware
(Beginning slash IS REQUIRED).
If set, the application forcibly redirects all insecure
connections to https://
Values obtained from Google through the Credentials tab in the Google API Console
You'll want an OAuth 2.0 Client ID
.
The application type is Web application
.
Google will require a domain name to call back to, so you'll either need your own or you can test on Heroku (Just please don't test in production!)
The Authorized JavaScript origins
should just be that location, and
the Authorized redirect URIs
should just be:
https://YOURDOMAINNAMEHERE/login/google/authorized
(https is preferrable and required for real deployment but not for testing)
Google does strange things with token scopes so you'll want to set this to 1
When running locally over http, you'll need to set this to 1
You'll need Python 3 and all the requirements as denoted above, and more
specifically in requirements.txt
. If you have the venv
module installed,
you can run setup.sh
which will install all the requirements to a
virtual environment in the venv
folder in the current directory.
You can then use run.sh
, along with a .env
file to set environment
variables (see above) to run the application.
The environment variables should be in .env
in the format:
export VAR=VALUE
to ensure they are actually visible to the application.
You'll then need to run adduser.py
to get the first user
(see scripts section below) and then you can do everything else
via the application.
This application is divided into a series of files that each handles a separate part.
The application itself. Runs everything. Contains all the setup code and the routes
Handles SQL stuff, like the creation of a session and of the tables. Only import once so that only one session exists.
Initialized with auth
and google
so that oauthdb
can be set up.
Contains the model objects for all the tables. See SQLalchemy docs on its declarative system
Contains all the FlaskForm
classes used by main.py
Contains the functions and objects to manage logins and when they are required. Also contains an email regex checker because I didn't know where else to put it and this is the smallest module.
Initialized with flask app, the string name of where it should redirect to
when login is required (See Login Manager docs), and sql
Contains the setup for Flask Dance Google OAuth
Contains the setup for storing Flask Dance logins in SQLAlchemy
Handles BASEURL
(see enviornment variables section)
The stylesheet used to space out a few elements.
Most styling is done by the CSS file of our main website
Contains all the templates, in Jinja2 format
We create four tables:
Database of hardware objects
Names and emails of authorized users
Contains information on every checkout ever done
See models.py
for more details on the above three tables
Table used by Flask-Dance to store OAuth2 tokens See it's documentation
See oauthdb.py
for more details on the above table.
adduser.py <username>
Adds username@auth.domain
(currently username@email.wm.edu
, see auth.py
)
to the users database
addhw.py <name> [quantity] [category]
Adds hardware to the database. quantity
and category
are optional
and will be set to 1
and None
respectively if not passed.