Moorpark Enthusiastic Programmers Club website
The purpose is to have a website for the club for easy sign up, and easy access to more club information and announcments. We.Love.Hackathons.
Link to website: https://epclub.pythonanywhere.com/
-
Clone this repository
-
Create a python virtual environment
python -m venv venv
-
Activate the virtual environment
Linux:
source venv/bin/activate
Windows:
venv\Scripts\activate.bat
-
Install dependencies
pip install -r requirements.txt
-
Initialize the database tables:
flask db upgrade
-
Run flask development server
python wsgi.py
Note: The development server will automatically reload source files when it detects an updated file on the disk.
-
Or, run the flask server with debug mode disabled
flask run
Note: This command is not sufficient for running the website in production.
This project uses SQLAlchemy to manage database ORMs (Object-Relational Models).
Each model in clubWebsite/database/models.py
is mapped to at least one table, where
properties of that model represent database columns. SQLAlchemy generates SQL statements,
and handles creating/destroying sessions.
The database also uses Alembic (provided by flask-migrate) for generating database migrations. Migrations are effectively scripts that describe the changes in database tables/columns over time, providing version control for database layouts.
When changing or adding database models, in order to use them in your application run
flask db migrate -m "<migration descriptive name here>"
to generate a new migration script. This behaves similarly to a git commit in that the
generated script will look at the current database structure, and compare it to the structure
described by the ORM models. It will automatically generate a migration script in migrations/versions/
with the generated upgrade()
and downgrade()
functions. It is often necessary to manually edit
or add extra code to facilitate with data migration. Examples of code that needs to be edited manually
include transforming types from one form to another (E.G. converting a string to an int, and vice versa).
After generating the migration script, you need to run
flask db upgrade
to apply the database migrations.
-
Create a PythonAnywhere account and login
-
Under the Dashboard tab -> Consoles start a new bash console
-
Clone the clubWebsite repo:
git clone https://github.com/Enthusiastic-Programmers/clubWebsite
cd clubWebsite/
-
Switch to the flask-server branch
git checkout flask-server
-
Create a virtual environment
python3 -m venv prod_venv
-
Enter the virtual environment
source prod_venv/bin/activate
-
Install python dependencies in the virtual environment
pip install -r requirements.txt
-
Create the database
flask db upgrade
-
Close the console
exit
-
Go to the
Web Apps
tab in your PythonAnywhere account -
Click
Add a new web app
-
Set the domain name (or leave it as the default of .pythonanywhere.com)
-
On the
Select a Python Web Framework
selectManual configuration
-
Select the python version
3.7
-
Click
Next
to generate a sample WSGI configuration file (we will edit this later) -
Under the
Code
section of the web app, se theSource Code
directory and theWorking Directory
to/home/<username>/clubWebsite
-
Under the
Virtualenv
tab, set the virtualenv directory to/home/<username>/clubWebsite/prod_venv
-
Under
Static files
, add a static location with the url/static
and the path/home/<username>/clubWebsite/clubWebsite/static
-
Under
Security
, setForce HTTPS
to Enabled -
Under the
Code
section, click theWSGI Configuration File
to edit it -
Delete the existing contents of the WSGI Configuration File and replace it with:
import sys
path = '/home/<username>/clubWebsite'
if path not in sys.path:
sys.path.append(path)
from wsgi import app as application
- Save the WSGI Configuration File, and click the Reload button to reload the site configuration
-
Under the Dashboard tab -> Consoles start a new bash console (or click on an existing one)
-
CD into the git repo:
cd /home/<username>/clubWebsite
-
Pull changes
git pull
-
Run database migrations (Important!)
flask db upgrade
-
Close the console
exit
-
Reload the web app