Set data queries

This commit is contained in:
Jose De Freitas 2021-04-01 17:19:16 -05:00
parent 1e24e86d02
commit f3b1cd3333
4 changed files with 26 additions and 19 deletions

View File

@ -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/<channel>")
@ -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"""
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
width="52px" height="22px" viewBox="0 0 52 22" fill="none">
@ -98,7 +103,7 @@ def img_channel(channel):
<rect x="0.5" y="0.5" height="99%" width="51" fill="none"/>
<g>
<text x="5" y="15" fill="#00b4f0" class="text">
{channels[channel]}
{cvote.vote}
</text>
</g>
</svg>
@ -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__":

View File

@ -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.
<img src="https://awesome-youtubers.herokuapp.com//channels/3Blue1Brown/image.svg">
<img src="http://127.0.0.1:5000/channels/Don_Jones/image.svg">

View File

@ -1,4 +1,4 @@
{% extends base.html %}
{% extends 'base.html' %}
{% block content %}
<table>
@ -6,9 +6,11 @@
<th>Name</th>
<th>Vote</th>
</tr>
{% for channel in channels %}
<tr>
<td>{{ channels.name }}</td>
<td>{{ channels.vote }}</td>
<td>{{ channel.name }}</td>
<td>{{ channel.vote }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}

Binary file not shown.