Backend application to house productivity tools:
- A habit tracker
- A post saver
Note: to be used in conjunction with productivity-hub
- Python
- Django
- Django Rest Framework
- Firebase Admin
- Fuzzywuzzy
- Beautiful Soup 4
- APScheduler (development only)
- Dotenv
- PostgreSQL
- Cron
FIREBASE_PROJECT_ID=
FIREBASE_PRIVATE_KEY_ID=
FIREBASE_PRIVATE_KEY=
FIREBASE_CLIENT_EMAIL=
FIREBASE_CLIENT_ID=
FIREBASE_CLIENT_CERT_URL=
SECRET_KEY=
DEBUG=
ALLOWED_HOSTS_DOMAIN=
ALLOWED_HOSTS_IP_ADDRESS=
CORS_ALLOWED_ORIGINS=
POSTGRES_NAME=
POSTGRES_USER=
POSTGRES_PASSWORD=
FIRST_TIME=
See the project page here
- Clone this repo
- Create a Firebase account, create a new project, and enable Firebase Authentication. This account will also be used with productivity-hub
- Go to Settings > Service Accounts > Generate new private key for .env
- Add environment variables to .env file using Firebase settings
- In
/hub_api/hub_api/wsgi.py
, change up the project directory folder to be able to load .env files in your server configuration - In
/hub_api/hub_api/settings.py
, for logs, change up the project directory folder to your designated log directory. pip install -r requirements.txt
- You may need to install Build Tools for Visual Studio - Microsoft
cd hub_api
python manage.py migrate
- On first run, set
FIRST_TIME=True
in .env - On subsequent runs, set
FIRST_TIME=False
in .env
- On first run, set
python manage.py runserver
- In
/hub-api/hub-api
, runpython manage.py createsuperuser
-
Example script is given in
run_cron_jobs
-
On your server, modify the scripts to each command
- i.e.
run_cron_jobs
line 8,python manage.py sample
- i.e.
-
Change the permissions of the cron jobs using
chmod +x /path/to/script
-
crontab -e
to open up/create the main cron file -
Example usage for the following commands:
-
*/30 * * * *
/path/to/script/runevery30minutes -
0 */3 * * *
/path/to/script/runevery3hours -
0 0 * * 0
/path/to/script/runeveryweek -
0 * 1,16 * *
/path/to/script/runevery2weeks (2 times a month, rather than every 2 weeks) -
You can also set outputs to files by attaching
>> /path/to/log/sample.log 2>&1
. This is good for debugging why your script isn't running
-
-
You can also set up a
MAILTO=
incrontab -e
to be sent an email everytime a cron job is finished
python manage.py generateschema --format openapi > schema.yml
- Go into Django shell
python manage.py shell
python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
Examples that require database downtime when changing models around in apps
-
Use
python manage.py sqlmigrate xxxx
to check the generated SQL statements created by Django. Modify if needed -
See
/open_apps/migrations/0001_initial.py
and/post_saver/migrations/0006_auto_20201129_2151.py
in combination
For a no downtime migration remapping a model to a different table, in models.py
, for your Model classes, set a
class Meta:
db_table = 'xxxxx'
for your table to be called 'xxxx'. By explicitly calling the db_table name, you bypass Django's auto-generated table names and gain control over your database naming conventions. (Not sure about any other operations you perform in a database)
python manage.py showmigrations
- Pick the migration you want to do back to
python manage.py migrate [app_name] [migration_you_want_to_revert_to]
You can configure to either run cron jobs via Cron on your server or to use Python AP Scheduler
- Cron jobs need to be configured within your server
- Python AP Scheduler needs to be run everytime your server starts up