Izmenjen templates folder tako da bude modularniji #4

Merged
fram3d merged 2 commits from svitvojimilioni/taskmanager:better-templates into master 2024-01-19 16:17:38 +00:00
11 changed files with 228 additions and 40 deletions

View File

@ -8,18 +8,17 @@ CONFIG_PATH = "/var/taskmanager/taskmanager/config.ini"
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_PATH) config.read(CONFIG_PATH)
ADMINPASS = config.get('credentials', 'ADMINPASS') ADMINPASS = config.get('credentials', 'ADMINPASS')
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
tasks = Task.query.all() tasks = Task.query.all()
return render_template('index.html', tasks = tasks) return render_template('pages/index.html', tasks = tasks)
@app.route('/addtask', methods=['GET','POST']) @app.route('/addtask', methods=['GET','POST'])
def addtask(): def addtask():
if request.method == 'GET': if request.method == 'GET':
return render_template('addtask.html') return render_template('pages/addtask.html')
elif request.method == 'POST': elif request.method == 'POST':
taskname = request.form['taskname'] taskname = request.form['taskname']
taskdesc = request.form['taskdesc'] taskdesc = request.form['taskdesc']
@ -27,9 +26,9 @@ def addtask():
# Input sanitation # Input sanitation
# Task name # Task name
if not taskname.isprintable(): if not taskname.isprintable():
return render_template('response.html', response = "Task name has to be made only of letters or numbers.") return render_template('pages/response.html', response = "Task name has to be made only of letters or numbers.")
if len(taskname) < 1 or len(taskname) > 40: if len(taskname) < 1 or len(taskname) > 40:
return render_template('response.html', response = "Task name lenght invalid, only smaller then 40 charachters allowed") return render_template('pages/response.html', response = "Task name lenght invalid, only smaller then 40 charachters allowed")
# Username # Username
if username == "": if username == "":
@ -38,28 +37,28 @@ def addtask():
try: try:
creatorid = User.query.filter_by(username = username).first().id creatorid = User.query.filter_by(username = username).first().id
except: except:
return render_template('response.html', response = 'No user with this username. Please register') return render_template('pages/response.html', response = 'No user with this username. Please register')
if creatorid is None: if creatorid is None:
return render_template('response.html', response = 'No user with this username. Please register.') return render_template('pages/response.html', response = 'No user with this username. Please register.')
# Task descripton # Task descripton
if taskdesc != '': if taskdesc != '':
if not taskdesc.isprintable(): if not taskdesc.isprintable():
return render_template('response.html', response = "Task description has to be made of printable characters.") return render_template('pages/response.html', response = "Task description has to be made of printable characters.")
if len(taskdesc) > 2000: if len(taskdesc) > 2000:
return render_template('response.html', response = "Task description lenght invalid, only smaller then 2000 charachters allowed") return render_template('pages/response.html', response = "Task description lenght invalid, only smaller then 2000 charachters allowed")
sqladdtask = Task(name = taskname, desc = taskdesc, creatorid = creatorid) sqladdtask = Task(name = taskname, desc = taskdesc, creatorid = creatorid)
try: try:
db.session.add(sqladdtask) db.session.add(sqladdtask)
db.session.commit() db.session.commit()
return render_template('response.html', response = 'Task added') return render_template('pages/response.html', response = 'Task added')
except: except:
return render_template('response.html', response = 'Adding task failed') return render_template('pages/response.html', response = 'Adding task failed')
@app.route('/register', methods=['POST', 'GET']) @app.route('/register', methods=['POST', 'GET'])
def register(): def register():
if request.method == 'GET': if request.method == 'GET':
return render_template('register.html') return render_template('pages/register.html')
elif request.method == 'POST': elif request.method == 'POST':
username = request.form['username'] username = request.form['username']
contact = request.form['contact'] contact = request.form['contact']
@ -67,34 +66,34 @@ def register():
# Username # Username
if not username.isalnum(): if not username.isalnum():
return render_template('response.html', response = "Username has to be made only of letters or numbers.") return render_template('pages/response.html', response = "Username has to be made only of letters or numbers.")
if len(username) < 1 or len(username) > 40: if len(username) < 1 or len(username) > 40:
return render_template('response.html', response = "Username lenght invalid, only smaller then 40 charachters allowed") return render_template('pages/response.html', response = "Username lenght invalid, only smaller then 40 charachters allowed")
# Contact # Contact
if contact != '': if contact != '':
if not contact.isprintable(): if not contact.isprintable():
return render_template('response.html', response = "Contact information has to be made of printable characters.") return render_template('pages/response.html', response = "Contact information has to be made of printable characters.")
if len(contact) > 100: if len(contact) > 100:
return render_template('response.html', response = "Contact lenght invalid, only smaller then 100 charachters allowed") return render_template('pages/response.html', response = "Contact lenght invalid, only smaller then 100 charachters allowed")
# Password # Password
if password != '': if password != '':
if not password.isprintable(): if not password.isprintable():
return render_template('response.html', response = "Password has to be made of printable characters.") return render_template('pages/response.html', response = "Password has to be made of printable characters.")
if len(password) > 500: if len(password) > 500:
return render_template('response.html', response = "Password lenght invalid, only smaller then 500 charachters allowed") return render_template('pages/response.html', response = "Password lenght invalid, only smaller then 500 charachters allowed")
sqladduser = User(username = username, contact = contact, password = password) sqladduser = User(username = username, contact = contact, password = password)
try: try:
db.session.add(sqladduser) db.session.add(sqladduser)
db.session.commit() db.session.commit()
return render_template('response.html', response = 'User added') return render_template('pages/response.html', response = 'User added')
except: except:
return render_template('response.html', response = 'Adding user failed') return render_template('pages/response.html', response = 'Adding user failed')
else: else:
return render_template('response.html', response = 'HTTP request method not recogniezed') return render_template('pages/response.html', response = 'HTTP request method not recogniezed')
@app.route('/projects/<int:task_id>', methods=['GET','POST']) @app.route('/projects/<int:task_id>', methods=['GET','POST'])
@ -102,40 +101,40 @@ def project(task_id:int):
try: try:
task = Task.query.get(task_id) task = Task.query.get(task_id)
except: except:
return render_template('response.html', response = 'Task not found, bad URL') return render_template('pages/response.html', response = 'Task not found, bad URL')
if task is None: if task is None:
return render_template('response.html', response = 'Task not found, bad URL') return render_template('pages/response.html', response = 'Task not found, bad URL')
users = gettaskusers(task_id) users = gettaskusers(task_id)
if request.method == 'GET': if request.method == 'GET':
return render_template("project.html", task = task, users = users) return render_template("pages/project.html", task = task, users = users)
elif request.method == 'POST': elif request.method == 'POST':
# Assigning user to task # Assigning user to task
username = request.form['username'] username = request.form['username']
for user in users: for user in users:
if username == user.username: if username == user.username:
return render_template('response.html', response = 'User already added to task') return render_template('pages/response.html', response = 'User already added to task')
try: try:
userid = User.query.filter_by(username = username).first().id userid = User.query.filter_by(username = username).first().id
except: except:
return render_template('response.html', response = 'User not found, please register.') return render_template('pages/response.html', response = 'User not found, please register.')
if userid is None: if userid is None:
return render_template('response.html', response = 'User not found, please register.') return render_template('pages/response.html', response = 'User not found, please register.')
sqladduser = TaskUser(userid = userid, taskid = task_id) sqladduser = TaskUser(userid = userid, taskid = task_id)
try: try:
db.session.add(sqladduser) db.session.add(sqladduser)
db.session.commit() db.session.commit()
return render_template('response.html', response = 'User added') return render_template('pages/response.html', response = 'User added')
except: except:
return render_template('response.html', response = 'Adding user failed') return render_template('pages/response.html', response = 'Adding user failed')
@app.route('/projects/<int:task_id>/del', methods=['GET','POST']) @app.route('/projects/<int:task_id>/del', methods=['GET','POST'])
def deltask(task_id:int): def deltask(task_id:int):
try: try:
task = Task.query.get(task_id) task = Task.query.get(task_id)
except: except:
return render_template('response.html', response = 'Task not found, bad URL') return render_template('pages/response.html', response = 'Task not found, bad URL')
if task is None: if task is None:
return render_template('response.html', response = 'Task not found, bad URL') return render_template('pages/response.html', response = 'Task not found, bad URL')
try: try:
taskusers = TaskUser.query.filter_by(taskid = task_id).all() taskusers = TaskUser.query.filter_by(taskid = task_id).all()
except: except:
@ -147,35 +146,35 @@ def deltask(task_id:int):
db.session.delete(task) db.session.delete(task)
db.session.commit() db.session.commit()
except: except:
return render_template('response.html', response = 'Deleting task failed') return render_template('pages/response.html', response = 'Deleting task failed')
try: try:
if taskusers != None: if taskusers != None:
for taskuser in taskusers: for taskuser in taskusers:
db.session.delete(taskuser) db.session.delete(taskuser)
db.session.commit() db.session.commit()
except: except:
return render_template('response.html', response = 'Deleting user assignment to task failed') return render_template('pages/response.html', response = 'Deleting user assignment to task failed')
return render_template('response.html', response = 'Task deleted') return render_template('pages/response.html', response = 'Task deleted')
else: else:
return render_template('deltask.html', task = task) return render_template('pages/deltask.html', task = task)
if request.method == 'POST': if request.method == 'POST':
password = request.form['password'] password = request.form['password']
if len(password) < 1 or len(password) > 500: if len(password) < 1 or len(password) > 500:
return render_template('response.html', response = "Password lenght invalid, only smaller then 500 charachters allowed") return render_template('pages/response.html', response = "Password lenght invalid, only smaller then 500 charachters allowed")
# Check password # Check password
if password != ADMINPASS and password != User.query.get(creatorid).password: if password != ADMINPASS and password != User.query.get(creatorid).password:
return render_template('response.html', response = 'Wrong password') return render_template('pages/response.html', response = 'Wrong password')
# Delete task # Delete task
try: try:
db.session.delete(task) db.session.delete(task)
db.session.commit() db.session.commit()
except: except:
return render_template('response.html', response = 'Deleting task failed') return render_template('pages/response.html', response = 'Deleting task failed')
try: try:
if taskusers != None: if taskusers != None:
for taskuser in taskusers: for taskuser in taskusers:
db.session.delete(taskuser) db.session.delete(taskuser)
db.session.commit() db.session.commit()
except: except:
return render_template('response.html', response = 'Deleting user assignment to task failed') return render_template('pages/response.html', response = 'Deleting user assignment to task failed')
return render_template('response.html', response = 'Task deleted') return render_template('pages/response.html', response = 'Task deleted')

View File

@ -0,0 +1,2 @@
<footer>
</footer>

View File

@ -0,0 +1,8 @@
<header>
<nav class="container">
<ul>
<li class="current"><a href="/">Home</a></li>
<li><a href="/register">Register</a></li>
</ul>
</nav>
</header>

View File

@ -0,0 +1,31 @@
{% extends "layouts/base.html" %}
{% block content %}
<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-index">
<section>
<div class="btn">
<a href="/addtask">Add new task</a>
</div>
<div class="tasks-wrap">
<h1>Tasks</h1>
{% for task in tasks %}
<div class="task">
<a href="/projects/{{task.id}}">
<div>{{task.id}}.</div>
<div>{{task.name}}</div>
</a>
</div>
{% endfor %}
</div>
</section>
</main>
<footer>
</footer>
{% endblock content %}

View File

@ -0,0 +1,15 @@
<!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>Task manager</title>
</head>
<body>
{% include "includes/header.html" %}
{% block content %}
{% endblock content %}
{% include "includes/footer.html" %}
</body>
</html>

View File

@ -0,0 +1,25 @@
{% extends "layouts/base.html" %}
{% block content %}
<main class="container page page-addtask">
<h1>Create new task</h1>
<div class="form-wrap">
<form action="/addtask" method="POST">
<div class="form-input">
<label for="taskname" class="label">Task name:</label>
<input type="text" name="taskname" id="taskname" required />
</div>
<div class="form-input">
<label for="taskdesc" class="label">Description:</label>
<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>
</div>
</div>
</form>
</main>
{% endblock content %}

View File

@ -0,0 +1,19 @@
{% extends "layouts/base.html" %}
{% block content %}
<body>
<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>
{% endblock content %}

View File

@ -0,0 +1,21 @@
{% extends "layouts/base.html" %}
{% block content %}
<main class="container page page-index">
<section>
<div class="btn">
<a href="/addtask">Add new task</a>
</div>
<div class="tasks-wrap">
<h1>Tasks</h1>
{% for task in tasks %}
<div class="task">
<a href="/projects/{{task.id}}">
<div>{{task.id}}.</div>
<div>{{task.name}}</div>
</a>
</div>
{% endfor %}
</div>
</section>
</main>
{% endblock content %}

View File

@ -0,0 +1,36 @@
{% extends "layouts/base.html" %}
{% block content %}
<main class="container page page-project">
<section >
<h1>{{task.name}}</h1>
<label class="label underline">Description</label>
<p>{{task.desc}}</p>
</section>
<section class="section-task">
<div>
<label class="label underline">Users added to this task</label>
{% for user in users %}
<div class="user-info-wrap">
<div><b>Username:</b> {{user.username}}</div>
<div><b>Contact info:</b> {{user.contact}}</div>
</div>
{% endfor %}
</div>
<div>
<label class="label underline"> Add person to task</label>
<div class="form-wrap">
<form action="/projects/{{task.id}}" method="POST">
<div class="form-input">
<label for="username" class="label">Username:</label>
<input type="text" name="username" id="username" required />
</div>
<div class="btn-wrap">
<button class="btn">Submit</button>
</div>
</form>
<p><a href="/projects/{{task.id}}/del">DELETE TASK</a><p>
</div>
</div>
</section>
</main>
{% endblock content %}

View File

@ -0,0 +1,24 @@
{% extends "layouts/base.html" %}
{% block content %}
<main class="container page page-register">
<div class="form-wrap">
<form action="/register" method="POST">
<div class="form-input">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required />
</div>
<div class="form-input">
<label for="contact">Contact:</label>
<input type="text" name="contact" id="contact" required />
</div>
<div class="form-input">
<label for="password">Password:</label>
<input type="password" name="password" placeholder="optional" id="password"/>
</div>
<div class="btn-wrap">
<button class="btn">Submit</button>
</div>
</form>
</div>
</main>
{% endblock content %}

View File

@ -0,0 +1,8 @@
{% extends "layouts/base.html" %}
{% block content %}
<main class="container page page-project">
<section >
<p>{{response}}<p>
</section>
</main>
{% endblock content %}