Skip to content

Commit

Permalink
added comments and docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahtisham.shahid authored and Ahtisham.shahid committed Aug 6, 2019
1 parent 23fa62a commit b0ec38d
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 44 deletions.
52 changes: 26 additions & 26 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#language: python
#python:
# - "3.6"
## - "3.7"
#services:
# - mysql
#env:
# -DJANGO=2.2 DB=mysql
#install: # => 5
# - pip install -r requirements.txt
#before_script: # => 6
# - mysql -e 'create database test;' -u root
#script: # => 7
# - python manage.py test
# - pylint --load-plugins pylint_django polls/
# - pylint --load-plugins pylint_django posts/


language: python
python:
- "3.6"
# - "3.7"
services:
- docker
before_install:
- docker-compose up
#before_script: # => 6
# - mysql -e 'create database test;' -u root
- mysql
env:
-DJANGO=2.2 DB=mysql
install: # => 5
- pip install -r requirements.txt
before_script: # => 6
- mysql -e 'create database test;' -u root
script: # => 7
- docker exec dockerdajango_web_1 python manage.py test
- docker exec dockerdajango_web_1 pylint --load-plugins pylint_django polls/
- docker exec dockerdajango_web_1 pylint --load-plugins pylint_django posts/
- python manage.py test
- pylint --load-plugins pylint_django polls/
- pylint --load-plugins pylint_django posts/


#services:
# - docker
#before_install:
# - docker-compose up
##before_script: # => 6
## - mysql -e 'create database test;' -u root
#script: # => 7
# - docker exec dockerdajango_web_1 python manage.py test
# - docker exec dockerdajango_web_1 pylint --load-plugins pylint_django polls/
# - docker exec dockerdajango_web_1 pylint --load-plugins pylint_django posts/
11 changes: 9 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ services:
db:
image: mysql:5.7
ports:
- '3308:3308'
- '3306:3306'
environment:
MYSQL_DATABASE: 'django'
# MYSQL_USER: 'root'
MYSQL_PASSWORD: 'root'
MYSQL_ROOT_PASSWORD: 'root'
volumes:
- .:/mysql

web:
build: .
command: python manage.py runserver 0.0.0.0:8000
command: >
bash -c "sleep 10
&& python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000"
# sleep 10 ; python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db

1 change: 1 addition & 0 deletions myproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand Down
10 changes: 8 additions & 2 deletions polls/admin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"""Edit and add admin panel modules"""

from django.contrib import admin
from .models import Choice, Question


class ChoiceInline(admin.TabularInline):
"""To add choice field in admin panel in question editor"""

model = Choice
extra = 3


class QuestionAdmin(admin.ModelAdmin):
"""Updated question view for admin"""

fieldsets = [
(None, {'fields': ['question_text']}),
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
Expand All @@ -18,4 +24,4 @@ class QuestionAdmin(admin.ModelAdmin):
search_fields = ['question_text']


admin.site.register(Question, QuestionAdmin)
admin.site.register(Question, QuestionAdmin)
6 changes: 6 additions & 0 deletions polls/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
"""
This is App config
"""
from django.apps import AppConfig


class PollsConfig(AppConfig):
"""
Polls config class
"""
name = 'polls'
21 changes: 19 additions & 2 deletions polls/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
"""
Models are created here
"""
import datetime
from django.db import models
from django.contrib import admin
from django.utils import timezone
import datetime


class Question(models.Model):
"""
Question Model
"""
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
"""
String representation of class
"""
return self.question_text

def was_published_recently(self):
"""
To test if is recently published
"""
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now

Expand All @@ -21,11 +32,17 @@ def was_published_recently(self):


class Choice(models.Model):
"""
Choice Model
"""
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self):
"""
String representation of class
"""
return self.choice_text

# class ChoiceInline(admin.TabularInline):
Expand Down
29 changes: 25 additions & 4 deletions polls/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
This is application testing module
"""

import datetime

from django.test import TestCase
Expand All @@ -7,6 +11,9 @@


class QuestionModelTests(TestCase):
"""
Question Model Tests.
"""

def test_was_published_recently_with_future_question(self):
"""
Expand Down Expand Up @@ -47,6 +54,10 @@ def create_question(question_text, days):


class QuestionIndexViewTests(TestCase):
"""
Question Model Tests for index.
"""

def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
Expand Down Expand Up @@ -105,6 +116,10 @@ def test_two_past_questions(self):


class QuestionDetailViewTests(TestCase):
"""
Question Detail view Tests.
"""

def test_future_question(self):
"""
The detail view of a question with a pub_date in the future
Expand All @@ -127,6 +142,10 @@ def test_past_question(self):


class Vote(TestCase):
"""
Vote view Tests.
"""

def test_not_question_exists(self):
"""
The detail view of a question with a pub_date in the future
Expand All @@ -143,18 +162,20 @@ def test_question_without_choice_exists(self):
"""
question = create_question(question_text='Future question.', days=-5)
url = reverse('polls:vote', args=(question.id,))
response = self.client.post(url, {'choice': 1})
self.client.post(url, {'choice': 1})
self.assertRaises(Choice.DoesNotExist)

def test_question_exists(self):
"""
test_question_exists
"""
question = create_question(question_text='Future question.', days=-5)
choice1 = Choice.objects.create(question_id=question.id, choice_text='text1')
Choice.objects.create(question_id=question.id, choice_text='text1')
choice2 = Choice.objects.create(question_id=question.id, choice_text='text2')

url = reverse('polls:vote', args=(question.id,))
response = self.client.post(url, {'choice': choice2.id})
# choice_after_update = Choice.objects.filter(id=choice2.id)
# print(choice_after_update.votes,'dasdasd asd asdas')
# self.assertEqual(choice_after_update.votes, 1)
self.assertRedirects(response, reverse('polls:results', args=(question.id,)), status_code=302,
target_status_code=200)
self.assertRedirects(response, reverse('polls:results', args=(question.id,)), 302, 200)
19 changes: 17 additions & 2 deletions polls/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
"""Polls URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path

from . import views

app_name='polls'
app_name = 'polls'

urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
]
20 changes: 14 additions & 6 deletions polls/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Application views"""

from django.shortcuts import render, get_object_or_404, reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseRedirect
from django.utils import timezone
from django.views import generic

from .models import Question, Choice
from django.template import loader
from django.views import generic


# #for index page
Expand Down Expand Up @@ -49,7 +50,8 @@
# def get_queryset(self):
# return Question.objects.order_by('-pub_date')[:5]

class IndexView(generic.ListView):
class IndexView(generic.ListView): # pylint: disable=too-many-ancestors
"""Return Index template"""
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'

Expand All @@ -58,7 +60,9 @@ def get_queryset(self):
return Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5]


class DetailView(generic.DetailView):
class DetailView(generic.DetailView): # pylint: disable=too-many-ancestors
"""Return question detail"""

model = Question
template_name = 'polls/detail.html'

Expand All @@ -69,13 +73,17 @@ def get_queryset(self):
return Question.objects.filter(pub_date__lte=timezone.now())


class ResultsView(generic.DetailView):
class ResultsView(generic.DetailView): # pylint: disable=too-many-ancestors
"""Return Index results"""

model = Question
template_name = 'polls/results.html'


# update choice table and increment vote
def vote(request, question_id):
"""Increment vote of question"""

question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
Expand Down

0 comments on commit b0ec38d

Please sign in to comment.