Merge pull request #35 from Sosokker/iteration3

Iteration3 Update button status + README.md
This commit is contained in:
Sirin Puenggun 2023-09-14 18:30:51 +07:00 committed by GitHub
commit bab964d08f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 7 deletions

View File

@ -33,8 +33,8 @@ or
4. Run these commands
```bash
python manage.py migrate
python manage.py loaddata data/polls-v1.json
python manage.py loaddata data/user.json
python manage.py loaddata data/polls.json
python manage.py loaddata data/users.json
python manage.py runserver
```

View File

@ -0,0 +1,28 @@
# Generated by Django 4.2.4 on 2023-09-12 09:10
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('polls', '0009_tag_remove_choice_votes_vote_question_tags'),
]
operations = [
migrations.CreateModel(
name='SentimentVote',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('vote_types', models.BooleanField()),
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'unique_together': {('user', 'question')},
},
),
]

View File

@ -9,7 +9,7 @@ Attributes:
None
"""
from django.db import models
from django.db import models, IntegrityError
from django.utils import timezone
from django.contrib import admin
from django.core.validators import MaxValueValidator, MinValueValidator
@ -160,6 +160,36 @@ class Question(models.Model):
"""
return self.vote_set.count()
# ! Most of the code from https://stackoverflow.com/a/70869267
def upvote(self, user):
try:
self.sentimentvote_set.create(user=user, question=self, vote_types=True)
self.up_vote_count += 1
self.save()
except IntegrityError:
vote = self.sentimentvote_set.filter(user=user)
if vote[0].vote_types == False:
vote.update(vote_types=True)
self.save()
else:
return 'already_upvoted'
return 'ok'
def downvote(self, user):
try:
self.sentimentvote_set.create(user=user, question=self, vote_types=False)
self.up_vote_count += 1
self.save()
except IntegrityError:
vote = self.sentimentvote_set.filter(user=user)
if vote[0].vote_types == True:
vote.update(vote_types=False)
self.save()
else:
return 'already_downvoted'
return 'ok'
class Choice(models.Model):
"""
@ -193,4 +223,14 @@ class Vote(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
def __str__(self):
return f"{self.user} voted for {self.choice} in {self.question}"
return f"{self.user} voted for {self.choice} in {self.question}"
# ! Most of the code from https://stackoverflow.com/a/70869267
class SentimentVote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
vote_types = models.BooleanField()
class Meta:
unique_together = ['user', 'question']

View File

@ -61,10 +61,10 @@
{% for choice in question.choice_set.all %}
<label>
{% if choice == selected_choice %}
<input type="radio" name="choice" value="{{ choice.id }}" class="hidden" />
<input type="radio" name="choice" value="{{ choice.id }}" class="hidden" checked="checked"/>
<button
type="button"
class="choice-button selected bg-white-500 border-2 border-black hover:bg-neutral-200 text-black px-4 py-2 rounded-lg shadow-md transition-colors duration-300 w-full py-5 font-bold text-lg truncate"
class="choice-button selected bg-green-500 border-solid border-2 border-black hover:bg-green-600 text-black px-4 py-2 rounded-lg shadow-md transition-colors duration-300 w-full py-5 font-bold text-lg truncate"
onclick="toggleChoice(this, '{{ choice.id }}')">
{{ choice.choice_text }}
</button>

View File

@ -112,8 +112,8 @@ def vote(request, question_id):
messages.success(request, "You vote successfully🥳")
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
else:
messages.success(request, "You vote successfully🥳")
Vote.objects.create(choice=selected_choice, user=request.user, question=question).save()
messages.success(request, "You vote successfully🥳")
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
else:
messages.error(request, "You cannot vote by typing the URL.")