jimMoonrock

Authorization

Jul 12th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. def registration(cursor, login, password, check_user):
  2.     if check_user is None:
  3.         cursor.execute(
  4.             """
  5.              INSERT INTO user (login, password)
  6.              VALUES (?, ?)""", (login, password)
  7.         )
  8.     else:
  9.         print("Thy another user!")
  10.  
  11.  
  12. def authorization(login, password, check_user,cursor):
  13.  
  14.     if check_user is not None:
  15.         while True:
  16.             if check_user['login'] == login:
  17.                 if password == check_user['password']:
  18.  
  19.                     print(f"Welcome, {login}")
  20.                     note(cursor, check_user)
  21.                     break
  22.                 else:
  23.                     print("Please login or password incorrect, try again!!")
  24.                     main()
  25.     else:
  26.         print("Unknown user")
  27.  
  28. def note(cursor, check_user):
  29.     while True:
  30.         user_choice = input("Do you want to create a note or sign out:")
  31.         if user_choice == "create a note":
  32.             note_title = input("Enter the names for the task: ")
  33.             note_text = input("Enter text of the task: ")
  34.             cursor.execute(
  35.                 """
  36.                  INSERT INTO notes (user_id, note_title,note_text)
  37.                  VALUES(?, ?, ?)
  38.              """, (check_user['id'], note_title, note_text))
  39.             continue
  40.         else:
  41.             break
  42.  
  43.  
  44. def main():
  45.     login = input("Input login: ")
  46.     password = sha256(input("Input password: ").encode()).hexdigest()
  47.     input_task = input("You want registration or authorization?: ")
  48.  
  49.     conn = sqlite3.connect("mydatabase.db")
  50.     conn.row_factory = sqlite3.Row
  51.     cursor = conn.cursor()
  52.  
  53.     cursor.execute(
  54.         """
  55.          CREATE TABLE
  56.          IF NOT EXISTS user
  57.          (
  58.              id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  59.              login,
  60.              password
  61.          )
  62.      """
  63.     )
  64.  
  65.     cursor.execute(
  66.         """
  67.          CREATE TABLE
  68.          IF NOT EXISTS notes
  69.          (
  70.              user_Id Integer not null,
  71.              note_title,
  72.              note_text,
  73.              FOREIGN KEY (user_id) REFERENCES information (id)
  74.          )
  75.      """
  76.     )
  77.  
  78.     check_user = cursor.execute(
  79.         """
  80.          SELECT id, login, password
  81.          FROM user
  82.          WHERE login = ?""", (login,)
  83.     ).fetchone()
  84.     print(check_user)
  85.  
  86.     if input_task == "registration":
  87.         registration(cursor, login, password, check_user)
  88.     elif input_task == "authorization":
  89.         authorization(login, password, check_user,cursor)
  90.  
  91.     conn.commit()
  92.     conn.close()
  93.  
  94.  
  95. if __name__ == '__main__':
  96.     main()
Add Comment
Please, Sign In to add comment