0% found this document useful (0 votes)
13 views

Setup Documentation

Uploaded by

agprayansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Setup Documentation

Uploaded by

agprayansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Project Setup and API Documentation

Table of Contents
1. Running the Project in a Local Environment
○ Prerequisites
○ Steps to Set Up
○ Using Docker
○ Updating Dependencies
2. API Endpoints
○ Register User
○ Login User
○ Get User Profile by User ID
○ Get User Profile by Username
○ Logout User
3. Security Considerations

Running the Project in a Local Environment


Prerequisites

1. IDE Installation: Install an IDE suitable for Django development, such as VS Code or
PyCharm.
2. Python Installation: Install Python 3.12.0 or newer and set its PATH in the Environment
variables.
3. Virtual Environment Setup: Create and activate a virtual environment for the project.

Steps to Set Up
Create a Project Directory:

mkdir myproject
cd myproject

1. Install virtualenv:
pip install virtualenv
2. Create Virtual Environment:
virtualenv venv or
python -m venv venv

3. Activate the Virtual Environment:


.\venv\Scripts\Activate

4. Install Django:
pip install django

5. Install Required Packages:


pip install -r requirements.txt
○ (Ensure requirements.txt lists all necessary packages with versions.)

6. Configure MongoDB: Update the MongoDB connection URI and database name in the
.env file. Adjust the collection name in models.py if necessary.

7. Apply Migrations:
python manage.py makemigrations
python manage.py migrate

8. Run the Development Server:


python manage.py runserver

Access the APIs at:

○ http://127.0.0.1:8000/
○ http://localhost:8000/
Using Docker

1. Install Docker Desktop: Install Docker Desktop and start the Docker engine.

Build and Run Docker Containers:


docker-compose run web python manage.py migrate
docker-compose up

2. Access the APIs at:


○ http://localhost:8000/

Updating Dependencies
To update package dependencies after code changes:
pip freeze > requirements.txt

Unit Test Cases


All the unit test cases can be found under the user folder, in the file named tests.py. Its coverage
report can be accessed with the .coverage file in the root project folder.
The commands to run the test cases and generate the coverage report are as follows:
python manage.py test
pip install coverage
coverage run manage.py test

This last command will generate the coverage report and update the .coverage file. But it can
only be done in the local setup.

API Endpoints
Register User

● Endpoint: /user/register_user
● Method: POST
Request Body:
json
{
"username": "alphanumeric",
"email": "emailId",
"password": "atleast 8 characters",
"first_name": "string",
"last_name": "string"
}


● Response Codes:
○ 201: User created successfully
○ 400: Bad Request

Login User

● Endpoint: /user/login
● Method: POST

Request Body:
json
{
"email": "user email",
"password": "user password"
}
Response Body:
json
{
"access_token": "jwt token",
"user_id": "user_id"
}

● Response Codes:
○ 200: Login successful, returns tokens
○ 401: Invalid credentials

Get User Profile by User ID

● Endpoint: /user/user_profile/<user_id>
● Method: GET
● Header: “User-ID” : “user_id”
● Response Codes:
○ 200: User details retrieved successfully
○ 400: User not found
○ 400: Invalid request

Get User Profile by Username

● Endpoint: /user/user_profile/username/<username>
● Method: GET
● Header: “User-ID” : “user_id”
● Response Codes:
○ 200: User details retrieved successfully
○ 404: User not found
○ 400: Invalid request

Logout User

● Endpoint: /user/logout
● Method: POST
● Header: “User-ID” : “user_id”
● Response Codes:
○ 200: Successfully logged out
○ 400: Bad Request

Swagger API Documentation

● URL: http://localhost:8000/swagger/
● Method: GET
● Description: Details of every API can be accessed, tested, and reviewed with the help
of swagger documentation, which can be accessed with the provided URL.

Security Considerations
● JWT Token Usage: JWT tokens are securely stored in cookies to mitigate XSS attacks.
● CSRF Protection: CSRF tokens are utilized with strict same-site access and expiration
times to prevent CSRF attacks.

This document provides a comprehensive guide for setting up and using the Django project
locally and with Docker, along with details about available API endpoints and security measures.

You might also like