Fix exception when trying to register existing user

This commit is contained in:
David Hoppenbrouwers
2022-10-08 15:59:58 +02:00
parent e093a30157
commit 6a6c5eb804
2 changed files with 21 additions and 15 deletions

View File

@@ -51,8 +51,10 @@ class DB:
).fetchone() ).fetchone()
comments = db.execute(''' comments = db.execute('''
select comment_id, parent_id, author_id, name, text, create_time, modify_time select comment_id, parent_id, author_id, name, text, create_time, modify_time
from comments, users from comments
where thread_id = ? and author_id = user_id left join users
on author_id = user_id
where thread_id = ?
''', ''',
(thread,) (thread,)
) )
@@ -293,18 +295,22 @@ class DB:
return False return False
def add_user(self, username, password, time): def add_user(self, username, password, time):
db = self._db() try:
c = db.cursor() db = self._db()
c.execute(''' c = db.cursor()
insert into users(name, password, join_time) c.execute('''
values (lower(?), ?, ?) insert into users(name, password, join_time)
''', values (lower(?), ?, ?)
(username, password, time) ''',
) (username, password, time)
if c.rowcount > 0: )
db.commit() if c.rowcount > 0:
return True db.commit()
return False return True
return False
except sqlite3.IntegrityError:
# User already exists, probably
return False
def _db(self): def _db(self):
return sqlite3.connect(self.conn) return sqlite3.connect(self.conn)

View File

@@ -274,7 +274,7 @@ def register():
): ):
flash('CAPTCHA answer is incorrect', 'error') flash('CAPTCHA answer is incorrect', 'error')
elif not db.add_user(username, hash_password(password), time.time_ns()): elif not db.add_user(username, hash_password(password), time.time_ns()):
flash('Failed to create user (username may already be taken)') flash('Failed to create account (username may already be taken)', 'error')
else: else:
flash('Account has been created. You can login now.', 'success') flash('Account has been created. You can login now.', 'success')
return redirect(url_for('index')) return redirect(url_for('index'))