You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0KB

  1. from models.database import db
  2. import mysql.connector
  3. def get_user(username):
  4. """
  5. Get the user with the given username
  6. :param username: The username
  7. :type username: str
  8. :return: user
  9. """
  10. db.connect()
  11. cursor = db.cursor()
  12. query = ("SELECT userid, username, password, login_attempts, last_login_attempt from users where username = %s")
  13. user = None
  14. try:
  15. cursor.execute(query, (username,))
  16. users = cursor.fetchall()
  17. if len(users):
  18. user = users[0]
  19. except mysql.connector.Error as err:
  20. print("Failed executing query: {}".format(err))
  21. cursor.fetchall()
  22. exit(1)
  23. finally:
  24. cursor.close()
  25. db.close()
  26. return user
  27. def get_users():
  28. """
  29. Retreive all registrered users from the database
  30. :return: users
  31. """
  32. db.connect()
  33. cursor = db.cursor()
  34. query = ("SELECT userid, username from users")
  35. try:
  36. cursor.execute(query)
  37. users = cursor.fetchall()
  38. except mysql.connector.Error as err:
  39. print("Failed executing query: {}".format(err))
  40. users = []
  41. cursor.fetchall()
  42. exit(1)
  43. finally:
  44. cursor.close()
  45. db.close()
  46. return users
  47. def set_login_attempts(userid, num, timestamp):
  48. """
  49. Set the number and timestamp of the failed login attempts for the given user.
  50. """
  51. db.connect()
  52. cursor = db.cursor()
  53. query = ("UPDATE users SET login_attempts = %s, last_login_attempt = %s WHERE userid = %s")
  54. try:
  55. cursor.execute(query, (num, timestamp, userid))
  56. db.commit()
  57. except mysql.connector.Error as err:
  58. print("Failed executing query: {}".format(err))
  59. cursor.fetchall()
  60. exit(1)
  61. finally:
  62. cursor.close()
  63. db.close()
  64. def get_user_id_by_name(username):
  65. """
  66. Get the id of the unique username
  67. :param username: Name of the user
  68. :return: The id of the user
  69. """
  70. db.connect()
  71. cursor = db.cursor()
  72. query = ("SELECT userid from users WHERE username = %s")
  73. userid = None
  74. try:
  75. cursor.execute(query, (username,))
  76. users = cursor.fetchall()
  77. if(len(users)):
  78. userid = users[0][0]
  79. except mysql.connector.Error as err:
  80. print("Failed executing query: {}".format(err))
  81. cursor.fetchall()
  82. exit(1)
  83. finally:
  84. cursor.close()
  85. db.close()
  86. return userid
  87. def get_user_name_by_id(userid):
  88. """
  89. Get username from user id
  90. :param userid: The id of the user
  91. :return: The name of the user
  92. """
  93. db.connect()
  94. cursor = db.cursor()
  95. query = ("SELECT username from users WHERE userid = %s")
  96. username = None
  97. try:
  98. cursor.execute(query, (userid,))
  99. users = cursor.fetchall()
  100. if len(users):
  101. username = users[0][0]
  102. except mysql.connector.Error as err:
  103. print("Failed executing query: {}".format(err))
  104. cursor.fetchall()
  105. exit(1)
  106. finally:
  107. cursor.close()
  108. db.close()
  109. return username