Fix exception when trying to register existing user
This commit is contained in:
34
db/sqlite.py
34
db/sqlite.py
@@ -51,8 +51,10 @@ class DB:
|
||||
).fetchone()
|
||||
comments = db.execute('''
|
||||
select comment_id, parent_id, author_id, name, text, create_time, modify_time
|
||||
from comments, users
|
||||
where thread_id = ? and author_id = user_id
|
||||
from comments
|
||||
left join users
|
||||
on author_id = user_id
|
||||
where thread_id = ?
|
||||
''',
|
||||
(thread,)
|
||||
)
|
||||
@@ -293,18 +295,22 @@ class DB:
|
||||
return False
|
||||
|
||||
def add_user(self, username, password, time):
|
||||
db = self._db()
|
||||
c = db.cursor()
|
||||
c.execute('''
|
||||
insert into users(name, password, join_time)
|
||||
values (lower(?), ?, ?)
|
||||
''',
|
||||
(username, password, time)
|
||||
)
|
||||
if c.rowcount > 0:
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
try:
|
||||
db = self._db()
|
||||
c = db.cursor()
|
||||
c.execute('''
|
||||
insert into users(name, password, join_time)
|
||||
values (lower(?), ?, ?)
|
||||
''',
|
||||
(username, password, time)
|
||||
)
|
||||
if c.rowcount > 0:
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
except sqlite3.IntegrityError:
|
||||
# User already exists, probably
|
||||
return False
|
||||
|
||||
def _db(self):
|
||||
return sqlite3.connect(self.conn)
|
||||
|
||||
2
main.py
2
main.py
@@ -274,7 +274,7 @@ def register():
|
||||
):
|
||||
flash('CAPTCHA answer is incorrect', 'error')
|
||||
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:
|
||||
flash('Account has been created. You can login now.', 'success')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
Reference in New Issue
Block a user