Skip to content

Commit

Permalink
Adding Choice model and votes
Browse files Browse the repository at this point in the history
- Adding a Choice model that stores choice options of a poll and votes
- Adding tests
  • Loading branch information
OmerCS8 committed May 1, 2022
1 parent 2efd06d commit af9e157
Show file tree
Hide file tree
Showing 6 changed files with 734 additions and 4 deletions.
3 changes: 2 additions & 1 deletion posts/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.contrib import admin
from .models import Resume, Rating, Comment, Poll, PollFile
from .models import Resume, Rating, Comment, Poll, PollFile, Choice

admin.site.register(Resume)
admin.site.register(Rating)
admin.site.register(Comment)
admin.site.register(Poll)
admin.site.register(PollFile)
admin.site.register(Choice)
25 changes: 25 additions & 0 deletions posts/migrations/0010_choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.0.3 on 2022-04-03 09:50

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),
('posts', '0009_add_rating_test_data'),
]

operations = [
migrations.CreateModel(
name='Choice',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('choice_text', models.CharField(max_length=200)),
('poll', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='posts.poll')),
('voters', models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL)),
],
),
]
28 changes: 28 additions & 0 deletions posts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ class Poll(Post):
def __str__(self):
return f"Poll {self.post_id} by {self.author}"

def get_amount_of_votes(self):
votes_amount = 0
for choice in self.choice_set.all():
votes_amount += choice.get_amount_of_votes()
return votes_amount


# -----------------------------PollFile model that saves a file of a Poll-----------------------------

Expand All @@ -149,3 +155,25 @@ class PollFile(models.Model):

def __str__(self):
return f"File {self.pk} of {self.poll}"


# ------------------------Choice model that stores one choice option of a Poll------------------------

class Choice(models.Model):
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
voters = models.ManyToManyField(User, blank=True)

def __str__(self):
return f"'{self.choice_text}' of {self.poll}"

def get_amount_of_votes(self):
return len(self.voters.all())

# get percentage of voters who voted to this choice
def get_percentage(self):
total_votes_amount = self.poll.get_amount_of_votes()
if total_votes_amount <= 0:
return 0
else:
return (self.get_amount_of_votes() / total_votes_amount) * 100
Loading

0 comments on commit af9e157

Please sign in to comment.