Bläddra i källkod

Working voting system

JoostSijm 6 år sedan
förälder
incheckning
2ae9fbe925

+ 3 - 0
app/models.py

@@ -259,6 +259,9 @@ class Code(db.Model):
 
     def get_digest(self, string):
         """Generate digest on string"""
+        if isinstance(string, int):
+            string = str(string)
+        string = string.encode('utf-8')
         secret = self.secret.encode('utf-8')
         blake = blake2b(digest_size=3)
         blake.update(secret + string)

+ 23 - 11
app/modules/vote/app.py

@@ -33,6 +33,20 @@ def main():
     )
 
 
+@login_required
+@BLUEPRINT.route("/codes")
+def codes():
+    """codes overview"""
+    code = Code.query.order_by(Code.expire_date.desc()).first()
+    users = User.query.all()
+
+    return render_template(
+        'vote/codes.j2',
+        users=users,
+        code=code,
+    )
+
+
 @BLUEPRINT.route('/create', methods=["GET", "POST"])
 @login_required
 def create():
@@ -120,25 +134,23 @@ def public(ballot_id):
     """Vote and view results of ballot"""
     ballot = Ballot.query.get(ballot_id)
 
-#   code = Code()
-#   code.secret = "test"
-
-#   db.session.add(code)
-#   db.session.commit()
     if request.method == 'POST':
         security_code = request.form['security_code']
         code = Code.query.order_by(Code.expire_date.desc()).first()
-        print(code.get_digest(code))
         user_id = None
-        for user in User.all():
-            if security_code == code.get_digest(str(user.id)):
+        for user in User.query.all():
+            if security_code == code.get_digest(user.id):
                 user_id = user.id
 
         if user_id is not None:
             for question_id, option_id in request.form.items():
-                if question_id == 'code':
+                if question_id == 'security_code':
                     continue
                 question = Question.query.get(question_id)
+                for option in question.options:
+                    if option.votes.filter(Vote.user_id == user_id).first():
+                        flash('Je hebt al gestemd.', 'warning')
+                        return redirect(url_for('vote.public', ballot_id=ballot.id))
                 option = question.options.filter(Option.id == option_id).first()
 
                 vote = Vote()
@@ -147,9 +159,9 @@ def public(ballot_id):
                 db.session.add(vote)
 
             db.session.commit()
-            flash('Succesvol gestemd', 'success')
+            flash('Succesvol gestemd.', 'success')
         else:
-            flash('Fout in veiligheids code', 'warning')
+            flash('Fout in veiligheids code.', 'warning')
 
     return render_template(
         'vote/public.j2',

+ 38 - 0
app/modules/vote/templates/vote/codes.j2

@@ -0,0 +1,38 @@
+{% extends "layout/backend.j2" %}
+{% block content %}
+<h1>Votes</h1>
+<div class="row">
+    <div class="col-sm">
+        <div class="card">
+            <div class="card-header">
+                Votes
+                <div class="float-right">
+                    <a href="{{ url_for('vote.create') }}" class="btn btn-secondary btn-sm">Create</a>
+                </div>
+            </div>
+            <div class="card-body">
+                <table class="table table-striped table-sm">
+                    <thead>
+                        <tr>
+                            <th>Name</th>
+                            <th>Discord</th>
+                            <th>Rival Regions</th>
+                            <th>Code</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        {% for user in users %}
+                        <tr>
+                            <td>{{ user.name }}</td>
+                            <td>{{ user.discord }}</td>
+                            <td>{{ user.game_id }}</td>
+                            <td>{{ code.get_digest(user.id) }}</td>
+                        </tr>
+                        {%- endfor -%}
+                    </tbody>
+                </table>
+            </div>
+        </div>
+    </div>
+</div>
+{% endblock %}

+ 18 - 2
app/modules/vote/templates/vote/public.j2

@@ -16,28 +16,44 @@
         </div>
     </nav>
     <div class="container mt-3">
+    	{% with messages = get_flashed_messages(with_categories=true) %}
+        {% if messages %}
+        {% for category, message in messages %}
+        <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
+            {{ message }}
+            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
+                <span aria-hidden="true">&times;</span>
+            </button>
+        </div>
+        {% endfor %}
+        {% endif %}
+        {% endwith %}
         <h1>{{ ballot.name }}</h1>
         <table class="table table-sm">
+        	{% if ballot.description %}
             <tr>
                 <th scope="row">Description</th>
                 <td>{{ ballot.description }}</td>
             </tr>
+            {% endif %}
             <tr>
                 <th scope="row">Start</th>
                 <td>{{ ballot.start_at }}</td>
             </tr>
             <tr>
-                <th scope="row">End</th>
+                <th scope="row">Eindigt</th>
                 <td>{{ ballot.end_at }}</td>
             </tr>
             <tr>
-                <th scope="row">User</th>
+                <th scope="row">Aangemaakt door</th>
                 <td>{{ ballot.user.name }}</td>
             </tr>
+            {% if ballot.priority %}
             <tr>
                 <th scope="row">Priority</th>
                 <td>{{ ballot.priority.name }}</td>
             </tr>
+            {% endif %}
         </table>
         {% if ballot.active() %}
         <form method="post">