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 (
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)

6
run.py
View File

@ -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():

View File

@ -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]

View File

@ -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()

View File

@ -16,7 +16,7 @@
<div class="form-group">
<input type="text" class="form-control" placeholder="Absolute path" name="path">
</div>
<button type="submit" class="btn btn-primary">Add Directory</button>
<button type="submit" class="btn btn-success">Add Directory</button>
</form>
</div>
@ -26,7 +26,8 @@
<div class="panel-heading">An excellent list</div>
<div class="panel-body">
<table class="info-table">
<table class="info-table table-hover table-striped">
<thead>
<tr>
<th>Display Name</th>
<th>Path</th>
@ -34,16 +35,19 @@
<th>Last indexed</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for dir in directories %}
<tr>
<td>{{ directories[dir].name }}</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>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>
{% endfor %}
</tbody>
</table>
</div>

View File

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

View File

@ -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#}
</style>

View File

@ -1,10 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% extends "layout.html" %}
</body>
</html>
{% block title %}An excellent title{% endblock title %}
{% 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 %}