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
|
2019-06-06 13:05:52 +03:00
|
|
|
db_lists = db.table('todo_lists')
|
|
|
|
# Todos of the module table
|
|
|
|
db_todos = db.table('todo_todos')
|
2019-05-19 06:53:26 +03:00
|
|
|
|
|
|
|
# Query
|
2019-06-06 13:05:52 +03:00
|
|
|
Query = utils.db()['query']()
|
|
|
|
|
|
|
|
# Time stamp
|
|
|
|
timestamp = int(time())
|
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
|
2019-06-06 13:05:52 +03:00
|
|
|
if db_lists.count(Query.name == listname) > 0:
|
2019-05-19 06:53:26 +03:00
|
|
|
return utils.output('end', 'list_already_exists', utils.translate('list_already_exists', { 'list': listname }))
|
|
|
|
|
2019-05-21 16:54:19 +03:00
|
|
|
dbCreateList(listname)
|
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
|
2019-06-06 13:05:52 +03:00
|
|
|
lists_nb = len(db_lists)
|
2019-05-19 09:30:53 +03:00
|
|
|
|
|
|
|
# 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-06-06 13:05:52 +03:00
|
|
|
for listelement in db_lists:
|
2019-05-19 09:30:53 +03:00
|
|
|
result += utils.translate('list_list_element', {
|
|
|
|
'list': listelement['name'],
|
2019-06-06 13:05:52 +03:00
|
|
|
'todos_nb': db_todos.count(Query.list == listelement['name'])
|
2019-05-19 09:30:53 +03:00
|
|
|
})
|
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
return utils.output('end', 'lists_listed', utils.translate('lists_listed', {
|
2019-05-19 09:30:53 +03:00
|
|
|
'lists_nb': lists_nb,
|
|
|
|
'result': result
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2019-05-19 07:42:46 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
def view_list(string, entities):
|
|
|
|
"""View a to-do list"""
|
|
|
|
|
|
|
|
# List name
|
|
|
|
listname = ''
|
|
|
|
|
|
|
|
# Find entities
|
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'list':
|
|
|
|
listname = item['sourceText'].lower()
|
|
|
|
|
|
|
|
# Verify if the list exists
|
|
|
|
if db_lists.count(Query.name == listname) == 0:
|
|
|
|
return utils.output('end', 'list_does_not_exist', utils.translate('list_does_not_exist', { 'list': listname }))
|
|
|
|
|
|
|
|
# Grab todos of the list
|
|
|
|
todos = db_todos.search(Query.list == listname)
|
|
|
|
|
|
|
|
if len(todos) == 0:
|
|
|
|
return utils.output('end', 'empty_list', utils.translate('empty_list', { 'list': listname }))
|
|
|
|
|
2019-06-06 13:49:20 +03:00
|
|
|
unchecked_todos = db_todos.search((Query.list == listname) & (Query.is_completed == False))
|
2019-06-06 13:05:52 +03:00
|
|
|
completed_todos = db_todos.search((Query.list == listname) & (Query.is_completed == True))
|
|
|
|
|
2019-06-06 13:49:20 +03:00
|
|
|
result_unchecked_todos = ''
|
2019-06-06 13:05:52 +03:00
|
|
|
result_completed_todos = ''
|
|
|
|
|
2019-06-06 13:49:20 +03:00
|
|
|
if len(unchecked_todos) == 0:
|
|
|
|
utils.output('inter', 'no_unchecked_todo', utils.translate('no_unchecked_todo', { 'list': listname }))
|
2019-06-06 13:05:52 +03:00
|
|
|
else:
|
2019-06-06 13:49:20 +03:00
|
|
|
for todo in unchecked_todos:
|
|
|
|
result_unchecked_todos += utils.translate('list_todo_element', {
|
2019-06-06 13:05:52 +03:00
|
|
|
'todo': todo['name']
|
|
|
|
})
|
|
|
|
|
2019-06-06 13:49:20 +03:00
|
|
|
utils.output('inter', 'unchecked_todos_listed', utils.translate('unchecked_todos_listed', {
|
2019-06-06 13:05:52 +03:00
|
|
|
'list': listname,
|
2019-06-06 13:49:20 +03:00
|
|
|
'result': result_unchecked_todos
|
2019-06-06 13:05:52 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(completed_todos) == 0:
|
|
|
|
return utils.output('end', 'no_completed_todo', utils.translate('no_completed_todo', { 'list': listname }))
|
|
|
|
|
|
|
|
for todo in completed_todos:
|
|
|
|
result_completed_todos += utils.translate('list_completed_todo_element', {
|
|
|
|
'todo': todo['name']
|
|
|
|
})
|
|
|
|
|
|
|
|
return utils.output('end', 'completed_todos_listed', utils.translate('completed_todos_listed', {
|
|
|
|
'list': listname,
|
|
|
|
'result': result_completed_todos
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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
|
2019-06-06 13:05:52 +03:00
|
|
|
if db_lists.count(Query.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
|
2019-06-06 13:05:52 +03:00
|
|
|
if db_lists.count(Query.name == new_listname) > 0:
|
2019-05-19 07:42:46 +03:00
|
|
|
return utils.output('end', 'list_already_exists', utils.translate('list_already_exists', { 'list': new_listname }))
|
|
|
|
|
|
|
|
# Rename the to-do list
|
2019-06-06 13:05:52 +03:00
|
|
|
db_lists.update({
|
2019-05-19 07:42:46 +03:00
|
|
|
'name': new_listname,
|
|
|
|
'updated_at': int(time())
|
2019-06-06 13:05:52 +03:00
|
|
|
}, Query.name == old_listname)
|
|
|
|
# Rename the list name of the todos
|
|
|
|
db_todos.update({
|
|
|
|
'list': new_listname,
|
|
|
|
'updated_at': int(time())
|
|
|
|
}, Query.list == 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
|
2019-06-06 13:05:52 +03:00
|
|
|
if db_lists.count(Query.name == 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': listname }))
|
|
|
|
|
|
|
|
# Delete the to-do list
|
2019-06-06 13:05:52 +03:00
|
|
|
db_lists.remove(Query.name == listname)
|
|
|
|
# Delete todos of that to-do list
|
|
|
|
db_todos.remove(Query.list == 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'))
|
|
|
|
|
2019-05-21 16:54:19 +03:00
|
|
|
# Verify the list exists
|
2019-06-06 13:05:52 +03:00
|
|
|
if db_lists.count(Query.name == listname) == 0:
|
2019-05-21 16:54:19 +03:00
|
|
|
# Create the new to-do list
|
|
|
|
dbCreateList(listname)
|
2019-05-19 11:16:34 +03:00
|
|
|
|
2019-05-19 11:30:21 +03:00
|
|
|
result = ''
|
2019-06-06 13:05:52 +03:00
|
|
|
for todo in todos:
|
|
|
|
# Add to-do to DB
|
|
|
|
dbCreateTodo(listname, todo)
|
|
|
|
result += utils.translate('list_todo_element', { 'todo': todo })
|
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 todos have been provided
|
|
|
|
if len(todos) == 0:
|
|
|
|
return utils.output('end', 'todos_not_provided', utils.translate('todos_not_provided'))
|
|
|
|
|
2019-05-21 16:54:19 +03:00
|
|
|
# Verify the list exists
|
2019-06-06 13:05:52 +03:00
|
|
|
if db_lists.count(Query.name == listname) == 0:
|
2019-05-21 16:54:19 +03:00
|
|
|
# Create the new to-do list
|
|
|
|
dbCreateList(listname)
|
|
|
|
|
2019-05-19 15:40:24 +03:00
|
|
|
result = ''
|
2019-06-06 13:05:52 +03:00
|
|
|
for todo in todos:
|
|
|
|
for db_todo in db_todos.search(Query.list == listname):
|
|
|
|
# Rough matching (e.g. 1kg of rice = rice)
|
|
|
|
if db_todo['name'].find(todo) != -1:
|
|
|
|
db_todos.update({
|
|
|
|
'is_completed': True,
|
|
|
|
'updated_at': timestamp
|
|
|
|
}, (Query.list == listname) & (Query.name == db_todo['name']))
|
2019-05-19 15:40:24 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
result += utils.translate('list_todo_element', { 'todo': db_todo['name'] })
|
2019-05-21 16:54:19 +03:00
|
|
|
|
|
|
|
return utils.output('end', 'todos_completed', utils.translate('todos_completed', {
|
|
|
|
'list': listname,
|
|
|
|
'result': result
|
|
|
|
}))
|
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
def uncomplete_todos(string, entities):
|
|
|
|
"""Complete todos"""
|
2019-05-19 15:40:24 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
# List name
|
|
|
|
listname = ''
|
2019-05-19 15:40:24 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
# Todos
|
|
|
|
todos = []
|
2019-05-19 15:40:24 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
# 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(',')]
|
2019-05-19 15:40:24 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
# Verify if a list name has been provided
|
|
|
|
if not listname:
|
|
|
|
return utils.output('end', 'list_not_provided', utils.translate('list_not_provided'))
|
2019-05-03 18:31:09 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
# Verify todos have been provided
|
|
|
|
if len(todos) == 0:
|
|
|
|
return utils.output('end', 'todos_not_provided', utils.translate('todos_not_provided'))
|
2019-05-19 11:16:34 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
# Verify if the list exists
|
|
|
|
if db_lists.count(Query.name == listname) == 0:
|
|
|
|
return utils.output('end', 'list_does_not_exist', utils.translate('list_does_not_exist', { 'list': listname }))
|
2019-05-19 11:16:34 +03:00
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
result = ''
|
|
|
|
for todo in todos:
|
|
|
|
for db_todo in db_todos.search(Query.list == listname):
|
|
|
|
# Rough matching (e.g. 1kg of rice = rice)
|
|
|
|
if db_todo['name'].find(todo) != -1:
|
|
|
|
db_todos.update({
|
|
|
|
'is_completed': False,
|
|
|
|
'updated_at': timestamp
|
|
|
|
}, (Query.list == listname) & (Query.name == db_todo['name']))
|
|
|
|
|
|
|
|
result += utils.translate('list_todo_element', { 'todo': db_todo['name'] })
|
|
|
|
|
2019-06-06 13:49:20 +03:00
|
|
|
return utils.output('end', 'todo_unchecked', utils.translate('todos_unchecked', {
|
2019-06-06 13:05:52 +03:00
|
|
|
'list': listname,
|
|
|
|
'result': result
|
2019-05-19 11:16:34 +03:00
|
|
|
}))
|
|
|
|
|
2019-05-21 16:54:19 +03:00
|
|
|
def dbCreateList(listname):
|
|
|
|
"""Create list in DB"""
|
|
|
|
|
2019-06-06 13:05:52 +03:00
|
|
|
db_lists.insert({
|
2019-05-21 16:54:19 +03:00
|
|
|
'name': listname,
|
2019-06-06 13:05:52 +03:00
|
|
|
'created_at': timestamp,
|
|
|
|
'updated_at': timestamp
|
|
|
|
})
|
|
|
|
|
|
|
|
def dbCreateTodo(listname, todoname):
|
|
|
|
"""Create to-todo in list DB table"""
|
|
|
|
|
|
|
|
db_todos.insert({
|
|
|
|
'list': listname,
|
|
|
|
'name': todoname,
|
|
|
|
'is_completed': False,
|
2019-05-21 16:54:19 +03:00
|
|
|
'created_at': timestamp,
|
|
|
|
'updated_at': timestamp
|
|
|
|
})
|