From 58f800271bc74d174bb4306c7ed8098cf5b1e569 Mon Sep 17 00:00:00 2001 From: Aevann Date: Mon, 4 Sep 2023 18:23:28 +0300 Subject: [PATCH] handle unique violation error --- files/routes/votes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/files/routes/votes.py b/files/routes/votes.py index 188721d8f..593650d09 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -6,6 +6,7 @@ from files.helpers.alerts import * from files.routes.wrappers import * from files.__main__ import app, limiter from files.routes.routehelpers import get_alt_graph +from sqlalchemy.exc import IntegrityError from math import floor from datetime import datetime @@ -97,7 +98,12 @@ def vote_post_comment(target_id, new, v, cls, vote_cls): # this is hacky but it works, we should probably do better later def get_vote_count(dir, real_instead_of_dir): - g.db.flush() + try: g.db.flush() + except IntegrityError as e: + if str(e).startswith('(psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "'): + abort(400, "You already voted on this!") + raise + votes = g.db.query(vote_cls) if real_instead_of_dir: votes = votes.filter(vote_cls.real == True)