Purchase Request Management System

Intro

The application for requests/tickets/tasks management raised from the need of ecsaping from shared excel files where the risk of getting things messy is very very high. That affects the data quality which has enormous imapct on further reporting and the analysis.

Applications
  • Single Django project can have multiple applications for single responsibility.
  • My Django project contains two applications:
    - application for registering and logging users into the system,
    - application for adding, updating, removing purchase requests.
  • Application package contains:
    - migrations - migrating model changes to database,
    - _init_.py - making python package,
    - admin.py - keeping admin instance,
    - apps.py - keeping app class for a projectc reference,
    - models.py - storing database models,
    - test.py - testing views or models,
    - views.py - metohds returning response to user,
    - url.py - keeps endpoints diretcign to methods in viwes.
  • All installed apps have to be included in mian project setting.py file:
    INSTALLED_APPS = [
       'users.apps.UsersConfig',
       'prs.apps.PrsConfig',
       'crispy_forms',
       'django.contrib.admin',
       'django.contrib.auth',
       'django.contrib.contenttypes',
       'django.contrib.sessions',
       'django.contrib.messages',
       'django.contrib.staticfiles',
    ]
Object Relational Mapper (ORM)
  • Django has ORM which allows to access database in obejct-oriented way.
  • ORM allows to use different databases like SQLite or PostrgreSQL having no changes to code but accessing database with objects, f.e.:
    - using SQLite db for development,
    - using PostgreSQL db for production.
  • In ORM, we can represent database structure with classes - they are called MODELS.
  • Django converts a class into a data schema with command:
        python manage.py makemigrations
  • ORM lets us to query the database through the model classes.
Terminology:
  • Endpoints - allocation of the web-server that you indicate you want to go to:
        www.web-page.com/endpoint
        incoming request --> endopoint of web-server --> response (JSON, HTML)
  • Model - Python class that, with its attributes, reflects database schema:
    - when you create a table in a relational db, you create a model in Django,
    - models in Django are Python classes,
    - class attributes reflects table fields,
    - this way Djagno allows write Python code and it interprets it and automatically performs all the db operation for us.
Workflow:
  • User hits the web address with a specified endpoint, e.g. www.web-page.com/api/my_endpoint.
  • Note that provided endpoint has two parts: api/ and my_endpoint.
  • Django goes to main web-service urls.py searching for a api/ endpoint.
    from django.urls import path, include
    urlpatterns = [
        path('api/', include('my_application.urls'))
    ]
  • Due to instructions in urls.py endpoint my_endpoint is being redirected to my_application urls.py.
  • The endpoint my_endpoint follows instruction in application urls.py where is mapped with a specific function.
    from django.urls import path
    from .views import main
    urlpatterns = [
        path('my_endpoint', main)
    ]
  • Application endpoint calls function 'main' from an application's views.py.
    from django.http import HttpResponse
    def main(request):
        return HttpResponse("<h1>Hello World</h1>")
  • Called function accepts request from endpoint and returns HTML response.
  • Hitting endpoint: www.web-page.com/api/my_endpoint results with html <h1>Hello World</h1> being displayed.

Features

App includes following features:

  • Object oriented database
  • Post forms data validation
  • User register/login system
  • User profile panel for info updates
  • Access security system
  • Page content filtering

Demo

This section presents app's demo:

First entry site look.

All open requests are stored in the Home tab.

When Purchase Request closed, it goes to the Archive tab.

Each buyer can enter a site with ist own Purchase Requests by clicking on the name.

Each buyer can access its Purchase Requests and udpdate/delete them.

Purchase Request number is restricted to be unique in db.

Setup

  1. Installing django library in venv:
        pip install django
  2. Checking django version:
        python -m django --version
  3. Starting project in project directory:
        django-admin startproject project_name
  4. Running django projects server:
        python manage.py runserver
  5. Creating app:
        python manage.py startapp blog
  6. Data migration each time when we want to update changes to db (first migration will create a database with default tables):
        python manage.py makemigrations
         it detects the changes and prepares them to be uploaded to the database
         the changes are being saved to migrations directory
  7. Applying changes to db:
        python manage.py migrate
  8. Creating superuser:
        python manage.py createsuperuser
  9. Checking sql of migrations:
        python manage.py sqlmigrate app_name num_of_migration
         it shows how model class was interpreted into sql statement
  10. Working with models interactivley in django shell
        python manage.py shell

Source Code

You can view the source code: HERE