.”,

        "multytasks": "Write new task by + or new line",
        "help": "Help message..."
    }
    self.task_file = "data/tasks.json"
    self.done_tasks = []
    self.undone_tasks = []
def load_tasks(self):
    # create file
    if not os.path.exists(self.task_file):
        with open(self.task_file, 'w') as f:
            f.write(json.dumps([]))
    with open(self.task_file) as f:
        tasks = json.loads(f.read())
        for t in tasks:
            if 'done' in t and int(t['done']) == 1:
                self.done_tasks.append(t)
            else:
                self.undone_tasks.append(t)
def save_tasks(self):
    self.load_tasks()
    self.undone_tasks = sorted(self.undone_tasks, key=lambda x: x['date'])
    self.done_tasks = sorted(self.done_tasks, key=lambda x: x['date'])
    with open(self.task_file, 'w') as f:
        f.write(json.dumps(self.undone_tasks + self.done_tasks))
def add_task(self, chat_id, newtask):
    self.load_tasks()
    # Проверить, что содержимое не пусто
    if not newtask:
        raise Exception('Task empty')
    # Разбить на отдельные задачи
    tasks = newtask.replace(" + ", '+').split("+")
    for t in tasks:
        self.undone_tasks.append({
            'date': time.time(),
            'text': t,
            'chat_id': chat_id
        })
    self.save_tasks()
def show_tasks(self):
    self.load_tasks()
    result = "TODO:\n"
    counter = 1
    for t in self.undone_tasks:
        result += str(counter) + '. ' + t['text'] + "\n"
        counter += 1
    return result
def done_task(self, index):
    self.load_tasks()
    # Проверить, что индекс есть в списке дел
    if int(index) > len(self.undone_tasks) or int(index)  len(self.undone_tasks) or int(index) < 1:
        raise Exception("Incorrect index")
    task = self.undone_tasks[int(index) - 1]
    if task['chat_id'] != chat_id:
        raise Exception("It's not your task")
    del self.undone_tasks[int(index) - 1]
    self.save_tasks()
    return True
def clear_all(self, chat_id):
    self.load_tasks()
    tasks = [t for t in self.undone_tasks if t['chat_id'] == chat_id]
    for t in tasks:
        self.undone_tasks.remove(t)
    self.save_tasks()
def clear_done(self):
    self.load_tasks()
    todaytasks = [t for t in self.done_tasks if date.fromtimestamp(t['date']) == date.today()]
    for t in todaytasks:
        self.done_tasks.remove(t)
    self.save_tasks()
def help(self):
    self.load_tasks()
    return self.texts["help"]
def multy_tasks(self, chat_id, type1):
    self.load_tasks()
    if type1.endswith("+"):
        task = type1.replace('+', ' ')
        self.undone_tasks.append({
            'chat_id': chat_id,
            'text': task,
            'date': time.time()
        })
    else:
        task = type1
        self.undone_tasks.append({
            'chat_id': chat_id,
            'text': task,
            'date': time.time()
        })
    self.save_tasks()

Leave a Reply

Your email address will not be published. Required fields are marked *