ISPP-G5 / NexONG_Backend

Backend of the app developed for the NGO Manos Abiertas con Norte
http://nexongapi.ew.r.appspot.com/api
Apache License 2.0
0 stars 1 forks source link
charity-donation-system education educative-software ngo ngo-data

Bluejay Dashboard Codacy BadgeCode style: black

Getting started with NexONG_Backend

Follow this guide to set up the project:

1. Clone the repository

2. Configure the virtual environment

Linux

Windows

You can create the Windows virtual environment by running commands as in Linux following this guide. However, I think the easiest option is creating it through VSCode.

image

3. Install requirements

4. Create the database

Unix

Windows

You can check if the database is correctly created using \l in the psql instance

5. Migrate the app and populate the database

In the root folder of the project, run:

6. Run the app

In the root folder of the project, run:

7. Swagger documentation

To consult the API's Swagger documentation you can check it in http://127.0.0.1:8000/docs while the app is running.

Usual errors encountered with the backend

1. DB has been updated

It's not unusual for the database to change. This causes an error, which usually, looks like this:

image

This happens because the migrations that were used to create the DB don't apply anymore. The solution is quite straightforward. Instead of executing the same commands every time and waste time, let's make shell scripts:

image

Linux

Define PostgreSQL database name

DB_NAME="nexongdb"

Prompt for sudo password

echo "Please enter your sudo password:" read -s SUDO_PASSWORD

Stop PostgreSQL service

echo "$SUDO_PASSWORD"

Drop PostgreSQL database

sudo -u postgres psql -c "DROP DATABASE IF EXISTS $DB_NAME;" sudo -u postgres psql -c "create database $DB_NAME owner nexong" sudo -u postgres psql -c "ALTER USER nexong CREATEDB"

echo "Database '$DB_NAME' has been deleted successfully."

- Then, create another file in the same folder as `drop_db.sh` called `migrations.sh` and paste the following code inside it:
```bash
#!/bin/bash

# Install requirements
pip install -r requirements.txt

# Delete previous migrations
rm -rf nexong/migrations

# Make migrations
python manage.py makemigrations nexong

# Migrate
python manage.py migrate

# Create superuser
python manage.py createsuperuser

#Loaddata
python manage.py loaddata populate.json

# Run server
python manage.py runserver

Windows

set PGPASSWORD=your_postgres_password

echo Dropping existing PostgreSQL database if it exists... echo. psql -U postgres -c "DROP DATABASE IF EXISTS nexongdb;" echo.

echo Creating PostgreSQL database... echo. psql -U postgres -c "CREATE DATABASE nexongdb WITH OWNER nexong;" echo.

echo PostgreSQL database created successfully.

cd /d "C:\Your_backend_project_root" echo Changing directory to the project root... echo.

- Then, create another file in the same folder as `drop_db.bat` called `migrations.bat` and paste the following code inside it:
```bash
@echo off
echo Going back one directory...
cd ..

echo Deleting "migrations" folder in "/root/nexong"...
rd /s /q "nexong\migrations"

echo Installing requirements...
pip install -r requirements.txt

echo Running "python manage.py makemigrations nexong"...
python manage.py makemigrations nexong

echo Running "python manage.py migrate"...
python manage.py migrate

echo Running "python manage.py loaddata populate.json"...
python manage.py loaddata populate.json

echo All steps completed successfully.

Note: if you want you can add python manage.py runserver at the end of the script or python manage.py createsuperuser after the populate command

In this way, the usual problem of DB modification is solved.