mirror of
https://github.com/Sosokker/ku-polls.git
synced 2025-12-18 13:04:05 +01:00
Merge pull request #57 from Sosokker/iteration4
Iteration4 Fix Issue #56 Redirect user when access invalid poll / Add minimum date to creation form
This commit is contained in:
commit
00c1f1797b
1
.flake8
1
.flake8
@ -5,6 +5,7 @@ exclude=
|
||||
mysite
|
||||
logs
|
||||
.venv
|
||||
venv
|
||||
apps.py
|
||||
setup.py
|
||||
per-file-ignores =
|
||||
|
||||
@ -2,7 +2,8 @@ import logging
|
||||
from typing import Any
|
||||
|
||||
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.models import User
|
||||
|
||||
@ -59,8 +60,12 @@ class PollCreateForm(forms.ModelForm):
|
||||
question_text = forms.CharField(min_length=10, max_length=100, required=True,
|
||||
widget=forms.TextInput(attrs={'class': box_style,
|
||||
'placeholder': "What is your question?"}))
|
||||
pub_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
|
||||
end_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
|
||||
pub_date = forms.DateTimeField(initial=timezone.now, required=True,
|
||||
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,
|
||||
widget=forms.TextInput(
|
||||
attrs={'class': box_style,
|
||||
@ -77,12 +82,12 @@ class PollCreateForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
Tag = apps.get_model('polls', 'Tag')
|
||||
# Tag = apps.get_model('polls', 'Tag')
|
||||
|
||||
tags = forms.MultipleChoiceField(
|
||||
choices=[(tag.id, tag.tag_text) for tag in Tag.objects.all()],
|
||||
widget=forms.CheckboxSelectMultiple,
|
||||
)
|
||||
# tags = forms.MultipleChoiceField(
|
||||
# choices=[(tag.id, tag.tag_text + "1131") for tag in Tag.objects.all()],
|
||||
# widget=forms.CheckboxSelectMultiple,
|
||||
# )
|
||||
|
||||
class Meta:
|
||||
model = Question
|
||||
|
||||
@ -14,7 +14,7 @@ class QuestionDetailViewTests(TestCase):
|
||||
def test_future_question(self):
|
||||
"""
|
||||
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.save()
|
||||
@ -23,7 +23,7 @@ class QuestionDetailViewTests(TestCase):
|
||||
|
||||
url = reverse("polls:detail", args=(future_question.id,))
|
||||
respone = self.client.get(url)
|
||||
self.assertEqual(respone.status_code, 404)
|
||||
self.assertEqual(respone.status_code, 302)
|
||||
|
||||
def test_past_question(self):
|
||||
"""
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404, render, redirect
|
||||
from django.views import generic
|
||||
from django.utils import timezone
|
||||
@ -51,6 +52,16 @@ class DetailView(LoginRequiredMixin, generic.DetailView):
|
||||
model = Question
|
||||
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):
|
||||
"""
|
||||
Excludes any questions that aren't published yet.
|
||||
@ -212,15 +223,21 @@ def search_poll(request):
|
||||
|
||||
results = []
|
||||
q = ''
|
||||
now = timezone.now()
|
||||
if 'q' in request.GET:
|
||||
form = PollSearchForm(request.GET)
|
||||
if form.is_valid():
|
||||
q = form.cleaned_data['q']
|
||||
# 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 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})
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user