This is the backend repository for FINSEC Website project, initialised in NUSSU CommIT's Technical 32nd Committee (AY2023/2024)
The backend for FINSEC Website project uses Django with REST API implementation. Project tasks and backlogs are documented through GitHub Issues and GitHub Projects.
Technical documentation can be found through the following link:
Google Drive link for Technical Docs
This guide is built with Ubuntu environment on Windows (or WSL) in mind. If you are a windows user, you can follow through the following commands by first opening your Ubuntu terminal or using your WSL.
- Download Ubuntu 22.04.2 LTS in Microsoft Store
- Open Windows PowerShell as an administrator
- Start the Ubuntu WSL shell
- Update your package repository, upgrade existing packages, and reboot
$ sudo apt update
$ sudo apt upgrade
$ reboot
- (If you haven't done so) Install PIP, the Python package manager and Virtual Environment module. Install Git as well
$ sudo apt install python3-pip python3-venv
$ sudo apt install git
- Import the PostgreSQL repository key, and add the repository
$ sudo apt install curl ca-certificates gnupg
$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null
$ echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
$ sudo apt update
- Install PostgreSQL 14. (Why 14?) because currently my internship uses 14 and I'm too lazy to have to store 2 postgres versions in my ubuntu terminal :(
$ sudo apt install postgresql-14 postgresql-client-14
- Start the PostgreSQL service on your computer
$ sudo service postgresql start
Note: this command is unique for Windows WSL users. If you are using "pure" Ubuntu (Linux OS), please use sudo systemctl start postgresql
instead.
- Log in as the Postgres superuser
$ sudo su - postgres
- Start the Postgres interactive terminal
psql postgres
- Create a database called
finsec
on Postgres, then create a user and grant it privileges to modify and write to the database
# CREATE DATABASE finsec;
# CREATE USER finsecuser WITH PASSWORD 'finsec';
# GRANT ALL PRIVILEGES ON DATABASE finsec TO finsecuser;
-
Leave the Postgres shell running on WSL for the later steps.
-
If you wish to to exit the Postgres shell use
CTRL-D
, and anotherCTRL-D
to logout of the Postgres superuser
-
Navigate to the directory of the cloned repository
-
Create a Python Virtual Environment
$ python3 -m venv env
- Activate the Virtual Environment
$ source env/bin/activate
- Install the required packages.
$ pip install -r requirements.txt
-
Change directory back to the root folder
./finsec-website-backend
-
Run in terminal
$ python3 manage.py createsuperuser
- Enter username and password
- Get a copy of the
.env
file from another team member. Place the.env
file in thefinsec_website_backend
folder
Ideally, the folder should have the following structure.
finsec_website_backend
├── api
├── finsec_website_backend
│ ├── __init__.py
│ ├── __pycache__
| ├── .env
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── DeveloperGuide.md
├── manage.py
└── requirements.txt
Note that this structure is taken at the time this README is created. However, I believe that the inner finsec_website_backend
folder should remain untouched, as it won't be the main development app for Django.
- Update your Postgres username and password in this file so Django can connect to your database
If you use the default user finsecuser
and password finsec
, then you don't have to modify the username anymore. However, you might be needing to update your PostgreSQL port. Check this accordingly with your system.
- Run the database migrations needed for this project
$ python3 manage.py migrate
- Run the local Django development server
$ python3 manage.py runserver
- Using your browser or Postman, navigate to
localhost:8000/api/public
and see if it works
If you don't want to use an Ubuntu / WSL system, then it should work well also on regular Windows systems. Do note that however, Windows might not entirely support backend development for more complex features (e.g. when redis
/celery
would be used for distributed infrastructure), thus it is recommended to refer to the Ubuntu/WSL development.
However, since the current iteration does not involve complex features that are not doable with a windows system, it is perfectly fine to use Windows (Powershell) for the current project.
-
Install PostgreSQL on your computer. We recommend Postgres 14, however Postgres 15 works well too in this case. When setting up PostgreSQL, don't forget to tick the option to install pgAdmin for data visualisation.
-
Create a Postgres database
- Open pgAdmin 4 and login with your master password
- Right click on "Databases", and click on the "Create >> Database" option
- Name the database "Finsec" or any other name that you like, and click "Save"
-
Navigate to the repository after cloning
-
Create a Python Virtual Environment
$ python3 -m venv env
- On your terminal (powershell/VSCode terminal), activate the Virtual Environment
$ ./env/Scripts/activate
- Install the required packages
$ pip install -r requirements.txt
- Get a copy of the
.env
file from another team member. Place the.env
file in thefinsec_website_backend
folder
Ideally, the folder should have the following structure.
finsec_website_backend
├── api
├── finsec_website_backend
│ ├── __init__.py
│ ├── __pycache__
| ├── .env
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── DeveloperGuide.md
├── manage.py
└── requirements.txt
Note that this structure is taken at the time this README is created. However, I believe that the inner finsec_website_backend
folder should remain untouched, as it won't be the main development app for Django.
-
Update your Postgres username and password in this file so Django can connect to your database. Change the username to
postgres
and the password to your postgres master password. You might also need to modify the Postgres port according to the port that Postgres is running on your computer. -
Run the database migrations needed for this project
$ python3 manage.py migrate
- Run the local Django development server
$ python3 manage.py runserver
- Using your browser or Postman, navigate to
localhost:8000/api/public
and see if it works
Cos of our frens who use MacOS (more superior than windows tho T_T)
-
Navigate to the repository after cloning
-
Create a Python Virtual Environment
$ python3 -m venv env
- Activate the Virtual Environment
$ source env/bin/activate
- Install the required packages
$ pip install -r requirements.txt
-
If you are on MacOS, make sure you have the Homebrew package manager installed.
This is a legit gamechanger :O -
Using Homebrew, install and start Postgres:
$ brew install postgresql
$ brew services start postgresql
- Start the Postgres interactive terminal
$ psql postgres
- Create a database called
finsec
on Postgres, then create a user and grant it privileges to modify and write to the database
# CREATE DATABASE finsec;
# CREATE USER finsecuser WITH PASSWORD 'finsec';
# GRANT ALL PRIVILEGES ON DATABASE finsec TO finsecuser;
CTRL-D
to exit the Postgres shell.
- Get a copy of the
.env
file from another team member. Place the.env
file in thefinsec_website_backend
folder
Ideally, the folder should have the following structure.
finsec_website_backend
├── api
├── finsec_website_backend
│ ├── __init__.py
│ ├── __pycache__
| ├── .env
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── DeveloperGuide.md
├── manage.py
└── requirements.txt
Note that this structure is taken at the time this README is created. However, I believe that the inner finsec_website_backend
folder should remain untouched, as it won't be the main development app for Django.
- Update your Postgres username and password in this file so Django can connect to your database.
If you use the default user finsecuser
and password finsec
, then you don't have to modify the username anymore. However, you might be needing to update your PostgreSQL port. Check this accordingly with your system.
- Run the database migrations needed for this project
$ python3 manage.py migrate
- Run the local Django development server
$ python3 manage.py runserver
- Using your browser or Postman, navigate to
localhost:8000/api/public
and see if it works
Detailed error:
django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5434 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Solution:
- Check your PostgreSQL port using
sudo service postgresql status
for windows users, orbrew services list
for mac users. - If your port number (should be starting with 54..) is different from port 5434, update the
DB_PORT
variable in your.env
file accordingly