mirror of
https://github.com/Sosokker/ku-polls.git
synced 2025-12-19 13:34:05 +01:00
Merge pull request #35 from Sosokker/iteration3
Iteration3 Update button status + README.md
This commit is contained in:
commit
bab964d08f
@ -33,8 +33,8 @@ or
|
|||||||
4. Run these commands
|
4. Run these commands
|
||||||
```bash
|
```bash
|
||||||
python manage.py migrate
|
python manage.py migrate
|
||||||
python manage.py loaddata data/polls-v1.json
|
python manage.py loaddata data/polls.json
|
||||||
python manage.py loaddata data/user.json
|
python manage.py loaddata data/users.json
|
||||||
python manage.py runserver
|
python manage.py runserver
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
28
polls/migrations/0010_sentimentvote.py
Normal file
28
polls/migrations/0010_sentimentvote.py
Normal 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')},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -9,7 +9,7 @@ Attributes:
|
|||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models, IntegrityError
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||||
@ -160,6 +160,36 @@ class Question(models.Model):
|
|||||||
"""
|
"""
|
||||||
return self.vote_set.count()
|
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):
|
class Choice(models.Model):
|
||||||
"""
|
"""
|
||||||
@ -194,3 +224,13 @@ class Vote(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
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']
|
||||||
@ -61,10 +61,10 @@
|
|||||||
{% for choice in question.choice_set.all %}
|
{% for choice in question.choice_set.all %}
|
||||||
<label>
|
<label>
|
||||||
{% if choice == selected_choice %}
|
{% 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
|
<button
|
||||||
type="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 }}')">
|
onclick="toggleChoice(this, '{{ choice.id }}')">
|
||||||
{{ choice.choice_text }}
|
{{ choice.choice_text }}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@ -112,8 +112,8 @@ def vote(request, question_id):
|
|||||||
messages.success(request, "You vote successfully🥳")
|
messages.success(request, "You vote successfully🥳")
|
||||||
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
||||||
else:
|
else:
|
||||||
messages.success(request, "You vote successfully🥳")
|
|
||||||
Vote.objects.create(choice=selected_choice, user=request.user, question=question).save()
|
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,)))
|
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
||||||
else:
|
else:
|
||||||
messages.error(request, "You cannot vote by typing the URL.")
|
messages.error(request, "You cannot vote by typing the URL.")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user