Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. from collections import OrderedDict
  2. import datetime
  3. import os
  4.  
  5.  
  6. from peewee import*
  7.  
  8. db = SqliteDatabase('to_do_list.db')
  9.  
  10. class ToDo(Model):
  11. """Model for creating to-do items.
  12. 'done' indicates that it's been completed, 'protected' makes it immune to cleanup"""
  13. task = CharField(max_length=255)
  14. timestamp = DateTimeField(default=datetime.datetime.now)
  15. done = BooleanField(default=False)
  16. protected = BooleanField(default=False)
  17.  
  18. class Meta:
  19. database = db
  20.  
  21. def clear():
  22. """Clear the display"""
  23. os.system('cis' if os.name == 'nt' else 'clear')
  24.  
  25. def initialize():
  26. """Connect to database, build tables if they don't exist"""
  27. db.connect()
  28. db.create_tables([ToDo], safe=True)
  29.  
  30. def view_entries(index, entries, single_entry):
  31. """View to-do list"""
  32. clear()
  33.  
  34. index = index % len(entries) # determines which entry is selected for modification
  35. if single_entry: #to see 1 entry
  36. entries = [entries[index]]
  37. index = 0
  38. else:
  39. print('\nMY TO-DO LIST')
  40. print('=' *40)
  41. prev_timestamp = None
  42.  
  43. for ind, entry in enumerate(entries):
  44. timestamp = entry.timestamp.strftime('%d/%B/%Y')
  45.  
  46. if timestamp != prev_timestamp: #same timestamps get printed only once
  47. print ('\n')
  48. print (timestamp)
  49. print ('=' * len(timestamp))
  50. prev_timestamp = timestamp
  51.  
  52. if ind == index: #placing selection tick
  53. tick = '> '
  54. else:
  55. tick = ' '
  56.  
  57. print('{}{}'.format(tick, entry.task), end='')
  58. if entry.done:
  59. print('\t(DONE)', end='')
  60. if entry.protected:
  61. print('\t <P>', end='')
  62. print('')
  63.  
  64. return entries #so that we can modify the given entry if needed
  65.  
  66. def add_entry(index, entries):
  67. """Add a new task"""
  68.  
  69. new_task = input('\nTo do: ')
  70. if input('Protect [yN? ').lower().strip() == 'y':
  71. protect = True
  72. else:
  73. protect = False
  74. ToDo.create(task=new_task, protected=protect)
  75.  
  76. def modify_entry(index, entries):
  77. """Modify the selected entry"""
  78. entry = view_entries(index, entries, True)[0]
  79. print('\n\n')
  80.  
  81. for key, value in sub_menu.items():
  82. print('{}) {}'.format(key, sub_menu[key].__doc__))
  83. print('q) Back to Main')
  84. next_action = input('Action: ')
  85.  
  86. if next_action.lower().strip() in sub_menu:
  87. sub_menu[next_action](entry)
  88. else:
  89. return
  90.  
  91. def cleanup_entries(index, entries):
  92. """Delete completed non protected entries older than a week"""
  93. if (input('Have you checked that you protected the important stuff? [yN]').lower().strip() == 'y'):
  94. now = datetime.datetime.now()
  95. for entry in entries:
  96. if (now - entry.timestamp > datetime.timedelta(7, 0, 0)and entry.done and not entry_protected):
  97. entry.delete_instance()
  98.  
  99. def modify_task(entry):
  100. """Modify task"""
  101. new_task = input('> ')
  102. entry.task = new_task
  103. entry.save()
  104.  
  105. def delete_entry(entry):
  106. """Erase entry"""
  107. if (input('Are you sure [yN]? ').lower().strip() == 'y'):
  108. entry.delete_instance()
  109.  
  110. def toggle_done(entry):
  111. """Toggle 'DONE'"""
  112. entry.done = not entry.done
  113. entry.save()
  114.  
  115.  
  116. def toggle_protection(entry):
  117. """Toggle 'protected'"""
  118. entry.protected = not entry.protected
  119. entry.save()
  120.  
  121. def menu_loop():
  122. choice = None
  123. index = 0 #shows selected entry
  124. entries = ToDo.select().order_by(ToDo.timestamp.asc())
  125. while choice != 'q':
  126. if len(entries) != 0:
  127. view_entries(index, entries, False)
  128.  
  129. print ('\n' + '=' * 40 + '\n')
  130. print('Previous/Next: p/n \n')
  131. for key, value in main_menu.items():
  132. print ('{}) {}'.format(key, value.__doc__))
  133. print('q) Quit')
  134.  
  135. choice = input ('\nAction: ')
  136. if choice in main_menu:
  137. try:
  138. main_menu[choice](index, entries)
  139. except ZeroDivisionError:
  140. continue
  141. entries = ToDo.select().order_by(ToDo.timestamp.asc()) #update entries after operations
  142.  
  143. elif choice == 'n':
  144. index += 1
  145. elif choice == 'p':
  146. index -= 1
  147.  
  148.  
  149.  
  150. main_menu = OrderedDict([
  151. ('a', add_entry),
  152. ('m', modify_entry),
  153. ('c', cleanup_entries)
  154. ])
  155.  
  156. sub_menu = OrderedDict([
  157. ('m', modify_task),
  158. ('d', toggle_done),
  159. ('p', toggle_protection),
  160. ('e', delete_entry)
  161. ])
  162.  
  163. if __name__ == '__main__':
  164. initialize()
  165. menu_loop()
  166. db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement