add task deleting

This commit is contained in:
fram3d 2024-01-18 14:04:00 +01:00
parent a1c41349bd
commit d0d9529e41
Signed by: fram3d
GPG Key ID: 938920E709EEA32A
6 changed files with 102 additions and 6 deletions

2
taskmanager/config.ini Normal file
View File

@ -0,0 +1,2 @@
[credentials]
ADMINPASS = defaultpassword

View File

@ -4,6 +4,7 @@ class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
desc = db.Column(db.String, nullable=True)
creatorid = db.Column(db.Integer, nullable=True)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)

View File

@ -2,6 +2,15 @@ from flask import render_template, request, redirect
from taskmanager import app, db
from taskmanager.functions import *
from taskmanager.models import *
import configparser
#CONFIG_PATH = "/var/taskmanager/taskmanager/config.ini"
CONFIG_PATH = "/home/anon/src/taskmanager/taskmanager/config.ini"
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
ADMINPASS = config.get('credentials', 'ADMINPASS')
@app.route('/', methods=['GET'])
def index():
@ -15,7 +24,17 @@ def addtask():
elif request.method == 'POST':
taskname = request.form['taskname']
taskdesc = request.form['taskdesc']
sqladdtask = Task(name = taskname, desc = taskdesc)
username = request.form['username']
if username == "":
creatorid = None
else:
try:
creatorid = User.query.filter_by(username = username).first().id
except:
return 'No user with this username. Please <a href="/register">register</a>.'
if creatorid is None:
return 'No user with this username. Please <a href="/register">register</a>.'
sqladdtask = Task(name = taskname, desc = taskdesc, creatorid = creatorid)
try:
db.session.add(sqladdtask)
db.session.commit()
@ -45,12 +64,14 @@ def register():
@app.route('/projects/<int:task_id>', methods=['GET','POST'])
def project(task_id:int):
try:
task = Task.query.get(task_id)
except:
return 'Task not found, bad URL'
if task is None:
return 'Task not found, bad URL'
users = gettaskusers(task_id)
if request.method == 'GET':
try:
task = Task.query.get(task_id)
except:
return 'Task not found, bad URL'
return render_template("project.html", task = task, users = users)
elif request.method == 'POST':
username = request.form['username']
@ -60,6 +81,8 @@ def project(task_id:int):
userid = User.query.filter_by(username = username).first().id
except:
return 'User not found, please <a href="/register">register</a>.'
if userid is None:
return 'User not found, please <a href="/register">register</a>.'
sqladduser = TaskUser(userid = userid, taskid = task_id)
try:
db.session.add(sqladduser)
@ -68,3 +91,34 @@ def project(task_id:int):
except:
return 'Adding user failed'
@app.route('/projects/<int:task_id>/del', methods=['GET','POST'])
def deltask(task_id:int):
try:
task = Task.query.get(task_id)
except:
return 'Task not found, bad URL'
if task is None:
return 'Task not found, bad URL'
creatorid = task.creatorid
if request.method == 'GET':
if creatorid is None:
try:
db.session.delete(task)
db.session.commit()
return 'Task deleted'
except:
return 'Deleting task failed'
else:
return render_template('deltask.html', task = task)
if request.method == 'POST':
password = request.form['password']
# Check password
if password != ADMINPASS and password != User.query.get(creatorid).password:
return 'Wrong password'
# Delete task
try:
db.session.delete(task)
db.session.commit()
return 'Task deleted'
except:
return 'Deleting task failed'

View File

@ -25,7 +25,11 @@
</div>
<div class="form-input">
<label for="taskdesc" class="label">Description:</label>
<input type="text" name="taskdesc" id="taskdesc" required />
<input type="text" name="taskdesc" id="taskdesc" placeholder="optional"/>
</div>
<div class="form-input">
<label for="username" class="label">Username:</label>
<input type="text" name="username" id="username" placeholder="optional"/>
</div>
<div class="btn-wrap">
<button class="btn">Submit</button>

View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/static/style.css" />
<title>Delete task</title>
</head>
<body>
<header>
<nav class="container">
<ul>
<li class="current"><a href="/">Home</a></li>
<li><a href="/register">Register</a></li>
</ul>
</nav>
</header>
<main class="container page page-addtask">
<h1>Create new task</h1>
<div class="form-wrap">
<form action="/projects/{{task.id}}/del" method="POST">
<p> Task creator's password <p>
<div class="form-input">
<label for="password" class="label">password:</label>
<input type="password" name="password" id="password" required />
</div>
<div class="btn-wrap">
<button class="btn">DELETE</button>
</div>
</div>
</form>
</main>
</body>
</html>

View File

@ -43,6 +43,7 @@
<button class="btn">Submit</button>
</div>
</form>
<p><a href="/projects/{{task.id}}/del">DELETE TASK</a><p>
</div>
</div>
</section>