Skip to main content

A simple polls application

Project description

Django Poll App

A simple polls application

Requirements

  • Python >= 3.5.2
  • Django 2.2.10
  • Django REST framework 3.11.0

Installation

Install using pip...

pip install djangopollapp

Add the following to your settings.py module:

INSTALLED_APPS = [
    ...
    'djangopollapp',
]

MIDDLEWARE = [
    ...
    'djangopollapp.middleware.PollMiddleware',
]

Include polls api to your urls.py:

    # Polls API
    url('^api-polls/', include('djangopollapp.api.urls'))

and

./manage.py migrate

Testing the Setup

Example superuser authentication:

curl -X POST -d "username=<username>&password=<password>" http://127.0.0.1:8000/api-token-auth/

# Response from DRF
{"token":"dfaef188d5f075802cf7b627a41e4dd3632d127b"}%  

After that we can create poll without questions:

curl -X POST -H "Authorization: Token <token>" 
-H "Content-Type: application/json" 
-d '{"poll":
        {
            "title":"Sample",
            "end_date":"2021-05-13T21:25:46Z",
            "description":"sample discript"
        }
    }' http://127.0.0.1:8000/api-polls/v1/polls/

# Response axample
{
    "id":1,
    "author":1,
    "title":"Created-poll",
    "start_date":"2020-05-15T06:41:04.837196Z",
    "end_date":"2021-05-13T21:25:46Z",
    "description":"some descript",
    "is_active":true,
    "questions":[]
}

Documentation

For clients

Unique ID

At the first request to the resource, the middleware checks for a unique id in its cookies. If the id is not found, the middleware generates a unique id in the format "uu id" and gives the guest user a cookie, after which all requests are made with the context of a unique id

Polls

Get all active polls

GET http://127.0.0.1:8000/api-polls/v1/polls/

#Response
    {
        "id": 6,
        "author": 1,
        "title": "One more poll",
        "start_date": "2020-05-15T11:36:44.018535Z",
        "end_date": "2021-05-13T21:25:46Z",
        "description": "some description for this poll",
        "is_active": true,
        "questions": null
    },
    {
        "id": 5,
        "author": 1,
        "title": "One more poll",
        "start_date": "2020-05-15T11:36:09.603685Z",
        "end_date": "2021-05-13T21:25:46Z",
        "description": "some description for this poll",
        "is_active": true,
        "questions": null
    },
    ...
]

Get polls by client, with choice detalization

GET http://127.0.0.1:8000/api-polls/v1/polls/for-client/

#Response
[
    {
        "id": 7,
        "author": 1,
        "title": "First poll",
        "start_date": "2020-05-15T11:50:09.293380Z",
        "end_date": "2021-05-13T21:25:46Z",
        "description": "some description for this poll",
        "is_active": true,
        "questions": [
            {
                "id": 19,
                "poll": 7,
                "qns": "what?",
                "qns_type": 1,
                "choices": null
            },
            {
                "id": 20,
                "poll": 7,
                "qns": "when?",
                "qns_type": 2,
                "choices": [
                    {
                        "id": 25,
                        "qns": 20,
                        "text": "Today",
                        "vote_counter": 0,
                        "selected": true
                    },
                    {
                        "id": 26,
                        "qns": 20,
                        "text": "Еomorrow",
                        "vote_counter": 0,
                        "selected": false
                    }
                ]
            },
            {
                "id": 21,
                "poll": 7,
                "qns": "where?",
                "qns_type": 3,
                "choices": [
                    {
                        "id": 27,
                        "qns": 21,
                        "text": "here",
                        "vote_counter": 0,
                        "selected": true
                    },
                    {
                        "id": 28,
                        "qns": 21,
                        "text": "there",
                        "vote_counter": 0,
                        "selected": true
                    }
                ]
            }
        ]
    }
]

To answer

We transfer the list of objects. Attributes of each object: question id, question type and selected options / text

POST http://127.0.0.1:8000/api-polls/v1/polls/{id}/to-answer/

#data
[
	{
	    "id": 19,
	    "qns_type": 1,
	    "choices": {"text": "Some text"}
	},
	{
	    "id": 20,
	    "qns_type": 2,
	    "choices": 25
	},
	{
	    "id": 21,
	    "qns_type": 4,
	    "choices": [27, 28]
	}
	...
]

#Response
HTTP Status 201 OK

For admins

Authorization

Uses standard token authorization from drf All requests except get require authorization

curl -d '{"username": <username>,"password": <password>}' -H "Content-Type: application/json" -X POST http://127.0.0.1:8000/api-token-auth/

#Response
{"token":"dfaef188d5f075802cf7b627a41e4dd3632d127b"}

Polls

Create

A poll is created in two parts:

  • Poll data
  • Question data

Each question has its own type.

  • 1 - Text answer
  • 2 - Single Choice
  • 3 - Multi Choice

If question type is 1(Text answer), choices field must be empty. If question type is 2(Single Choice) or 3(Multi Choice), choices field will be a include choice objects

In the data of each question, we also put choices depending on its type

curl http://127.0.0.1:8000/api-polls/v1/polls/ \
-X POST \
-H "Authorization: Token dfaef188d5f075802cf7b627a41e4dd3632d127b" \
-d '{
  "poll": {
    "title": "One more poll",
    "end_date": "2021-05-13T21:25:46Z",
    "description": "some description for this poll",
  },
  "questions": [
    ...
    {
      "qns": "what the fuck?",
      "qns_type": 1,
      "choices": [
        ...
        {
          "text": "some choice",
        }
      ]
    },
    ...
  ]
}'

Update

You can change one or more fields

Poll

Editable fields type mean
title string Poll title
end_date datetime Date of end poll
description string Description
PATCH http://127.0.0.1:8000/api-polls/v1/polls/{id}/

#Response
{
    "id": 7,
    "author": 1,
    "title": "First poll",
    "start_date": "2020-05-15T11:50:09.293380Z",
    "end_date": "2021-05-13T21:25:46Z",
    "description": "some description for this poll!!!",
    "is_active": true,
    "questions": [
        {
            "id": 19,
            "poll": 7,
            "qns": "what?what?",
            "qns_type": 1,
            "choices": null
        },
        ...
    ]
}

Question

Editable fields type mean
poll int related poll
qns string question
qns_type int 1 - text, 2 - single choice,
3 - multi choice
PATCH http://127.0.0.1:8000/api-polls/v1/question/{id}/

#Response
{
    "id": 19,
    "poll": 7,
    "qns": "what?",
    "qns_type": 1,
    "choices": null
}

Delete

Poll

DELETE http://127.0.0.1:8000/api-polls/v1/polls/{id}/

#Response
HTTP Status 204 No Content

Question

DELETE http://127.0.0.1:8000/api-polls/v1/questions/{id}/

#Response
HTTP Status 204 No Content

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangopollapp-0.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djangopollapp-0.1.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file djangopollapp-0.1.1.tar.gz.

File metadata

  • Download URL: djangopollapp-0.1.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

File hashes

Hashes for djangopollapp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9fc62f01d7d7c84c425b88067588f361bebd670e3db1ba031b242649cc42153a
MD5 08bff2f22be16614e871f9ad9e0da1cc
BLAKE2b-256 6d4a9d23e625b706ec742768c0db156f5c50604a03af87e5d5f1fe5df3398333

See more details on using hashes here.

File details

Details for the file djangopollapp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: djangopollapp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.4

File hashes

Hashes for djangopollapp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 43da8b560076c01a371bd84779cee8b7d82aeaa3d3d12910aea1732ecea156fd
MD5 de728540f81343eba2efcea71f835a23
BLAKE2b-256 22246c3ed133543cbbd96dcf16064ca2d1b191ce1da20d4331cfcbc7e2e57b50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page