diff --git a/database.sql b/database.sql index 0ac7b76..d311a94 100644 --- a/database.sql +++ b/database.sql @@ -12,7 +12,7 @@ CREATE TABLE Directory ( CREATE TABLE Task ( id INTEGER PRIMARY KEY AUTOINCREMENT, directory_id INTEGER, - task_type INTEGER, + type INTEGER, completed BOOLEAN DEFAULT 0, completed_time DATETIME, FOREIGN KEY (directory_id) REFERENCES Directory(id) diff --git a/run.py b/run.py index d2fa965..16aaa4f 100644 --- a/run.py +++ b/run.py @@ -192,7 +192,11 @@ def directory_del(dir_id): @app.route("/task") def task(): - return + tasks = storage.tasks() + directories = storage.dirs() + + return render_template("task.html", tasks=tasks, directories=directories) + @app.route("/dashboard") def dashboard(): diff --git a/spec/LocalStorage_spec.py b/spec/LocalStorage_spec.py index b5b685c..ceafe34 100644 --- a/spec/LocalStorage_spec.py +++ b/spec/LocalStorage_spec.py @@ -1,5 +1,5 @@ from unittest import TestCase -from storage import LocalStorage, Directory, DuplicateDirectoryException, User, DuplicateUserException, Option +from storage import LocalStorage, Directory, DuplicateDirectoryException, User, DuplicateUserException, Option, Task import os @@ -178,5 +178,25 @@ class LocalStorageTest(TestCase): self.assertEqual(s.dirs()[dir_id].options[0].value, "val2") self.assertEqual(s.dirs()[dir_id].options[0].dir_id, 1) + def test_save_task(self): + + s = LocalStorage("test_database.db") + + dir_id = s.save_directory(Directory("/some/dir", True, [], "my dir")) + task_id = s.save_task(Task(0, dir_id)) + + self.assertEqual(s.tasks()[task_id].dir_id, dir_id) + self.assertEqual(task_id, 1) + + def test_del_task(self): + s = LocalStorage("test_database.db") + + dir_id = s.save_directory(Directory("/some/dir", True, [], "my dir")) + task_id = s.save_task(Task(0, dir_id)) + + s.del_task(task_id) + + with self.assertRaises(KeyError): + _ = s.tasks()[task_id] diff --git a/storage.py b/storage.py index b045ec7..22103d8 100644 --- a/storage.py +++ b/storage.py @@ -1,7 +1,7 @@ import sqlite3 import os import flask_bcrypt - +import time class CheckSumCalculator: @@ -56,6 +56,16 @@ class Directory: return self.path + " | enabled: " + str(self.enabled) + " | opts: " + str(self.options) +class Task: + + def __init__(self, task_type: int, dir_id: int, completed: bool = False, completed_time: time.time = None, task_id: int = None): + self.id = task_id + self.type = task_type + self.dir_id = dir_id + self.completed = completed + self.completed_time = completed_time + + class LocalStorage: """ Manages storage of application data to disk. @@ -65,10 +75,11 @@ class LocalStorage: def __init__(self, db_path): self.cached_dirs = {} self.cached_users = {} + self.cached_tasks = {} self.db_path = db_path self.dir_cache_outdated = True # Indicates that the database was changed since it was cached in memory self.user_cache_outdated = True - pass + self.task_cache_outdated = True def init_db(self, script_path): """Creates a blank database. Overwrites the old one""" @@ -282,5 +293,59 @@ class LocalStorage: conn.commit() conn.close() + def save_task(self, task: Task): + """Save a task to the database""" + self.task_cache_outdated = True + conn = sqlite3.connect(self.db_path) + c = conn.cursor() + c.execute("INSERT INTO Task (directory_id, type, completed, completed_time) VALUES (?,?,?,?)", + (task.dir_id, task.type, task.completed, task.completed_time)) + c.execute("SELECT last_insert_rowid()") + + task_id = c.fetchone()[0] + + conn.commit() + c.close() + conn.close() + + return task_id + + def tasks(self): + """Get the (cached) list of taks""" + + if self.task_cache_outdated: + + conn = sqlite3.connect(self.db_path) + c = conn.cursor() + c.execute("SELECT id, directory_id, type, completed, completed_time FROM Task") + + tasks = c.fetchall() + + c.close() + conn.close() + + for db_task in tasks: + + task = Task(db_task[2], db_task[1], db_task[3], db_task[4], db_task[0]) + self.cached_tasks[task.id] = task + + self.task_cache_outdated = False + return self.cached_tasks + + else: + return self.cached_tasks + + def del_task(self, task_id): + """Delete a task from the database""" + + self.task_cache_outdated = True + + conn = sqlite3.connect(self.db_path) + c = conn.cursor() + c.execute("DELETE FROM Task WHERE id=?", (task_id, )) + + c.close() + conn.commit() + conn.close() diff --git a/templates/directory.html b/templates/directory.html index 8f7ae2f..964986e 100644 --- a/templates/directory.html +++ b/templates/directory.html @@ -16,7 +16,7 @@
- + @@ -26,7 +26,8 @@
An excellent list
- +
+ @@ -34,16 +35,19 @@ - + + {% for dir in directories %} - + {% endfor %} + +
Display Name PathLast indexed Action
{{ directories[dir].name }}
{{ directories[dir].path }}
2018-02-21Manage Manage
diff --git a/templates/directory_manage.html b/templates/directory_manage.html index 84560d1..bb18135 100644 --- a/templates/directory_manage.html +++ b/templates/directory_manage.html @@ -34,21 +34,25 @@
An excellent option list
- +
+ + + {% for option in directory.options %} - + {% endfor %} +
Key Value Action
{{ option.key }} {{ option.value }}RemoveRemove

@@ -67,7 +71,7 @@
- +
diff --git a/templates/layout.html b/templates/layout.html index 06df9af..bb956d0 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -35,9 +35,9 @@ padding: 4px; } - .info-table tr:nth-child(even) { - background-color: #fafafa; - } +{# .info-table tr:nth-child(even) {#} +{# background-color: #fafafa;#} +{# }#} {# todo: box-shadow 0 1px 10px 1px #1AC8DE#} diff --git a/templates/task.html b/templates/task.html index 566549b..39a48b4 100644 --- a/templates/task.html +++ b/templates/task.html @@ -1,10 +1,60 @@ - - - - - Title - - +{% extends "layout.html" %} - - \ No newline at end of file +{% block title %}An excellent title{% endblock title %} + +{% block body %} + +
+ +
+
An excellent form
+
+
+ + + + + + + +
+
+
+ +
+
An excellent panel
+
+ + + + + + + + + {% for task in tasks %} + + + + + + + + {% endfor %} +
Task typeDirectoryCompleted
{{ task.type }}{{ directories[task.dir_id].name }}{{ task.completed }}
+ + + +
+
+ +
+ +{% endblock body %} \ No newline at end of file