Issue
As I say in the title, I want to allow a user to vote on an instance named, Course
and lock the vote after 72 hours or 3 days (In my example, it will be 2 minutes). Where I get stuck is that I want to allow users to edit vote if the course has been edited by its owner. When a course being edited, all its votes must be unlocked.
How bad today is, All the logic are gone, I can't think anymore, already tear off multiple sheets of paper!
So far, I can lock a vote, I mean I think I lock it.
''' stuff '''
if vote:
NOW = datetime.datetime.now()
DEADLINE = 2 # minutes
if (NOW - course.date_edited).total_seconds() < (DEADLINE * 60):
vote.amount = amount
vote.save()
else:
vote = Vote.objects.create(
user = user,
amount = amount,
content_object = course,
)
date_edited
for the the 2 models are set to auto_now_add = True
in db
I think another approach is to edit the date for all the votes for a course when editing it, like:
course.votes.all().update(date_voted=....) # I am not sure
let me know how to do it. Thank you in advance!
Solution
You can set auto_now=True
for date_voted
field and add date_vote_created
with auto_now_add=True
. And in your view check if course has been changed after this date also:
if vote:
NOW = datetime.datetime.now()
DEADLINE = 2 # minutes
if course.date_edited > vote.date_voted or (NOW - vote.date_vote_created).total_seconds() < (DEADLINE * 60):
vote.amount = amount
vote.save()
UPD
To unlock vote for two minutes after course has been changed you can reset date_vote_created
in separate if
blocks.
if vote:
NOW = datetime.datetime.now()
DEADLINE = 2 # minutes
if course.date_edited > vote.date_voted:
vote.date_vote_created = NOW
vote.save()
if course.date_edited > vote.date_voted or (NOW - vote.date_vote_created).total_seconds() < (DEADLINE * 60):
vote.amount = amount
vote.save()
UPD #2
if course.date_edited > vote.date_voted:
vote.amount = amount
vote.date_vote_created = NOW
vote.save()
elif (NOW - vote.date_vote_created).total_seconds() < (DEADLINE * 60):
vote.amount = amount
vote.save()
else:
print("Vote locked")
Answered By - neverwalkaloner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.