From 06e010dbcb36bc677092e8415c90463dee69a622 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Thu, 16 Aug 2018 20:45:03 +0100 Subject: [PATCH] Add ability to change priority of tasks --- bot.py | 43 +++++++++++++++++++++++++++++++++++++++++++ help.md | 3 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index e3a6684..5d573d5 100755 --- a/bot.py +++ b/bot.py @@ -17,6 +17,7 @@ PROPERTY_LAST_COMMAND = "last_command" PROPERTY_LAST_ARGUMENTS = "last_arguments" CONFIG_FILE = 'config.json' +ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' with open(CONFIG_FILE) as file: @@ -79,6 +80,8 @@ def process_command(command, arguments, chat_id): marco(chat_id) elif command == '/delete_all_tasks': delete_all_tasks(chat_id) + elif command == '/priority': + priority(chat_id, arguments) else: set_property(PROPERTY_LAST_COMMAND, '/add', chat_id) set_property(PROPERTY_LAST_ARGUMENTS, arguments, chat_id) @@ -384,6 +387,46 @@ def delete_all_tasks(chat_id): BOT.sendMessage(chat_id, "Deleted all tasks.") +def priority(chat_id, arguments): + """ + Changes the priority of a task + """ + if len(arguments) < 2: + BOT.sendMessage(chat_id, "Not enough arguments: /priority PRIORITY" + "ID-OF-TASK [ID-OF-TASK...]") + return + + priorities = list(ALPHABET) + priorities.append('NONE') + + if arguments[0].upper() not in priorities: + BOT.sendMessage(chat_id, "Priority (first argument) must be letter or" + "'none'") + return + + new_priority = arguments[0].upper() + # This is what no priority is defined as for the sorting + if new_priority == 'NONE': + new_priority = '{' + del arguments[0] + + for i in arguments: + if not i.isnumeric(): + BOT.sendMessage(chat_id, "Task IDs (second argument and beyond)" + "must be integers") + return + + tasks = get_tasks(chat_id) + + for i in arguments: + i = int(i) + BOT.sendMessage(chat_id, "Setting priority of '{}'.".format(tasks[i])) + tasks[i].priority = new_priority + + set_tasks(tasks, chat_id) + + return + if __name__ == "__main__": MessageLoop(BOT, on_message).run_as_thread() diff --git a/help.md b/help.md index 3027a93..beaf253 100644 --- a/help.md +++ b/help.md @@ -2,12 +2,13 @@ - /add - Add a new task - /rm [id [id [id]...]] - Remove task(s) -- /ls [id [id [id]...]] - list all tasks +- /ls [filters] - list all tasks - /do [id [id [id]...]] - do task(s) - /undo [id [id [id]...]] - undo task(s) - /marco - test if bot is up - /help - show help information - /last - run the last command sent - /export - send all tasks as plaintext +- /priority id [id [id [id]...]] - Set the priority of task(s) Anything sent without a command is assumed to be a new task to be added