Package: djangopollapp
Django Poll AppA simple polls applicationRequirementsPython >= 3.5.2Django 2.2.10Django REST framework 3.11.0InstallationInstall using pip...pipinstalldjangopollappAdd the following to your settings.py module:INSTALLED_APPS=[...'djangopollapp',]MIDDLEWARE=[...'djangopollapp.middleware.PollMiddleware',]Include polls api to your urls.py:# Polls APIurl('^api-polls/',include('djangopollapp.api.urls'))and./manage.pymigrateTesting the SetupExample superuser authentication:curl-XPOST-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-XPOST-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":[]}DocumentationFor clientsUnique IDAt 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 idPollsGet all active pollsGEThttp://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 detalizationGEThttp://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 answerWe transfer the list of objects.
Attributes of each object:question id, question type and selected options / textPOSThttp://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]}...]#ResponseHTTPStatus201OKFor adminsAuthorizationUses standard token authorization from drfAll requests except get require authorizationcurl-d'{"username": <username>,"password": <password>}'-H"Content-Type: application/json"-XPOSThttp://127.0.0.1:8000/api-token-auth/#Response{"token":"dfaef188d5f075802cf7b627a41e4dd3632d127b"}PollsCreateA poll is created in two parts:Poll dataQuestion dataEach question has its own type.1 - Text answer2 - Single Choice3 - Multi ChoiceIf 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 objectsIn the data of each question, we also put choices depending on its typecurlhttp://127.0.0.1:8000/api-polls/v1/polls/\-XPOST\-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",}]},...]}'UpdateYou can change one or more fieldsPollEditable fieldstypemeantitlestringPoll titleend_datedatetimeDate of end polldescriptionstringDescriptionPATCHhttp://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},...]}QuestionEditable fieldstypemeanpollintrelated pollqnsstringquestionqns_typeint1 - text, 2 - single choice,3 - multi choicePATCHhttp://127.0.0.1:8000/api-polls/v1/question/{id}/#Response{"id":19,"poll":7,"qns":"what?","qns_type":1,"choices":null}DeletePollDELETEhttp://127.0.0.1:8000/api-polls/v1/polls/{id}/#ResponseHTTPStatus204NoContentQuestionDELETEhttp://127.0.0.1:8000/api-polls/v1/questions/{id}/#ResponseHTTPStatus204NoContent