from crontab import CronTab import logging def post(user=None, task_id=None): """ Remove task Parameters ---------- user: str User id task_id: str Task id Returns ------- result: list of str and bool result[0]: message result[1]: bool if it is deleted or not Notes ----- % in any of string make script falls. Examples -------- >>> import requests >>> import json >>> params = {"user": 'test', task_id:'88657y!'} >>> requests.post(url='/MG/routines/remove', json=params) ['Your task has successfully removed', True] """ user_taskid = "{}:{}".format(user, task_id) user_cron = CronTab(user=True) jobs = [job for job in user_cron.find_comment(user_taskid)] if len(jobs) > 1: logging.warning('There are two task with the same id, {}.'.format(task_id)) result = ['Your task could not be removed.', False] elif len(jobs) == 0: result = ['Wrong task id. This task does not exists.', False] else: user_cron.remove(jobs[0]) user_cron.write() result = ['Your task has successfully removed', True] return result