def task_lists(): # connecting to the database db = get_db() lists = db.execute("SELECT * FROM taskslist").fetchall() return render_template("tasks_list/tasklists.html", lists=lists)
def delete_tasklist(index): # connecting to the database db = get_db() db.execute(f"DELETE FROM taskslist WHERE id = '{index}' ") db.commit() return redirect(url_for("taskslist.task_lists"))
def sort(): # connecting to the database db = get_db() #Retrieve task list in alphabetical order lists = db.execute('SELECT id, name, last_updated, created_at' ' FROM taskslist' ' ORDER BY name ASC').fetchall() return render_template("tasks_list/tasklists.html", lists=lists)
def delete_task(index): db = get_db() task_list_id = db.execute("SELECT taskslist_id FROM tasks WHERE id LIKE ? " ,(index,)).fetchone() #Deleye value from database db.execute(f"DELETE FROM tasks WHERE id = '{index}' ") db.commit() return redirect(url_for('tasks.tasks_' , list_index = task_list_id['taskslist_id']))
def tasks_(list_index): #Get the DB connection db = get_db() tasklist = db.execute(f"SELECT * FROM taskslist WHERE id = {list_index}").fetchone() #Get tasks by tasks list id tasks = db.execute(f"SELECT * FROM tasks WHERE taskslist_id = {list_index}").fetchall() #Render the tasks template return render_template("tasks/tasks.html", tasklist = tasklist, tasks = tasks, list_index = list_index)
def sort(list_index): #Connecting to the database db = get_db() tasklist = db.execute(f"SELECT * FROM taskslist WHERE id = {list_index}").fetchone() tasks = db.execute("""SELECT id, name, last_updated, created_at, status, priority, description FROM tasks WHERE taskslist_id LIKE ? ORDER BY name ASC""",(list_index,) ).fetchall() return render_template("tasks/tasks.html", tasklist = tasklist, tasks = tasks, list_index = list_index)
def create_tasklist(): create_tasklist = CreateTaskList() if create_tasklist.validate_on_submit(): name = create_tasklist.name.data # connecting to the database db = get_db() db.execute("INSERT INTO taskslist (name) VALUES (?);", (name, )) db.commit() return redirect(url_for('taskslist.task_lists')) return render_template("tasks_list/create_tasklist.html", form=create_tasklist)
def create_task(index): create_task = CreateTaskForm() if create_task.validate_on_submit(): new_name = create_task.new_name.data new_description = create_task.new_description.data priority = create_task.priority.data # connecting to the database db = get_db() db.execute("INSERT INTO tasks (name,status,priority,description,taskslist_id) VALUES (?,?,?,?,?);" , (new_name,"New",priority,new_description,index,)) db.commit() return redirect(url_for("tasks.tasks_",list_index = index)) return render_template("tasks/create_task.html", form = create_task)
def edit_tasklist_name(index): edit_name_form = EditNameForm() if edit_name_form.validate_on_submit(): new_name = edit_name_form.new_name.data # connecting to the database db = get_db() db.execute( f"UPDATE taskslist SET name = '{new_name}' , last_updated = '{datetime.datetime.now()}' WHERE id = '{index}' " ) db.commit() return redirect(url_for('taskslist.task_lists')) return render_template("tasks_list/edit_name.html", form=edit_name_form)
def login(): login = LoginForm() if login.validate_on_submit(): # read values from the login wtform username = login.username.data password = login.password.data # get the DB connection db = get_db() # insert user into db try: # get user by username user = db.execute('SELECT * FROM users WHERE username LIKE ?', (username, )).fetchone() # check if user exists in db if user != None: # check if password is correct if user['password'] == password: # store the user id and username in the session session['uid'] = user['id'] session['username'] = user['username'] return redirect(url_for('taskslist.task_lists')) else: print("Wrong Password!") else: return redirect(url_for('user.add_user')) except sqlite3.Error as er: print('SQLite error: %s' % (' '.join(er.args))) return redirect("/404") return render_template('user/login.html', form=login)
def add_user(): user = AddUser() if user.validate_on_submit(): username = user.username.data password = user.password.data first_name = user.first_name.data last_name = user.last_name.data # get the DB connection db = get_db() # insert user into DB try: # execute our insert SQL statement db.execute( "INSERT INTO users (username, password , firstname , lastname) VALUES (?, ? , ? , ?);", ( username, password, first_name, last_name, )) # write changes to DB db.commit() return redirect(url_for('user.login')) except sqlite3.Error as er: print('SQLite error: %s' % (' '.join(er.args))) return redirect("/404") return render_template('user/add.html', form=user)
def edit_task(index): #Create instance from edit form edit_task_form = EditTaskForm() if edit_task_form.validate_on_submit(): new_name = edit_task_form.new_name.data new_description = edit_task_form.new_description.data status = edit_task_form.status.data priority = edit_task_form.priority.data db = get_db() #Get tasks list id from tasks table by tasks id task_list_id = db.execute("SELECT taskslist_id FROM tasks WHERE id LIKE ? " ,(index,)).fetchone() #Values update db.execute(f"UPDATE tasks SET name = '{new_name}',status = '{status}',priority = '{priority}' , last_updated = '{datetime.datetime.now()}', description = '{new_description}' WHERE id = '{index}' ") db.commit() return redirect(url_for('tasks.tasks_', list_index = task_list_id['taskslist_id'])) return render_template("tasks/edit_task.html", form = edit_task_form)