IO_Zhik

Говнокод

Feb 23rd, 2021
836
2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sqlalchemy import create_engine
  2. from sqlalchemy import Column, Integer, String, Date
  3. from sqlalchemy.orm import sessionmaker
  4. from sqlalchemy.ext.declarative import declarative_base
  5.  
  6. import datetime
  7.  
  8. Base = declarative_base()
  9.  
  10.  
  11. class Table(Base):
  12.     __tablename__ = "task"
  13.     id = Column(Integer, primary_key=True)
  14.     task = Column(String)
  15.     deadline = Column(Date, default=datetime.datetime.today())
  16.  
  17.     def __repr__(self):
  18.         return "<Task> " + self.task
  19.  
  20.     def __str__(self):
  21.         return self.task
  22.  
  23.  
  24. def print_menu(items):
  25.     for index, item in enumerate(items):
  26.         print(f"{index+1}) {item}")
  27.     print("0) Exit")
  28.  
  29.  
  30. def print_tasks(tasks, date, is_today=False, is_all_tasks=False, new_line=True, custom_title=None):
  31.     if custom_title is not None:
  32.         title_string = custom_title
  33.     else:
  34.         title_string = date.strftime("%A %e %b").replace('  ', ' ')
  35.  
  36.         if is_today:
  37.             title_string = "Today " + date.strftime("%e %b").replace('  ', ' ')
  38.         elif is_all_tasks:
  39.             title_string = "All tasks"
  40.  
  41.     print(f"{title_string}:")
  42.  
  43.     if len(tasks) > 0:
  44.         for num, task in enumerate(tasks):
  45.             if is_all_tasks:
  46.                 date_string = task.deadline.strftime("%e %b").lstrip()
  47.                 print(f"{num + 1}. {task}. {date_string}")
  48.             else:
  49.                 print(f"{num + 1}. {task}")
  50.     else:
  51.         print("Nothing to do!")
  52.  
  53.     if new_line:
  54.         print()
  55.  
  56.  
  57. engine = create_engine('sqlite:///todo.db?check_same_thread=False')
  58. Base.metadata.create_all(engine)
  59.  
  60. Session = sessionmaker(bind=engine)
  61. session = Session()
  62.  
  63. stage = []
  64. while True:
  65.     if len(stage) == 0:
  66.         print_menu(["Today's tasks", "Week's tasks", "All tasks", "Missed tasks", "Add task", "Delete task"])
  67.         action = input().strip()
  68.         if action == '1':
  69.             stage.append("tasks")
  70.             stage.append("today")
  71.         elif action == '2':
  72.             stage.append("tasks")
  73.             stage.append("week")
  74.         elif action == '3':
  75.             stage.append("tasks")
  76.             stage.append("all")
  77.         elif action == '4':
  78.             stage.append("tasks")
  79.             stage.append("missed")
  80.         elif action == '5':
  81.             stage.append("add_task")
  82.         elif action == '6':
  83.             stage.append("delete")
  84.         elif action == '0':
  85.             stage.append("exit")
  86.  
  87.     elif stage[0] == 'tasks':
  88.         date_from = datetime.datetime.today()
  89.         date_to = None
  90.         base_query = session.query(Table).order_by(Table.deadline)
  91.  
  92.         if stage[1] == 'today':
  93.             tasks = base_query.filter(Table.deadline == date_from.date()).all()
  94.             print_tasks(tasks, date_from, is_today=True)
  95.  
  96.         elif stage[1] == 'week':
  97.             date_to = date_from + datetime.timedelta(days=7)
  98.             current_date = date_from
  99.             while current_date < date_to:
  100.                 tasks = base_query.filter(Table.deadline == current_date.date()).all()
  101.                 print_tasks(tasks, current_date)
  102.                 current_date += datetime.timedelta(days=1)
  103.  
  104.         elif stage[1] == 'all':
  105.             tasks = base_query.all()
  106.             print_tasks(tasks, date_from, is_all_tasks=True)
  107.  
  108.         elif stage[1] == 'missed':
  109.             tasks = base_query.filter(Table.deadline < date_from.date()).all()
  110.             if len(tasks) > 0:
  111.                 print_tasks(tasks, date_from, is_all_tasks=True, custom_title="Missed tasks")
  112.             else:
  113.                 print("Nothing is missed!")
  114.                 print()
  115.  
  116.         stage.clear()
  117.  
  118.     elif stage[0] == 'add_task':
  119.         print("Enter task")
  120.         task_text = input().strip()
  121.  
  122.         print("Enter deadline")
  123.         task_deadline = datetime.datetime.strptime(input().strip(), "%Y-%m-%d")
  124.  
  125.         new_task_row = Table(task=task_text, deadline=task_deadline)
  126.         session.add(new_task_row)
  127.         session.commit()
  128.         print("The task has been added!")
  129.         print()
  130.         stage.pop()
  131.  
  132.     elif stage[0] == 'delete':
  133.         tasks = session.query(Table).order_by(Table.deadline).all()
  134.  
  135.         if len(tasks) > 0:
  136.             title = "Choose the number of the task you want to delete"
  137.             print_tasks(tasks, None, is_all_tasks=True, new_line=False, custom_title=title)
  138.             task_num = int(input().strip())
  139.             session.delete(tasks[task_num - 1])
  140.             session.commit()
  141.             print("The task has been deleted!")
  142.         else:
  143.             print("Nothing to delete")
  144.         print()
  145.         stage.pop()
  146.  
  147.     elif stage[0] == 'exit':
  148.         print("Bye!")
  149.         break
  150.  
RAW Paste Data