Coder Social home page Coder Social logo

ispp-g5 / nexong_backend Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 1.0 911 KB

Backend of the app developed for the NGO Manos Abiertas con Norte

Home Page: http://nexongapi.ew.r.appspot.com/api

License: Apache License 2.0

Python 99.65% HTML 0.35%
charity-donation-system education educative-software ngo ngo-data

nexong_backend's Introduction

Bluejay Dashboard Codacy BadgeCode style: black

Getting started with NexONG_Backend

Follow this guide to set up the project:

1. Clone the repository

  • Clone NexONG-backend repository by executing git clone https://github.com/ISPP-G5/NexONG_Backend.git in the directory of your choice.

2. Configure the virtual environment

Linux

  • Install virtualenv pip install virtualenv.
  • In the root directory of the project you just cloned, create the virtual environment by running python3 -m venv myenv, myenv being what you want to name the virtual environment.
  • Activate your new virtual environment with source venv/bin/activate.

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.

  • Open the cloned project in VSCode.
  • Press ctrl+shift+p on your keyboard.
  • Select the option Python: Select Interpreter.
  • Press + Create Virtual Environment.
  • Select Venv.
  • Select Python 3.11
  • If there is a pop-up about the requirements, just press OK.
  • If you see that it is not activated automatically, close the VSCode window and open it again.
  • When it is activated, you should be able to see (.myenv) on the terminal, something like this:

image

3. Install requirements

  • Install project dependencies by running pip install -r requirements.txt in the project's root folder.

4. Create the database

Unix

  • Install postgres running sudo apt install postgresql.
  • Access the postgres instance with sudo su - postgres.
  • Create the user for the database psql -c "create user nexong with password 'nexong'".
  • Create the database psql -c "create database nexongdb owner nexong".
  • Set the role psql -c "ALTER USER nexong CREATEDB".

Windows

  • Install postgres on your machine from the official website.
  • Access the installation folder C:\Program Files\PostgreSQL\16\bin and execute psql -U postgres.
  • Create the user for the database CREATE USER nexong WITH PASSWORD 'nexong';.
  • Create the database create database nexongdb owner nexong;.
  • Set the role ALTER USER nexong CREATEDB;.

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:

  • python manage.py makemigrations nexong
  • python manage.py migrate
  • python manage.py loaddata populate.json

6. Run the app

In the root folder of the project, run:

  • python manage.py runserver
  • Access to the DEMO API on http://127.0.0.1:8000/api/

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:

  • Open the backend project in VSCode and create the folder .vscode in the root folder. It should look like this:

image

Linux

  • Create a file called drop_db.sh inside .vscode and paste this inside it:
#!/bin/bash

# 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:
#!/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
  • Make both scripts executable by running this in the .vscode directory:
chmod +x drop_db.sh
chmod +x migrations.sh
  • Finally, run first the drop_db.sh script and then the migrations.sh script. To run them, go to the .vscode directory, execute drop_db.sh or migrations.sh in the terminal.

Windows

  • Create a file called drop_db.bat inside .vscode and paste this inside it:
@echo off
cd /d "C:\Program Files\PostgreSQL\16\bin"
echo Changing directory to PostgreSQL bin folder...
echo.

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:
@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

  • Finally, run first the drop_db.bat script and then the migrations.bat script. To run them, go to the .vscode directory, execute drop_db.bat or migrations.bat in the terminal.

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

nexong_backend's People

Contributors

pedlopruz avatar juanlurm avatar pabpercab1 avatar manortbla avatar auroranavas avatar ahydul avatar felixogudiel avatar mromalde avatar marnunrey2 avatar

Forkers

auroranavas

nexong_backend's Issues

Update readme

Right now, the README of the backend repository is outdated. It needs to be updated so frontend developers can use the backend smoothly.

Admin authorisation

Implement middlewares: What can an admin user account do? Which data can the admin change and see and have access to, and where can do a GET, POST, PUT, or DELETE occur?

User, Educator, Partner, Volunteer y Family CRUD

Educator, Partner, Volunteer and Family are derived from User. Therefore, if you do a GET on an educator with id 1 it should return educator with id 1 and the user that has the FK of educator id 1. If you do an operation on Educator, Partner, Volunteer and Family it should also be done on User.

And fix User CRUD.

Register and Login

Implement the basic authentication feature for the backend project, taking into consideration the next week's future integration with an authentication framework. This should include the login and the register method.

Lesson CRUD

Beware of dependencies: Educator and Student

Export Partner / Partners data

Create a CSV, a PDF and an Excel of the selected data of the backend giving also the option to download the related files in a zip/rar or accessible via URL to properly see them in the exported documents.

For this task, it is necessary to take the same structure/solution as the task developed in the week before.

Hash+salt passwords

It is necessary to implement proper password security for the authorisation feature, currently, it consists of plain text. The password should be correctly encrypted in the database, and it should be nearly impossible to obtain it back if the DB is stolen.

Punctual donations in the donations export

This issue consists of changing the current donations export feature to include punctual donations when the donations are exported. This should be reflected as a new column in the donation export that points out whether the donation is a registered user or a punctual donation.

It will also be great to include a last row only in the donations (not punctual), as a sum of all the donations of the table (only in the PDF).

Export Volunteers / Volunteer data

Create a CSV, a PDF and an Excel of the selected data of the backend giving also the option to download the related files in a zip/rar or accessible via URL to properly see them in the exported documents.

For this task, it is necessary to take the same structure/solution as the task developed in the week before.

Duplicated /api endpoint

Describe the bug
Right now there is a duplicated endpoint in src/urls.py because of a merge mistake. This can cause errors in the application.

Evaluation CRUD

Beware of dependencies, also this in the db translates to CRUD of StudentEvaluation and LessonEvaluation.

Export Students / Student data

Create a CSV, a PDF and an Excel of the selected data of the backend giving also the option to download the related files in a zip/rar or accessible via URL to properly see them in the exported documents.

For this task, it is necessary to take the same structure/solution as the task developed in the week before.

Files organisation

Now if you upload a file to the DB, the names stay the same. What happens when the backend gets 2000 files? Probably some names will collide. The plan is to implement a name and date system for the names of the files. The backend is responsible for change when a new file is uploaded.

Also, it should include a new table with the documents required for the homepage.

Populate

Create an extended version of the current populate for the database.

Export Dontations / Donation data

Create a CSV or a PDF of the selected data of the backend giving also the option to download the related files in a zip/rar or accessible via URL to see them in the exported documents properly.

The documents should be accessible using /API/export/type/document
The URL must accept a query or the method should accept a body to establish a range of values or a sorting method.

Check all business logic

Check if currently all the models as well as the serializers are working as expected. For example, a birthday cannot be 12/12/2030, or you cannot create a second lesson event of the same lesson if both of them collide. Also, this task should check the general state of the models, and that the business logic is working as expected in the requirement document.

Remove the admin authentication for frontend

The admin authentication is required to access some POST, PUT and DELETE methods. To give a first usable API, it is essential to remove the current authorisation of the deployed API to be usable for the frontend without the login process.

Authentication Framework+Google

Choose an authentication system for the backend to manage the logged users. The current options for this system are:

The chosen system must be consensual and chosen according to requirements. Other systems can be proposed also if any of this fits in NexONG.

After the decision of the authentication system, it should be implemented in the app. Note that if the chosen system lacks social account support, the integration should be done manually.

Finally, it is required to integrate at least the Google Register and Sign-in method for the project.

Email validation

Include a procedure to send an email after the registration process to verify that the user has registered in the application. This should include a new user model that reflects this verification and validation.

Backend documentation

Create a /docs endpoint in the backend application (it can have another name rather than "docs", but it should be related). This endpoint should contain the swagger documentation of the entire backend. For the documentation, you should implement it using Swagger (https://swagger.io/). Still, try to keep the repository as well as the documentation organised and clean, for that reason consider creating a folder containing all of these.

Incorrect validator in meeting API

Describe the bug
There is a validator of another class in the serializer of meeting. It must be removed in order to work on meetings.

To Reproduce
Steps to reproduce the behavior:

  1. Go to /api/meeting and recieve an error because the validator is not correct for this class

Expected behavior
Standard behaviour of an API class

Update Populate

Along the week 7, some minor changes have been made to the db. It is expected that the populate will now be either incomplete or non working. The populate.json file shoud be updated so that it can continue tu be useful.

Models changes in the app

After the meeting with our client, some old requirements have been changed and new ones have been introduced. Read the requirements sheet to update your knowledge.

Partner Authorisation

Implement middlewares: What can a partner user account do? Which data can the partner change and see and have access to, and where can do a GET, POST, PUT, or DELETE occur?

For this task, it is necessary to take the same structure/solution as the task developed the week before.

Upload documents to the DB

How do you manage the documents like PDFs in the DB? Investigate the options. Create a new table with the server route to the document?

Educator authorisation

Implement middlewares: What can an educator user account do? Which data can the educator change and see and have access to, and where can do a GET, POST, PUT, or DELETE occur?

For this task, it is necessary to take the same structure/solution as the task developed the week before.

Volunteer Authorisation

Implement middlewares: What can a volunteer user account do? Which data can the volunteer change and see and have access to, and where can a GET, POST, PUT, or DELETE occur?

For this task, it is necessary to take the same structure/solution as the task developed the week before.

Student and Family Authorisation

Implement middlewares: What can a student account (managed by the family, of course, only in the case it is not linked to it) or a family user account do? Which data can the student/family change, see and have access to, and where can a GET, POST, PUT or DELETE occur?

For this task, it is necessary to take the same structure/solution as the task developed the week before.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.