/pharmacies_api_mongodb

Simple Django application integrating MongoDB as the main database and implements CRUD operations on a Pharmacies collection.

Primary LanguagePythonMIT LicenseMIT

Pharmacies API MongoDB

This project is a simple Django REST API that integrates MongoDB as the main database. It implements CRUD operations on a Pharmacies collection.

Features

  • MongoDB database to store pharmacy documents
  • Django REST Framework to build the RESTful API
  • djongo MongoDB adapter to connect Django and MongoDB
  • Basic authentication for API endpoints
  • API documentation using OpenAPI/Swagger
  • API testing

Setup

Prerequisites

  • Python >= 3.8
  • Django
  • Django Rest Framework
  • MongoDB

Steps

  1. Clone the repo git@github.com:mustafa-kamel/pharmacies_api_mongodb.git
  2. Create a virtual environment python -m venv venv
  3. Activate virtual environment source venv/bin/activate
  4. Install dependencies: pip install -r requirements.txt
  5. Copy the .env.example file to .env run cp .env.example .env
  6. Update the variables in .env file according to your configurations.
  7. Apply migrations: python manage.py migrate
  8. Create a superuser: python manage.py createsuperuser
  9. Run the server: python manage.py runserver

Database

The project uses MongoDB as the backend database. Make sure MongoDB is installed locally and running on port 27017.

Create a local MongoDB database called pharmacies from MongoDBCompass or from shell using:

mongosh
use pharmacies

If you want to use a cloud database instead you will need to add its configuration to the project settings file.

Environment Variables

Make sure to set the following environment variables in .env:

  • MONGO_DB: The mongodb name.
  • SECRET_KEY: Django secret key.
  • DEBUG: Set to True for development, False for production.

Creating New User

  • Use the Django admin interface or the API endpoint /api/users/ to create a new user, Or you can do that simply from the django shell:
python manage.py shell

Then you need to create a new user using this code:

from django.contrib.auth.models import User
User.objects.create_user(username="USERNAME", password="USER_PASSWORD")

These user credentials will be used for basic authentication.

Running the API

  • Use the Django development server to run the API: python manage.py runserver
  • Access the API at http://localhost:8000/pharmacies/

Running Tests

  • Use pytest to run tests: coverage run -m pytest.
  • Generate coverage report: coverage report or coverage html for HTML report that shows a 99% test coverage.

API Documentation

Run In Postman