Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import argv
- import psycopg2
- def init():
- connection = psycopg2.connect("dbname=todo user=postgres password=Password!")
- cursor = connection.cursor()
- cursor.execute("""
- CREATE TABLE todos (
- id SERIAL PRIMARY KEY,
- title TEXT NOT NULL,
- created_at TIMESTAMP DEFAULT NOW(),
- is_done BOOLEAN DEFAULT FALSE
- )
- """)
- connection.commit()
- connection.close()
- class TodoDB:
- def __init__(self, dsn:str):
- self.dsn = dsn
- def connect(self):
- return psycopg2.connect(self.dsn)
- def add_todo(self, title):
- with self.connect() as conn:
- cursor = conn.cursor()
- cursor.execute("INSERT INTO todos (title) VALUES(%s)", (title,))
- conn.commit()
- def get_todos(self):
- with self.connect() as conn:
- cursor = conn.cursor()
- cursor.execute("SELECT id, title, created_at, is_done FROM todos order by created_at DESC")
- return cursor.fetchall()
- def set_is_done(self, todo_id):
- with self.connect() as conn:
- cursor = conn.cursor()
- cursor.execute("UPDATE todos SET is_done=true WHERE id=%s", (todo_id,))
- conn.commit()
- if len(argv) > 1 and argv[1] == "setup":
- init()
- database = TodoDB("dbname=todo user=postgres password=Password!")
- for todo_id, title, created_at, is_done in database.get_todos():
- if is_done:
- text_is_done = "✔️"
- else:
- text_is_done = "❌"
- print(f"{text_is_done} Zadanie nr {todo_id}: {title}")
- print("---" * 10)
- action = input("Co chcesz zrobić? d- dodaj, z - oznacz jako zrobione: ")
- if action == "d":
- title = input("Co masz do zrobienia? ")
- database.add_todo(title)
- elif action == "z":
- todo_id = int(input("Które zadanie zrobiłeś? "))
- database.set_is_done(todo_id)
- else:
- print("Niepoprawny wybór")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement