A backend for the CSUA interblags.
A Django web app that:
- Hosts our website at https://www.csua.berkeley.edu
- Displays information about our club
- Manages and displays current officers, politburo, sponsors, and events
- Provides new CSUA account creation
- Provides CSUA account password resets
- Provides interface for managing LDAP groups
- Runs a slack bot on https://csua.slack.com
- Runs a discord bot on https://www.csua.berkeley.edu/discord
- Tracks Soda 311 office computer usage
See issues for a list of TODOs.
- (Optional) Create a fork of this repo
- Do this if you don't have write access
- If you do have write access, use a new branch instead
- Clone to your local/development machine
- Install dependencies
- Make changes, test, repeat
- Commit those changes
- Push commits to your fork/branch
- Make a pull request
- Install Python 3.9+
- Create venv
python3 -m venv venv - Activate venv
source venv/bin/activateor prepend/venv/bin/to these commands - Install pre-commit with
pre-commit install - Install poetry
pip install -U pip setuptoolspip install poetry
- Install dependencies with
poetry install - Create your
.envfile by copying.env.dev, e.g.cp .env.dev .env - Set up local sqlite database with
python3 manage.py migrate - Create the required database table for the google calendar integration
python3 manage.py createcachetable - Run server with
python3 manage.py runserver- If on soda, you will have to run
python3 manage.py runserver 0.0.0.0:$PORTwhere$PORTis between 8000 and 8999, and connect by going tohttp://soda.berkeley.edu:$PORT
- If on soda, you will have to run
- Navigate web browser to http://127.0.0.1:8000/
- Create admin user with
python3 manage.py createsuperuser- Visit the admin page at http://127.0.0.1:8000/admin/ to add a semester object
If you're using GNU/Linux or OSX, use bootstrap.sh.
pre-commit is a tool that picks up formatting and other issues before making a commit. It will automatically format your python code with black. This is so that the code is clean and consistent, making it easier to review.
Additionally, I recommend you set up autoformatting with black on-save. If you use vim, you can add this to your .vimrc:
autocmd BufWritePost *.py silent exec "!black <afile>" | exec "redraw!"- Make changes to
db_data/models.py venv/bin/python3 manage.py makemigrationson your development machinevenv/bin/python3 manage.py migrateto apply new migrations to your local db- Commit and push your changes to
models.pyas well as generatedmigrations/ - Pull latest changes on remote machine
python3 manage.py migrateon remote machine to update database with latest models- Run
sudo systemctl reload csua-backend-gunicornon the remote machine so the changes take effect
Go to https://www.csua.berkeley.edu/admin/ to edit data!
Django's online documentation has more detail on a project's structure
apps/- This Django project is divided into "apps" (i.e.
main_page/,db_data/, etc.) csua_backend/holds the projects's configurations- Each app is divided into:
migrations/lists the changes that have been made to the database models__init__.pyjust tells python the app is a python moduleadmin.pydetails how db models should be viewed in the admin interfaceapps.pyprobably says that this directory is an appmodels.pycontains the database models of the apptests.pyhas unit tests to test the apps functionalityurls.pysays what URLs route to which viewsviews.pyhas functions that serve a "view" (webpage)
- This Django project is divided into "apps" (i.e.
fixtures/contains database fixtures to record and bootstrap content and various database datamedia_root/is where user-uploaded files are served fromrequirements.txtlists the required python packages for this project.static_root/is where static files are served from (many of which come from./static/and are moved here bymanage.py'scollectstatic)templates/holds the html templates that are populated and served by viewsmanage.pyis a command-line script for performing actions on the project
- Add an API key with the Google Calendar API enabled to the
GOOGLE_CALENDAR_API_KEYenvironment variable in your.envfile. - Set up the django cache table by running
python3 manage.py createcachetable - Run
python3 manage.py fetch_calendarto fetch the calendar data, this cache lasts for 1 hour so you will need to add a cronjob to run this command each hour.
As of 2024 we no longer use Travis CI (our repo was formerly located here). Please manually deploy from Tap instead:
sshintotap.csua.berkeley.edu- Change directory to the project directory
/webserver/csua-backend/ sudo -u www-data git pullsudo -u www-data venv/bin/python manage.py collectstaticto update static filessudo -u www-data venv/bin/python manage.py migrateto migrate dbsudo -u www-data venv/bin/python manage.py testto make sure tests passsudo systemctl restart csua-backend-gunicornto restart server
- This Django app runs as a wsgi app on a
gunicornserver ontap. - The
gunicornprocess is managed bysystemdand the service file is located at/etc/systemd/system/csua-backend-gunicorn.service- This service can be manipulated with
systemctl- To reload the wsgi app, run
sudo systemctl reload csua-backend-gunicorn
- To reload the wsgi app, run
- This service can be manipulated with
tapruns debian 9.8 (stretch), we are using Python 3.9.- The app is behind an Nginx proxy.
- Nginx serves the static and media files, homedirs, and forwards all other requests to the app.
- https://github.com/CSUA/services-nginx/blob/master/sites-available/www.csua.berkeley.edu
/etc/nginx/sites-available/www.csua.berkeley.edu
mysqlclientis installed and necessary for deployment ontap
These changes are here so that the newuser script and deployment script run properly.
If the change, /etc/sudoers may also need to be changed.
www-data ALL = (root) NOPASSWD: /webserver/csua-backend/apps/newuser/config_newuser
www-data ALL = NOPASSWD: /bin/systemctl restart csua-backend-gunicorn
tap runs an OpenLDAP server. It is accessible from anywhere over TLS on port 636.
For an LDAP client to connect, it must accept our self-signed certificate.
Usually this is done by adding this line to /etc/ldap/ldap.conf:
TLS_REQCERT allow
python3 manage.py dumpdata db_data > fixtures/$(date +db_data-%m%d%y.json)