From 2ea13da31a1b67ea4967f9d4f7140642127f32f4 Mon Sep 17 00:00:00 2001 From: Jose De Freitas Date: Thu, 1 Apr 2021 17:41:10 -0500 Subject: [PATCH] Set request limits --- src/voter/main.py | 16 ++++++++-------- src/voter/readme.md | 2 +- src/voter/youtubers.sqlite3 | Bin 12288 -> 12288 bytes 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/voter/main.py b/src/voter/main.py index acfe4a5..882a802 100644 --- a/src/voter/main.py +++ b/src/voter/main.py @@ -12,13 +12,10 @@ from flask_limiter.util import get_remote_address app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///youtubers.sqlite3" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + db = SQLAlchemy(app) -limiter = Limiter( - app, - key_func=get_remote_address, - default_limits=["1200 per day", "50 per hour"] -) +limiter = Limiter(app, key_func=get_remote_address,) lock = threading.Lock() @@ -38,12 +35,14 @@ channels_names = [cname.name for cname in channels] # Routes @app.route("/") +@limiter.exempt def index(): """ Main route of the website. """ return "Awesome YouTubers voting system website." @app.route("/channels/all") +@limiter.exempt def list_channels(): """ Lists all channels in the database. """ @@ -51,6 +50,7 @@ def list_channels(): @app.route("/channels/") +@limiter.limit("20 per minute") def get_channel(channel): """ If no query specified, prints the name of the @@ -67,14 +67,13 @@ def get_channel(channel): # Adds/substracts 1 from the channel. if vote == "upvote": cvote.vote += 1 + db.session.commit() elif vote == "downvote": cvote.vote -= 1 + db.session.commit() 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." @@ -86,6 +85,7 @@ def get_channel(channel): @app.route("/channels//image.svg") +@limiter.exempt def img_channel(channel): """ Returns the YouTube score in a svg image. """ diff --git a/src/voter/readme.md b/src/voter/readme.md index 133aa3a..6051e7d 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/youtubers.sqlite3 b/src/voter/youtubers.sqlite3 index 84510d6e0e18e129b0e6376269ec9b3113cfe8e3..decd46583f5481af9bce26d44c89866dc7942e63 100644 GIT binary patch delta 34 pcmZojXh@hK&B!-V#+i|CW5Q`+ZcY{kZc)ad#NtGz&1NE}qyeO32@L=M delta 25 hcmZojXh@hK&B!)U#+i|AW5Q|Si2>Z3%|uQ~0|05S2qypl