taskmanager/taskmanager/routes.py

69 lines
2.2 KiB
Python

from flask import render_template, request, redirect
from taskmanager import app, db
from taskmanager.functions import *
from taskmanager.models import *
@app.route('/', methods=['GET'])
def index():
tasks = Task.query.all()
return render_template('index.html', tasks = tasks)
@app.route('/addtask', methods=['GET','POST'])
def addtask():
if request.method == 'GET':
return render_template('addtask.html')
elif request.method == 'POST':
taskname = request.form['taskname']
taskdesc = request.form['taskdesc']
sqladdtask = Task(name = taskname, desc = taskdesc)
try:
db.session.add(sqladdtask)
db.session.commit()
return 'Task added'
except:
return 'Adding task failed'
@app.route('/register', methods=['POST', 'GET'])
def register():
if request.method == 'GET':
return render_template('register.html')
elif request.method == 'POST':
username = request.form['username']
contact = request.form['contact']
sqladduser = User(username = username, contact = contact)
try:
db.session.add(sqladduser)
db.session.commit()
return 'User added'
except:
return 'Adding user failed'
else:
return 'HTTP request method not recogniezed'
@app.route('/projects/<int:task_id>', methods=['GET','POST'])
def project(task_id:int):
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']
if username in users:
return 'User already added to task'
try:
userid = User.query.filter_by(username = username).first().id
except:
return 'User not found, please <a href="/register">register</a>.'
sqladduser = TaskUser(userid = userid, taskid = task_id)
try:
db.session.add(sqladduser)
db.session.commit()
return 'User added'
except:
return 'Adding user failed'