Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Choice model and votes #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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