Precious Leads / Django Backend
Installation
Python3 and postgres are asumed to be installed before starting.
First, clone the repository:
git clone https://github.com/acalderon85/preciousleads.git
Next, you will need any python version between 3.10 and 3.12.
Any other version will not work because of pip dependencies failing.
It is recommended to install pyenv to manage multiple versions of Python.
Next, create an environment using venv called .venv and install the requirements:
python3 -m venv .venv # or python (instead of python3) if using pyenv
source .venv/bin/activate
pip install -r requirements.txt
When new dependencies are added, pip install -r requirements.txt needs to be re-run. Always make sure that you run source .venv/bin/active, if not, the requirements will be installed globally.
Local Setup
Copy the file .env.example and rename it to .env on the root of the repository.
You will have to manually set on the .env file secret key and DB connection credentials:
DJANGO_SECRET_KEY=[django key]
DB_USERNAME=[postgres username]
DB_PASSWORD=[postgres password]
The Django secret key is used locally, so you can use any random insecure string such as:
DJANGO_SECRET_KEY=django-insecureasdjald213012390asdas0vjcasdij21338ASDJVivjdi
Google SSO Test Keys
To test Google SSO, the following keys need to be set:
# Test keys for Google SSO (these live inside Bitwarden)
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY=
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET=
These keys are inside the Precious Leads Bitwarden vault.
NOTE: The .env file is gitignored, so any private information (such as Stripe or future API keys) should be set inside the .env file, not .env.example so that it is not accidentally committed.
Any variable will be overriden with an OS-level environment variable if supplied, for example:
DB_USERNAME=jon python manage.py runserver
Will try to connect to your database using the username jon.
Database Creation
Use a database manager or psql on the command line:
CREATE DATABASE precious ENCODING 'UTF8';
Run pending migrations:
python manage.py migrate
Fixtures
The next step is to run and load the Fixtures data.
Fixtures are needed to create the two super users, andres@preciousleads.io and yuan@preciousleads.io.
You must log in with a superuser to create new Oauth Applications, for example.
Oauth Applications
Applications such as React Web App, Zapier, or the Mobile APP need to be registered as applications so that tokens can be retrieved.
Follow the Oauth Application Guide to create the Client IDs required.
Running the server
Use the command:
source .venv/bin/activate # if needed
python manage.py runserver 8048
The port 8048 part is important to make sure the Frontend version can connect properly.
You should be able to see the admin by accessing the url http://localhost:8048/admin
You should be able to log in using your super user credentials.
Running Redis
Redis is used along with Celery for background tasks.
Install Redis on Linux:
sudo apt-get update
sudo apt-get install redis
Install Redis on Mac OS:
brew install redis
Ping Redis to verify it’s running:
redis-cli ping
Should respond with PONG.
If not running, you should start Redis:
redis-server
Alternatively, if redis is installed using Homebrew on Mac OS, you can use:
brew services start redis
Or in the background:
redis-server --daemonize yes
Running Celery worker
In the project directory, run the following:
celery -A preciousleads worker --loglevel=info
Running Leads > Import leads from the Bruno test will verify if the worker is running properly. After importing leads, run the test Leads > Check status. There is a file with 3 leads inside the samples folder.
If the status is PENDING, Celery failed to run properly.
Testing on your LAN
Running preciousleads-expo with a local Django Backend requires you to pass 0.0.0.0 as hostname:
python manage.py runserver 0.0.0.0:8048
On the Expo side, you will need to connect to LAN IP of the machine running Django, for example http://192.168.0.1:8048. This URL may change often as your router assigns a new IP.
SMTP Email server on local
If no SMTP server is set, email-sending actions (such as reset password, create new user invitation) will not work, a 500 error SMTPServerDisconnected will appear.
Locally, emails can be printed in the Django console by editing the .env file:
EMAIL_BACKEND_CONSOLE=True