tonible14012002 / Home-Security-Backend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setup Environment

Requirement

  • Python, pip installed
  • Virtualenv installed via pip
  • Postgresql

Create Django environment

clone project Create virtualenv and activate virtualenv by run following cmds line by line

virtualenv env
.\env\scripts\activate (activate virtualenv)

You should see (env) at the head of cmd. All the following step require virtualenv is activated.

cd dlib-19.24.0
python setup.py install

Wait for dlib installed successfully. Install opencv and face_recognition

pip install opencv-python
pip install face_recognition

now install all the requirement packages listed in requirements.txt. Run following cmd in the root directory (same directory to requirements.txt).

pip install -r requirements.txt

If errors occur liên hệ Nam Anh. now try to run django server. Migration alert will occur because we havent connect and migrate model to DB.

python manage.py runserver

Postgresql

  • open pgAdmin4
  • Create new database name security
  • connect the db to django.
  • You can google it and follow the instruction.

After connect the database to django server. Run django server again to check db server connected successfully. Now migrate Django models to DB.

python manage.py makemigrations
python manage.py migrate

Open Querytool and run these cmds

create extension if not exists CUBE
ALTER TABLE accounts_myuser
  add vec_low CUBE
ALTER TABLE accounts_myuser
  add vec_high CUBE
create index accounts_myuser_vec_idx on accounts_myuser (vec_low, vec_high)

Now our environment is ready.

AUTHENTICATION

POST - get token (Must Provide account credentials - username, password)
http://127.0.0.1:8000/accounts/auth/token/
POST - refresh token
http://127.0.0.1:8000/accounts/auth/token/refresh/

USERS MANAGEMENT

Ordinary user viewset

http://127.0.0.1:8000/accounts/ordinary/
Method
  • GET - list: Admin only.
  • POST - register user: no Permission. =======
http://127.0.0.1:8000/accounts/ordinary/{id}/
Method
  • PUT - update: only Account owner or Admin.
  • GET - retrieve: only Account owner or Admin.
  • DELETE - detele: Only Account owner or Admin.

Admin user viewset

http://127.0.0.1:8000/accounts/admin/

Method

  • GET - list: only admin.
http://127.0.0.1:8000/accounts/admin/{pk}

Method

  • GET - retrieve admin detail: only Admin.

SEARCH AND FILTER

  • example
FILTER

search fields include username, first_name, email, phone, address Decendant ordered by birth, username

http://127.0.0.1:8000/accounts/ordinary/?ordering=-birth,-username
  • ordering_fields = ['username', 'first_name', 'email', 'phone', 'address', 'visits__time', 'visits'] List accounts ordered by nearest visit
http://127.0.0.1:8000/accounts/ordinary/?ordering=-visits__time
or
http://127.0.0.1:8000/accounts/ordinary/?ordering=-visits__time
SEARCH
  • Search through all accounts' attributes
  • Search fields = ['username', 'first_name', 'email', 'phone', 'address']
  • We can use both filter and search
http://127.0.0.1:8000/accounts/ordinary/?search=092393123
http://127.0.0.1:8000/accounts/ordinary/?ordering=-birth,-username&search=BuiNgocNam
http://127.0.0.1:8000/accounts/ordinary/?ordering=-birth&search=namanhble14012002@gmail.com
PAGINATION

All the response data will be paginated into pages. Here is the sample format

{
    "next": null,
    "previous": null,
    "num_pages": 1,
    "count": 4,
    "results": [
        {
            "pk": 8,
            "username": "tony123",
            "first_name": "Bui",
            "last_name": "NamAnh",
            "email": "namanh12@gmail.com",
            "phone": "0796518081",
            "address": "Ta quang buu",
            "birth": "2022-10-31"
        },
        {
            "pk": 7,
            "username": "tony12",
            "first_name": "Bui",
            "last_name": "NamAnh",
            "email": "namanh1@gmail.com",
            "phone": "0796518081",
            "address": "Ta quang buu",
            "birth": "2022-10-31"
        },
    ]
}

About


Languages

Language:Python 97.1%Language:HTML 2.9%