Advertisement
mbratanov

Untitled

Jan 11th, 2025
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. from abc import abstractmethod, ABC
  2. import psycopg2
  3.  
  4.  
  5.  
  6. class DatabaseInterface(ABC):
  7.  
  8.     @abstractmethod
  9.     def create_table(self):
  10.         pass
  11.  
  12.     @abstractmethod
  13.     def save_user(self, email_address: str, hashed_password: str):
  14.         pass
  15.  
  16.     @abstractmethod
  17.     def get_password_by_email(self, email_address: str):
  18.         pass
  19.  
  20. class Database(DatabaseInterface):
  21.     def __init__(self):
  22.         self.host = "localhost"
  23.         self.port = "5432"
  24.         self.database = "postgres"
  25.         self.user = "postgres"
  26.         self.password = "<PASSWORD>"
  27.         self.connection = None
  28.         self.create_table()
  29.  
  30.     def connect(self):
  31.         self.connection = psycopg2.connect(host=self.host,
  32.                                            port=self.port,
  33.                                            database=self.database,
  34.                                            user=self.user,
  35.                                            password=self.password)
  36.  
  37.     def close(self):
  38.         if self.connection:
  39.             self.connection.close()
  40.             self.connection = None
  41.  
  42.     def __enter__(self):
  43.         self.connect()
  44.         return self
  45.  
  46.     def __exit__(self, exc_type, exc_val, exc_tb):
  47.         self.close()
  48.  
  49.     def create_table(self):
  50.         self.connect()
  51.  
  52.         try:
  53.             cursor = self.connection.cursor()
  54.             query = """
  55.            CREATE TABLE IF NOT EXISTS users (
  56.                id SERIAL PRIMARY KEY,
  57.                email_address TEXT UNIQUE NOT NULL,
  58.                password TEXT NOT NULL)
  59.            """
  60.             cursor.execute(query)
  61.             self.connection.commit()
  62.         finally:
  63.             self.close()
  64.  
  65.     def save_user(self, email_address: str, hashed_password: str):
  66.         try:
  67.             with self as db:
  68.                 cursor = db.connection.cursor()
  69.                 query = """INSERT INTO users (email_address, password) VALUES (%s, %s)"""
  70.                 cursor.execute(query, (email_address, hashed_password))
  71.                 db.connection.commit()
  72.         except psycopg2.Error as e:
  73.             print(str(e))
  74.  
  75.     def get_password_by_email(self, email_address: str):
  76.         with self as db:
  77.             cursor = db.connection.cursor()
  78.             query = """SELECT password FROM users WHERE email_address = %s"""
  79.             parameters = (email_address,)
  80.             cursor.execute(query, parameters)
  81.             result = cursor.fetchone()
  82.             if result is None:
  83.                 raise ValueError(f"No user found with e-mail address '{email_address}'.")
  84.             return result[0]
  85.  
  86. if __name__ == '__main__':
  87.     with Database() as my_db:
  88.         my_db.create_table()
  89.         my_db.save_user("test@test.com", "my_hashed_password")
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement