From f3b1cd33336bde0226729d35d633c107083fc8eb Mon Sep 17 00:00:00 2001 From: Jose De Freitas Date: Thu, 1 Apr 2021 17:19:16 -0500 Subject: [PATCH] Set data queries --- src/voter/main.py | 35 ++++++++++++++++++++--------------- src/voter/readme.md | 2 +- src/voter/templates/all.html | 8 +++++--- src/voter/youtubers.sqlite3 | Bin 12288 -> 12288 bytes 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/voter/main.py b/src/voter/main.py index 1588b25..acfe4a5 100644 --- a/src/voter/main.py +++ b/src/voter/main.py @@ -1,8 +1,7 @@ import threading from flask import ( - Flask, jsonify, - make_response, request, - render_template + Flask, make_response, + request, render_template ) from flask_sqlalchemy import SQLAlchemy from flask_limiter import Limiter @@ -23,8 +22,8 @@ limiter = Limiter( lock = threading.Lock() -# Database model +# Database model class Channel(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -32,6 +31,12 @@ class Channel(db.Model): vote = db.Column(db.Integer, default=0, nullable=False) +channels = Channel.query.order_by(Channel.name).all() +channels_names = [cname.name for cname in channels] + + +# Routes + @app.route("/") def index(): """ Main route of the website. """ @@ -42,10 +47,7 @@ def index(): def list_channels(): """ Lists all channels in the database. """ - return render_template( - "all.html", - channels=Channel.query.order_by(Channel.name).all() - ) + return render_template("all.html", channels=channels) @app.route("/channels/") @@ -60,22 +62,24 @@ def get_channel(channel): if "vote" in request.args: vote = str(request.args["vote"]) - if channel in channels: + if channel in channels_names: + cvote = Channel.query.filter_by(name=channel).first() # Adds/substracts 1 from the channel. if vote == "upvote": - channels[channel] += 1 + cvote.vote += 1 elif vote == "downvote": - channels[channel] -= 1 + cvote.vote -= 1 else: return "Vote word not recognised." # Write to database file. + db.session.commit() return f"You {vote}d successfully the channel {channel}." else: return "Channel not found on the list." else: - if channel in Channel.query.order_by(Channel.name).all(): + if channel in channels_names: return "Channel: " + channel else: return "Channel not found on the list." @@ -85,7 +89,8 @@ def get_channel(channel): def img_channel(channel): """ Returns the YouTube score in a svg image. """ - if channel in Channel.query.order_by(Channel.name).all(): + if channel in channels_names: + cvote = Channel.query.filter_by(name=channel).first() svg_image = f""" @@ -98,7 +103,7 @@ def img_channel(channel): - {channels[channel]} + {cvote.vote} @@ -108,7 +113,7 @@ def img_channel(channel): response.headers.set('Content-Type', 'image/svg+xml') return response else: - return "Channel not found on the list" + return "Channel not found on the list." if __name__ == "__main__": diff --git a/src/voter/readme.md b/src/voter/readme.md index 532787d..133aa3a 100644 --- a/src/voter/readme.md +++ b/src/voter/readme.md @@ -32,4 +32,4 @@ video quality, coverage of the topics it explains, among others), but, of course so I think being able to receive opinions from all users is a good way to filter and have truly awesome content based on the community's opinion. - \ No newline at end of file + \ No newline at end of file diff --git a/src/voter/templates/all.html b/src/voter/templates/all.html index 49b74b8..0f196b1 100644 --- a/src/voter/templates/all.html +++ b/src/voter/templates/all.html @@ -1,4 +1,4 @@ -{% extends base.html %} +{% extends 'base.html' %} {% block content %} @@ -6,9 +6,11 @@ + {% for channel in channels %} - - + + + {% endfor %}
Name Vote
{{ channels.name }}{{ channels.vote }}{{ channel.name }}{{ channel.vote }}
{% endblock %} \ No newline at end of file diff --git a/src/voter/youtubers.sqlite3 b/src/voter/youtubers.sqlite3 index 3b2572b863031bbd66acfb74641a5fee89b494af..84510d6e0e18e129b0e6376269ec9b3113cfe8e3 100644 GIT binary patch delta 17 YcmZojXh@hK&B!)U#+i|AW5NP`05Nd|N&o-= delta 17 YcmZojXh@hK&B!!S#+i|6W5NP`05LTMLI3~&