Skip to content

Commit

Permalink
Merge pull request #11 from rohitsinghh01/main
Browse files Browse the repository at this point in the history
fix: multiple votes issue resolved in gymkhana/voting_poll
  • Loading branch information
RishabhSharma96 committed Feb 18, 2024
2 parents 6026782 + d019145 commit 31aa2e9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 125 deletions.
24 changes: 7 additions & 17 deletions FusionIIIT/applications/gymkhana/templatetags/voters_tag.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
from django import template
import re

register = template.Library()

toggel = False


## A tag function to find whether to show the poll to the user or not
@register.simple_tag
def validate(user, groups):
roll = user.username[:3]
roll = user.username[:5]
branch = user.extrainfo.department.name
print(groups)
if roll in groups.keys():
if groups[roll][0] == "All":
if roll in groups:
allowed_branches = groups[roll]
if 'All' in allowed_branches or branch in allowed_branches:
return True
else:
if branch in groups[roll]:
return True
else:
return False
else:
return False


return False

@register.simple_tag
def result():
return toggel
return toggel
186 changes: 78 additions & 108 deletions FusionIIIT/templates/gymkhanaModule/active_poll.html
Original file line number Diff line number Diff line change
@@ -1,117 +1,87 @@
{% load voters_tag %}
{% block active_poll %}
<div class="ui active tab" data-tab="active">
<div class="ui styled fluid accordion">
{% for poll in voting_polls %} {% comment %} {% validate request.user poll.groups as car%}
{% if car %} {% endcomment %}
<div class="title" style="display: flex; align-items: center">
<i class="dropdown icon"></i>
<div
style="
display: flex;
justify-content: space-between;
width: 100%;
align-items: center;
"
>
<p style="margin: 0">{{poll.title}}</p>
{% if poll.exp_date > now %}
<p style="margin: 0">Expired</p>
{% else %}
<p style="margin: 0">Expires in: {{poll.exp_date}} Days</p>
{% endif %}
</div>
</div>
<div class="content">
<p
class="transition hidden"
style="white-space: pre; background-color: aliceblue; padding: 10px"
>
{{poll.desc}}
</p>
<form
action="{% url 'gymkhana:vote' poll.id %}"
method="POST"
id="active"
>
{% csrf_token %} {% if poll.exp_date > now %}
<div
style="
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
margin: 12px 0;
"
>
<p>Results</p>
<p style="padding: 15px; background-color: bisque">{{poll.max}}</p>
</div>
{% else %} {% for choice in poll.choices %}
<div
class="ui grid"
style="box-shadow: 0 0 6.3px 0 rgb(230, 230, 230); margin: 5px"
>
<div class="twelve wide column">{{choice.title}}</div>
<div class="four wide column">
<input
type="radio"
id="{{choice.id}}"
name="choice"
value="{{choice.id}}"
/>
</div>
</div>
{% endfor %} {% endif %}
<div class="ui grid">
<div class="two column row">
<div class="column">
{% if poll.exp_date == 'expire' %}
<button class="ui primary button" id="btn" disabled>
Poll Expired
</button>
{% elif request.user in poll.voters %}
<button class="ui primary button" id="btn" disabled>
Already Voted
</button>
{% else %}
<button class="ui primary button" id="btn">Submit Vote</button>
{% endif %}
</div>
<div class="column">
<p style="margin: 0">
<b>Created By:</b> {{poll.created_by.0}}, {{poll.created_by.1}}
</p>
<div class="ui styled fluid accordion">
{% for poll in voting_polls %}
{% validate request.user poll.groups as can_view %}
{% if can_view %}
<div class="title" style="display: flex; align-items: center">
<i class="dropdown icon"></i>
<div style="display: flex; justify-content: space-between; width: 100%; align-items: center;">
<p style="margin: 0">{{ poll.title }}</p>
{% if poll.exp_date > now %}
<p style="margin: 0">Expired</p>
{% else %}
<p style="margin: 0">Expires in: {{ poll.exp_date }} Days</p>
{% endif %}
</div>
</div>
</div>
</form>
<div class="content">
<p class="transition hidden" style="white-space: pre; background-color: aliceblue; padding: 10px">
{{ poll.desc }}
</p>
<form action="{% url 'gymkhana:vote' poll.id %}" method="POST" id="active">
{% csrf_token %}
{% if poll.exp_date > now %}
<div
style="display: flex; justify-content: center; align-items: center; flex-direction: column; margin: 12px 0;">
<p>Results</p>
<p style="padding: 15px; background-color: bisque">{{ poll.max }}</p>
</div>
{% else %}
{% for choice in poll.choices %}
<div class="ui grid" style="box-shadow: 0 0 6.3px 0 rgb(230, 230, 230); margin: 5px">
<div class="twelve wide column">{{ choice.title }}</div>
<div class="four wide column">
<input type="radio" id="{{ choice.id }}" name="choice" value="{{ choice.id }}">
</div>
</div>
{% endfor %}
{% endif %}
<div class="ui grid">
<div class="two column row">
<div class="column">
{% if poll.exp_date == 'expire' %}
<button class="ui primary button" id="btn" disabled>Poll Expired</button>
{% elif roll in poll.voters %}
<button class="ui primary button" id="btn" disabled>
Already Voted
</button>
{% else %}
<button class="ui primary button" id="btn">Submit Vote</button>
{% endif %}
</div>
<div class="column">
<p style="margin: 0"><b>Created By:</b> {{ poll.created_by.0 }}, {{ poll.created_by.1 }}</p>
</div>
</div>
</div>
</form>
</div>
{% endif %}
{% endfor %}
</div>

{% comment %} {% endif %} {% endcomment %} {% endfor %}
</div>
</div>
<script>
form = document.getElementById("active");
submit_btn = document.getElementById("btn");

submit_btn.addEventListener("click", function (event) {
event.preventDefault();
form = document.getElementById("active");
submit_btn = document.getElementById("btn");

var checked = false;
const rbs = document.querySelectorAll('input[name="choice"]');
let selectedValue;
for (const rb of rbs) {
if (rb.checked) {
checked = true;
break;
}
}
if (!checked) {
alert("Please select one option");
} else {
submit_btn.classList.add("loading");
form.submit();
}
});
submit_btn.addEventListener("click", function (event) {
event.preventDefault();
var checked = false;
const rbs = document.querySelectorAll('input[name="choice"]');
for (const rb of rbs) {
if (rb.checked) {
checked = true;
break;
}
}
if (!checked) {
alert("Please select one option");
} else {
submit_btn.classList.add("loading");
form.submit();
}
});
</script>
{% endblock active_poll %}
{% endblock active_poll %}

0 comments on commit 31aa2e9

Please sign in to comment.