JoostSijm 6 жил өмнө
parent
commit
6a9b7153f8

+ 22 - 0
app/models.py

@@ -208,12 +208,34 @@ class Question(db.Model):
     )
 
     def has_voten(self, user_id):
+        """Check if user has voted"""
         for option in self.options:
             if option.votes.filter(Vote.user_id == user_id).first():
                 return True
         return False
 
 
+    def get_option(self, option):
+        """Get the pro option"""
+        return self.options.filter(Option.name == option).first()
+
+
+    def score(self):
+        """Get winning option from question"""
+        if self.combined_approval_voting:
+            pro_option = self.get_option('Voor')
+            pro_votes = pro_option.votes.count()
+            against_option = self.get_option('Tegen')
+            against_votes = against_option.votes.count()
+            return pro_votes - against_votes
+
+        higest = Option()
+        for option in self.options:
+            if higest.votes.count() < option.votes.count():
+                higest = option
+        return higest.name
+
+
 class Option(db.Model):
     """Model for Option"""
     id = db.Column(db.Integer, primary_key=True)

+ 5 - 0
app/modules/vote/templates/vote/public/view.j2

@@ -86,6 +86,11 @@
         {% if question.description %}
         <p class="card-text">{{ question.description }}</p>
         {% endif %}
+        {% if question.combined_approval_voting %}
+        <p class="card-text">Score: {{ question.score() }}</p>
+        {% else %}
+        <p class="card-text">Meeste stemmen: {{ question.score() }}</p>
+        {% endif %}
     </div>
     <ul class="list-group list-group-flush">
         {% for option in question.options %}

+ 5 - 0
app/modules/vote/templates/vote/view.j2

@@ -45,6 +45,11 @@
 				{% if question.description %}
 			    <p class="card-text">{{ question.description }}</p>
 			    {% endif %}
+			    {% if question.combined_approval_voting %}
+			    <p class="card-text">Score: {{ question.score() }}</p>
+			    {% else %}
+			    <p class="card-text">Meeste stemmen: {{ question.score() }}</p>
+			    {% endif %}
 			</div>
 			{% if question.options.all() | count %}
 			<ul class="list-group list-group-flush">