-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
you cannot change your vote within 30 days.
- Loading branch information
Showing
2 changed files
with
7 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
from datetime import timedelta | ||
|
||
from django.contrib import messages | ||
from django.http import HttpResponseRedirect | ||
from django.shortcuts import get_object_or_404 | ||
from django.urls import reverse | ||
from django.utils import timezone | ||
from django.http import HttpResponseForbidden | ||
from poll_app.models import Question, Vote | ||
|
||
|
||
def can_change_vote(view_func): | ||
def _wrapped_view(request, *args, **kwargs): | ||
slug = kwargs.get('slug') | ||
question = get_object_or_404(Question, slug=kwargs['slug']) | ||
user_vote = Vote.objects.filter(voter=request.user, question=question).first() | ||
|
||
if user_vote: | ||
# Если голос был отдан менее 30 дней назад | ||
time_difference = timezone.now() - user_vote.created_at | ||
if time_difference < timedelta(days=30): | ||
# Запрещаем изменение голоса | ||
return HttpResponseForbidden("You cannot change your vote within 30 days.") | ||
|
||
# Если условия не нарушены, выполняем оригинальную функцию | ||
messages.error(request, "You cannot change your vote within 30 days.") | ||
return HttpResponseRedirect(reverse('poll_app:results', args=(slug,))) | ||
return view_func(request, *args, **kwargs) | ||
|
||
return _wrapped_view |