mirror of
https://github.com/Sosokker/ku-polls.git
synced 2025-12-18 13:04:05 +01:00
Fix Issue #56 and add min date of form creation
Fix Search show future question
This commit is contained in:
parent
2008c1ea1e
commit
383761dbfa
1
.flake8
1
.flake8
@ -5,6 +5,7 @@ exclude=
|
|||||||
mysite
|
mysite
|
||||||
logs
|
logs
|
||||||
.venv
|
.venv
|
||||||
|
venv
|
||||||
apps.py
|
apps.py
|
||||||
setup.py
|
setup.py
|
||||||
per-file-ignores =
|
per-file-ignores =
|
||||||
|
|||||||
@ -2,7 +2,8 @@ import logging
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.apps import apps
|
# from django.apps import apps
|
||||||
|
from django.utils import timezone
|
||||||
from django.contrib.auth.forms import UserCreationForm
|
from django.contrib.auth.forms import UserCreationForm
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
@ -59,8 +60,12 @@ class PollCreateForm(forms.ModelForm):
|
|||||||
question_text = forms.CharField(min_length=10, max_length=100, required=True,
|
question_text = forms.CharField(min_length=10, max_length=100, required=True,
|
||||||
widget=forms.TextInput(attrs={'class': box_style,
|
widget=forms.TextInput(attrs={'class': box_style,
|
||||||
'placeholder': "What is your question?"}))
|
'placeholder': "What is your question?"}))
|
||||||
pub_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
|
pub_date = forms.DateTimeField(initial=timezone.now, required=True,
|
||||||
end_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
|
widget=forms.DateInput(attrs={'type': 'date',
|
||||||
|
'min': str(timezone.now()).split()[0]}))
|
||||||
|
end_date = forms.DateTimeField(initial=timezone.now()+timezone.timedelta(1),
|
||||||
|
widget=forms.DateInput(attrs={'type': 'date',
|
||||||
|
'min': str(timezone.now()+timezone.timedelta(1)).split()[0]}))
|
||||||
short_description = forms.CharField(max_length=200,
|
short_description = forms.CharField(max_length=200,
|
||||||
widget=forms.TextInput(
|
widget=forms.TextInput(
|
||||||
attrs={'class': box_style,
|
attrs={'class': box_style,
|
||||||
@ -77,12 +82,12 @@ class PollCreateForm(forms.ModelForm):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
Tag = apps.get_model('polls', 'Tag')
|
# Tag = apps.get_model('polls', 'Tag')
|
||||||
|
|
||||||
tags = forms.MultipleChoiceField(
|
# tags = forms.MultipleChoiceField(
|
||||||
choices=[(tag.id, tag.tag_text) for tag in Tag.objects.all()],
|
# choices=[(tag.id, tag.tag_text + "1131") for tag in Tag.objects.all()],
|
||||||
widget=forms.CheckboxSelectMultiple,
|
# widget=forms.CheckboxSelectMultiple,
|
||||||
)
|
# )
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Question
|
model = Question
|
||||||
|
|||||||
@ -14,7 +14,7 @@ class QuestionDetailViewTests(TestCase):
|
|||||||
def test_future_question(self):
|
def test_future_question(self):
|
||||||
"""
|
"""
|
||||||
The detail view of a question with a pub_date in the future
|
The detail view of a question with a pub_date in the future
|
||||||
returns a 404 not found.
|
returns a 302 not found(Redirect to Index).
|
||||||
"""
|
"""
|
||||||
future_question = create_question(question_text="Future question.", day=10)
|
future_question = create_question(question_text="Future question.", day=10)
|
||||||
future_question.save()
|
future_question.save()
|
||||||
@ -23,7 +23,7 @@ class QuestionDetailViewTests(TestCase):
|
|||||||
|
|
||||||
url = reverse("polls:detail", args=(future_question.id,))
|
url = reverse("polls:detail", args=(future_question.id,))
|
||||||
respone = self.client.get(url)
|
respone = self.client.get(url)
|
||||||
self.assertEqual(respone.status_code, 404)
|
self.assertEqual(respone.status_code, 302)
|
||||||
|
|
||||||
def test_past_question(self):
|
def test_past_question(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from django.http import Http404
|
||||||
from django.shortcuts import get_object_or_404, render, redirect
|
from django.shortcuts import get_object_or_404, render, redirect
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@ -51,6 +52,16 @@ class DetailView(LoginRequiredMixin, generic.DetailView):
|
|||||||
model = Question
|
model = Question
|
||||||
template_name = "polls/detail.html"
|
template_name = "polls/detail.html"
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Overide get method, If user search poll that don't avaialable
|
||||||
|
then, redirect to Index Page.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return super().get(request, *args, **kwargs)
|
||||||
|
except Http404:
|
||||||
|
return redirect("polls:index")
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""
|
"""
|
||||||
Excludes any questions that aren't published yet.
|
Excludes any questions that aren't published yet.
|
||||||
@ -212,15 +223,21 @@ def search_poll(request):
|
|||||||
|
|
||||||
results = []
|
results = []
|
||||||
q = ''
|
q = ''
|
||||||
|
now = timezone.now()
|
||||||
if 'q' in request.GET:
|
if 'q' in request.GET:
|
||||||
form = PollSearchForm(request.GET)
|
form = PollSearchForm(request.GET)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
q = form.cleaned_data['q']
|
q = form.cleaned_data['q']
|
||||||
# Case insensitive (icontains)
|
# Case insensitive (icontains)
|
||||||
results = Question.objects.filter(question_text__icontains=q)
|
results = Question.objects.filter(
|
||||||
|
Q(question_text__icontains=q) & Q(pub_date__lte=now)
|
||||||
|
& ((Q(end_date__gte=now) | Q(end_date=None)))
|
||||||
|
)
|
||||||
# * If user search with empty string then show every poll.
|
# * If user search with empty string then show every poll.
|
||||||
if q == '':
|
if q == '':
|
||||||
results = Question.objects.all()
|
results = Question.objects.filter(
|
||||||
|
Q(pub_date__lte=now) & ((Q(end_date__gte=now) | Q(end_date=None)))
|
||||||
|
).order_by("-pub_date")
|
||||||
return render(request, 'polls/search.html', {'form': form, 'results': results, 'q': q})
|
return render(request, 'polls/search.html', {'form': form, 'results': results, 'q': q})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user