Some work on the tasks CRUD

This commit is contained in:
simon 2018-02-21 21:52:41 -05:00
parent 165844e4ca
commit 1602f47b4e
8 changed files with 171 additions and 24 deletions

View File

@ -12,7 +12,7 @@ CREATE TABLE Directory (
CREATE TABLE Task ( CREATE TABLE Task (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
directory_id INTEGER, directory_id INTEGER,
task_type INTEGER, type INTEGER,
completed BOOLEAN DEFAULT 0, completed BOOLEAN DEFAULT 0,
completed_time DATETIME, completed_time DATETIME,
FOREIGN KEY (directory_id) REFERENCES Directory(id) FOREIGN KEY (directory_id) REFERENCES Directory(id)

6
run.py
View File

@ -192,7 +192,11 @@ def directory_del(dir_id):
@app.route("/task") @app.route("/task")
def task(): def task():
return tasks = storage.tasks()
directories = storage.dirs()
return render_template("task.html", tasks=tasks, directories=directories)
@app.route("/dashboard") @app.route("/dashboard")
def dashboard(): def dashboard():

View File

@ -1,5 +1,5 @@
from unittest import TestCase 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 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].value, "val2")
self.assertEqual(s.dirs()[dir_id].options[0].dir_id, 1) 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]

View File

@ -1,7 +1,7 @@
import sqlite3 import sqlite3
import os import os
import flask_bcrypt import flask_bcrypt
import time
class CheckSumCalculator: class CheckSumCalculator:
@ -56,6 +56,16 @@ class Directory:
return self.path + " | enabled: " + str(self.enabled) + " | opts: " + str(self.options) 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: class LocalStorage:
""" """
Manages storage of application data to disk. Manages storage of application data to disk.
@ -65,10 +75,11 @@ class LocalStorage:
def __init__(self, db_path): def __init__(self, db_path):
self.cached_dirs = {} self.cached_dirs = {}
self.cached_users = {} self.cached_users = {}
self.cached_tasks = {}
self.db_path = db_path self.db_path = db_path
self.dir_cache_outdated = True # Indicates that the database was changed since it was cached in memory self.dir_cache_outdated = True # Indicates that the database was changed since it was cached in memory
self.user_cache_outdated = True self.user_cache_outdated = True
pass self.task_cache_outdated = True
def init_db(self, script_path): def init_db(self, script_path):
"""Creates a blank database. Overwrites the old one""" """Creates a blank database. Overwrites the old one"""
@ -282,5 +293,59 @@ class LocalStorage:
conn.commit() conn.commit()
conn.close() 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()

View File

@ -16,7 +16,7 @@
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" placeholder="Absolute path" name="path"> <input type="text" class="form-control" placeholder="Absolute path" name="path">
</div> </div>
<button type="submit" class="btn btn-primary">Add Directory</button> <button type="submit" class="btn btn-success">Add Directory</button>
</form> </form>
</div> </div>
@ -26,7 +26,8 @@
<div class="panel-heading">An excellent list</div> <div class="panel-heading">An excellent list</div>
<div class="panel-body"> <div class="panel-body">
<table class="info-table"> <table class="info-table table-hover table-striped">
<thead>
<tr> <tr>
<th>Display Name</th> <th>Display Name</th>
<th>Path</th> <th>Path</th>
@ -34,16 +35,19 @@
<th>Last indexed</th> <th>Last indexed</th>
<th>Action</th> <th>Action</th>
</tr> </tr>
</thead>
<tbody>
{% for dir in directories %} {% for dir in directories %}
<tr> <tr>
<td>{{ directories[dir].name }}</td> <td>{{ directories[dir].name }}</td>
<td><pre style="width: 80%">{{ directories[dir].path }}</pre></td> <td><pre style="width: 80%">{{ directories[dir].path }}</pre></td>
<td><i class="far {{ "fa-check-square" if directories[dir].enabled else "fa-square" }}"></i></td> <td><i class="far {{ "fa-check-square" if directories[dir].enabled else "fa-square" }}"></i></td>
<td>2018-02-21</td> <td>2018-02-21</td>
<td><a href="directory/{{ dir }}">Manage</a> </td> <td><a href="directory/{{ dir }}" class="btn btn-primary">Manage</a> </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table> </table>
</div> </div>

View File

@ -34,21 +34,25 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">An excellent option list</div> <div class="panel-heading">An excellent option list</div>
<div class="panel-body"> <div class="panel-body">
<table class="info-table"> <table class="info-table table-striped table-hover">
<thead>
<tr> <tr>
<th>Key</th> <th>Key</th>
<th>Value</th> <th>Value</th>
<th>Action</th> <th>Action</th>
</tr> </tr>
</thead>
<tbody>
{% for option in directory.options %} {% for option in directory.options %}
<tr> <tr>
<td>{{ option.key }}</td> <td>{{ option.key }}</td>
<td>{{ option.value }}</td> <td>{{ option.value }}</td>
<td><a href="/directory/{{ directory.id }}/del_opt/{{ option.id }}" >Remove</a></td> <td><a class="btn btn-danger" href="/directory/{{ directory.id }}/del_opt/{{ option.id }}" >Remove</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table> </table>
<hr> <hr>
@ -67,7 +71,7 @@
</div> </div>
<button type="submit" class="btn btn-primary">Add option</button> <button type="submit" class="btn btn-success">Add option</button>
</form> </form>
</div> </div>

View File

@ -35,9 +35,9 @@
padding: 4px; padding: 4px;
} }
.info-table tr:nth-child(even) { {# .info-table tr:nth-child(even) {#}
background-color: #fafafa; {# background-color: #fafafa;#}
} {# }#}
{# todo: box-shadow 0 1px 10px 1px #1AC8DE#} {# todo: box-shadow 0 1px 10px 1px #1AC8DE#}
</style> </style>

View File

@ -1,10 +1,60 @@
<!DOCTYPE html> {% extends "layout.html" %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body> {% block title %}An excellent title{% endblock title %}
</html>
{% block body %}
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">An excellent form</div>
<div class="panel-body">
<form class="form-inline" action="/task/add">
<label for="type">Create </label>
<select class="form-control" id="type" name="type" >
<option value="1">Indexing</option>
<option value="2">Thumbnail Generation</option>
</select>
<label for="directory">task for</label>
<select class="form-control" id="directory" name="directory" >
{% for dir_id in directories %}
<option value="{{ dir_id }}">{{ directories[dir_id].name }}</option>
{% endfor %}
</select>
<input type="submit" class="form-control btn btn-success" value="Add">
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">An excellent panel</div>
<div class="panel-body">
<table class="info-table">
<tr>
<th>Task type</th>
<th>Directory</th>
<th>Completed</th>
</tr>
{% for task in tasks %}
<tr>
<td>{{ task.type }}</td>
<td>{{ directories[task.dir_id].name }}</td>
<td>{{ task.completed }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock body %}