2019-05-03 18:31:09 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import utils
|
2019-05-19 06:53:26 +03:00
|
|
|
from time import time
|
|
|
|
|
|
|
|
# Package database
|
|
|
|
db = utils.db()['db']
|
|
|
|
|
|
|
|
# Lists of the module table
|
|
|
|
lists = db.table('todo_lists')
|
|
|
|
|
|
|
|
# Query
|
|
|
|
List = utils.db()['query']()
|
2019-05-03 18:31:09 +03:00
|
|
|
|
|
|
|
def create_list(string, entities):
|
2019-05-19 06:53:26 +03:00
|
|
|
"""Create a to-do list"""
|
|
|
|
|
|
|
|
# List name
|
|
|
|
listname = ''
|
|
|
|
|
|
|
|
# Find entities
|
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'list':
|
2019-05-19 09:47:42 +03:00
|
|
|
listname = item['sourceText'].lower()
|
2019-05-19 06:53:26 +03:00
|
|
|
|
|
|
|
# Verify if a list name has been provided
|
|
|
|
if not listname:
|
2019-05-19 11:16:34 +03:00
|
|
|
return utils.output('end', 'list_not_provided', utils.translate('list_not_provided'))
|
2019-05-19 06:53:26 +03:00
|
|
|
|
|
|
|
# Verify if list already exists or not
|
|
|
|
if lists.count(List.name == listname) > 0:
|
|
|
|
return utils.output('end', 'list_already_exists', utils.translate('list_already_exists', { 'list': listname }))
|
|
|
|
|
2019-05-19 07:42:46 +03:00
|
|
|
timestamp = int(time())
|
2019-05-19 06:53:26 +03:00
|
|
|
# Create the new to-do list
|
|
|
|
lists.insert({
|
|
|
|
'name': listname,
|
|
|
|
'todos': [],
|
2019-05-19 07:42:46 +03:00
|
|
|
'created_at': timestamp,
|
|
|
|
'updated_at': timestamp
|
2019-05-19 06:53:26 +03:00
|
|
|
})
|
2019-05-03 18:31:09 +03:00
|
|
|
|
2019-05-19 06:53:26 +03:00
|
|
|
return utils.output('end', 'list_created', utils.translate('list_created', { 'list': listname }))
|
2019-05-04 04:26:11 +03:00
|
|
|
|
2019-05-19 07:42:46 +03:00
|
|
|
def view_lists(string, entities):
|
|
|
|
"""View to-do lists"""
|
|
|
|
|
2019-05-19 09:30:53 +03:00
|
|
|
# Lists number
|
|
|
|
lists_nb = len(lists)
|
|
|
|
|
|
|
|
# Verify if a list exists
|
|
|
|
if lists_nb == 0:
|
|
|
|
return utils.output('end', 'no_list', utils.translate('no_list'))
|
|
|
|
|
|
|
|
result = ''
|
2019-05-19 11:30:21 +03:00
|
|
|
# Fill end-result
|
2019-05-19 09:30:53 +03:00
|
|
|
for listelement in lists:
|
|
|
|
result += utils.translate('list_list_element', {
|
|
|
|
'list': listelement['name'],
|
|
|
|
'todos_nb': len(listelement['todos'])
|
|
|
|
})
|
|
|
|
|
2019-05-19 11:16:34 +03:00
|
|
|
return utils.output('end', 'lists_listed', utils.translate('list_list', {
|
2019-05-19 09:30:53 +03:00
|
|
|
'lists_nb': lists_nb,
|
|
|
|
'result': result
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2019-05-19 07:42:46 +03:00
|
|
|
|
2019-05-04 04:26:11 +03:00
|
|
|
def rename_list(string, entities):
|
2019-05-19 07:42:46 +03:00
|
|
|
"""Rename a to-do list"""
|
|
|
|
|
|
|
|
# Old list name
|
|
|
|
old_listname = ''
|
|
|
|
|
|
|
|
# New list name
|
|
|
|
new_listname = ''
|
|
|
|
|
|
|
|
# Find entities
|
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'old_list':
|
2019-05-19 09:47:42 +03:00
|
|
|
old_listname = item['sourceText'].lower()
|
2019-05-19 07:42:46 +03:00
|
|
|
elif item['entity'] == 'new_list':
|
2019-05-19 09:47:42 +03:00
|
|
|
new_listname = item['sourceText'].lower()
|
2019-05-19 07:42:46 +03:00
|
|
|
|
|
|
|
# Verify if an old and new list name have been provided
|
|
|
|
if not old_listname or not new_listname:
|
2019-05-19 11:16:34 +03:00
|
|
|
return utils.output('end', 'new_or_old_list_not_provided', utils.translate('new_or_old_list_not_provided'))
|
2019-05-19 07:42:46 +03:00
|
|
|
|
|
|
|
# Verify if the old list exists
|
|
|
|
if lists.count(List.name == old_listname) == 0:
|
2019-05-19 09:47:42 +03:00
|
|
|
return utils.output('end', 'list_does_not_exist', utils.translate('list_does_not_exist', { 'list': old_listname }))
|
2019-05-19 07:42:46 +03:00
|
|
|
|
|
|
|
# Verify if the new list name already exists
|
|
|
|
if lists.count(List.name == new_listname) > 0:
|
|
|
|
return utils.output('end', 'list_already_exists', utils.translate('list_already_exists', { 'list': new_listname }))
|
|
|
|
|
|
|
|
# Rename the to-do list
|
|
|
|
lists.update({
|
|
|
|
'name': new_listname,
|
|
|
|
'updated_at': int(time())
|
|
|
|
}, List.name == old_listname)
|
2019-05-04 04:26:11 +03:00
|
|
|
|
|
|
|
return utils.output('end', 'list_renamed', utils.translate('list_renamed', {
|
2019-05-19 07:42:46 +03:00
|
|
|
'old_list': old_listname,
|
|
|
|
'new_list': new_listname
|
2019-05-04 04:26:11 +03:00
|
|
|
}))
|
|
|
|
|
|
|
|
def delete_list(string, entities):
|
2019-05-19 09:47:42 +03:00
|
|
|
"""Delete a to-do list"""
|
|
|
|
|
|
|
|
# List name
|
|
|
|
listname = ''
|
|
|
|
|
|
|
|
# Find entities
|
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'list':
|
|
|
|
listname = item['sourceText'].lower()
|
|
|
|
|
|
|
|
# Verify if a list name has been provided
|
|
|
|
if not listname:
|
2019-05-19 11:16:34 +03:00
|
|
|
return utils.output('end', 'list_not_provided', utils.translate('list_not_provided'))
|
2019-05-19 09:47:42 +03:00
|
|
|
|
|
|
|
# Verify if the list exists
|
|
|
|
if lists.count(List.name == listname) == 0:
|
|
|
|
return utils.output('end', 'list_does_not_exist', utils.translate('list_does_not_exist', { 'list': listname }))
|
|
|
|
|
|
|
|
# Delete the to-do list
|
|
|
|
lists.remove(List.name == listname)
|
2019-05-04 04:26:11 +03:00
|
|
|
|
2019-05-19 09:47:42 +03:00
|
|
|
return utils.output('end', 'list_deleted', utils.translate('list_deleted', { 'list': listname }))
|
2019-05-03 18:31:09 +03:00
|
|
|
|
2019-05-19 09:30:53 +03:00
|
|
|
def add_todos(string, entities):
|
2019-05-19 11:16:34 +03:00
|
|
|
"""Add todos to a to-do list"""
|
2019-05-03 18:31:09 +03:00
|
|
|
|
2019-05-19 11:16:34 +03:00
|
|
|
# List name
|
|
|
|
listname = ''
|
|
|
|
|
|
|
|
# Todos
|
|
|
|
todos = []
|
|
|
|
|
|
|
|
# Find entities
|
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'list':
|
|
|
|
listname = item['sourceText'].lower()
|
|
|
|
elif item['entity'] == 'todos':
|
|
|
|
# Split todos into array and trim start/end-whitespaces
|
|
|
|
todos = [chunk.strip() for chunk in item['sourceText'].lower().split(',')]
|
|
|
|
|
|
|
|
# Verify if a list name has been provided
|
|
|
|
if not listname:
|
|
|
|
return utils.output('end', 'list_not_provided', utils.translate('list_not_provided'))
|
|
|
|
|
|
|
|
# Verify todos have been provided
|
|
|
|
if len(todos) == 0:
|
|
|
|
return utils.output('end', 'todos_not_provided', utils.translate('todos_not_provided'))
|
|
|
|
|
|
|
|
# Grab existing todos of the list
|
|
|
|
existing_todos = lists.get(List.name == listname)['todos']
|
|
|
|
|
2019-05-19 11:30:21 +03:00
|
|
|
result = ''
|
|
|
|
# Transform todo to a dict for DB and fill end-result
|
|
|
|
for i, todo in enumerate(todos):
|
|
|
|
todo = { 'name': todo, 'is_completed': False }
|
|
|
|
todos[i] = todo
|
|
|
|
result += utils.translate('list_todo_element', { 'todo': todo['name'] })
|
|
|
|
|
2019-05-19 15:40:24 +03:00
|
|
|
# Verify the list exists
|
|
|
|
if lists.count(List.name == listname) == 0:
|
|
|
|
# Create the to-do list
|
|
|
|
lists.insert({
|
|
|
|
'name': listname,
|
|
|
|
'todos': todos,
|
|
|
|
'created_at': timestamp,
|
|
|
|
'updated_at': timestamp
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
# Add todos to the to-do list
|
|
|
|
lists.update({
|
|
|
|
'todos': todos + existing_todos,
|
|
|
|
'updated_at': int(time())
|
|
|
|
}, List.name == listname)
|
2019-05-19 11:16:34 +03:00
|
|
|
|
|
|
|
return utils.output('end', 'todos_added', utils.translate('todos_added', {
|
|
|
|
'list': listname,
|
|
|
|
'result': result
|
2019-05-04 04:26:11 +03:00
|
|
|
}))
|
|
|
|
|
2019-05-19 15:40:24 +03:00
|
|
|
def complete_todos(string, entities):
|
|
|
|
"""Complete todos"""
|
2019-05-04 04:26:11 +03:00
|
|
|
|
2019-05-19 15:40:24 +03:00
|
|
|
# List name
|
|
|
|
listname = ''
|
|
|
|
|
|
|
|
# Todos
|
|
|
|
todos = []
|
|
|
|
|
|
|
|
# Find entities
|
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'list':
|
|
|
|
listname = item['sourceText'].lower()
|
|
|
|
elif item['entity'] == 'todos':
|
|
|
|
# Split todos into array and trim start/end-whitespaces
|
|
|
|
todos = [chunk.strip() for chunk in item['sourceText'].lower().split(',')]
|
|
|
|
|
|
|
|
# Verify if a list name has been provided
|
|
|
|
if not listname:
|
|
|
|
return utils.output('end', 'list_not_provided', utils.translate('list_not_provided'))
|
|
|
|
|
|
|
|
# Verify the list exists
|
|
|
|
if lists.count(List.name == listname) == 0:
|
|
|
|
return utils.output('end', 'list_does_not_exist', utils.translate('list_does_not_exist', { 'list': listname }))
|
|
|
|
|
|
|
|
# Verify todos have been provided
|
|
|
|
if len(todos) == 0:
|
|
|
|
return utils.output('end', 'todos_not_provided', utils.translate('todos_not_provided'))
|
|
|
|
|
|
|
|
result = ''
|
|
|
|
updated_todos = []
|
|
|
|
db_todos = lists.get(List.name == listname)['todos']
|
|
|
|
|
|
|
|
# # Verify same word in todo. Allow to not give the exact same todo (e.g. 1kg of rice = rice)
|
|
|
|
# if db_todo['name'].find(todo) != -1:
|
|
|
|
|
|
|
|
# result += utils.translate('list_todo_element', { 'todo': db_todo['name'] })
|
|
|
|
|
|
|
|
# Complete todos
|
|
|
|
# lists.update({
|
|
|
|
# 'todos': updated_todos,
|
|
|
|
# 'updated_at': int(time())
|
|
|
|
# }, List.name == listname)
|
|
|
|
|
|
|
|
# return utils.output('end', 'todos_completed', utils.translate('todos_completed', {
|
|
|
|
# 'list': listname,
|
|
|
|
# 'result': result
|
|
|
|
# }))
|
|
|
|
|
|
|
|
# Complete potatoes from my list
|
|
|
|
# TODO: look in all lists first, if several then ask to specify from which list
|
|
|
|
# Complete potatoes from the shopping list
|
2019-05-03 18:31:09 +03:00
|
|
|
|
2019-05-19 11:16:34 +03:00
|
|
|
def uncomplete_todos(string, entities):
|
|
|
|
"""WIP"""
|
|
|
|
|
|
|
|
# Complete potatoes from my list
|
|
|
|
# TODO: look in all lists first, if several then ask to specify from which list
|
|
|
|
# Complete potatoes from the shopping list
|
|
|
|
|
|
|
|
return utils.output('end', 'todo_uncompleted', utils.translate('todo_uncompleted', {
|
|
|
|
'list': 'fake',
|
|
|
|
'todo': 'todo 1'
|
|
|
|
}))
|
|
|
|
|
2019-05-19 15:40:24 +03:00
|
|
|
def view_todos(string, entities):
|
2019-05-03 18:31:09 +03:00
|
|
|
"""WIP"""
|
|
|
|
|
2019-05-19 15:40:24 +03:00
|
|
|
# TODO
|
2019-05-19 07:42:46 +03:00
|
|
|
|