Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- from flask import render_template
- from flask import request
- from flask import redirect
- from string import Template
- app = Flask(__name__)
- @app.route('/')
- def index():
- greeting = "Hello World"
- return render_template("index.html") #greeting=greeting)
- @app.route('/login')
- def login():
- return render_template("login.html")
- @app.route('/register')
- def register():
- return render_template("register.html")
- table_template = Template("""
- <table style="border:1px solid black">
- <tr>
- <td style="border:1px solid black">ID</td>
- <td style="border:1px solid black">Name</td>
- <td style="border:1px solid black">Description</td>
- <td style="border:1px solid black">Action</td>
- <td style="border:1px solid black">List Name:</td>
- </tr>
- ${data}
- </table>
- """)
- row_template = Template("""
- <tr>
- <td style="border:1px solid black">${id}</td>
- <td style="border:1px solid black">${name}</td>
- <td style="border:1px solid black">${description}</td>
- <td style="border:1px solid black"><a href="/todo/delete/${id}">Delete</a></td>
- <td style="border:1px solid black"><a href="/todo/update/${id}">Update</a></td>
- </tr>
- """)
- add_row_template = Template("""
- <form method="POST" action="/todo/add">
- Table Name: Test! |
- ID #:<input type="number" name="id"></input>
- Task Name:<input name="task_name"></input>
- Task Description:<input name="task_description"></input>
- <input type="submit" name="send" value="Create task">
- </form>
- """)
- change_template = Template("""
- <form method="POST" action="/todo/update/${id}">
- ID # Being Edited:<h2>${id}</h2>
- Task Name:<input name="task_name"></input>
- Task Description:<input name="task_description"></input>
- <input type="submit" name="send" value="Update Task">
- </form>
- """)
- @app.route('/todo/view', methods=['GET'])
- def todolist():
- html = ''
- data = ''
- lines = open('todolists_names.txt').readlines()
- for l in lines:
- (id, name, description) = l.strip('\n').split('-', 2)
- data += row_template.substitute(id=id, name=name, description=description)
- # a = open("todolists_names.txt","r")
- # b = a.readlines()
- # for name in b:
- # blah = open("templates/test.txt","a")
- # blah.write(TODOLIST_TEMPLATE.substitute(list_name=name))
- return (add_row_template.substitute(table_name="test")+
- table_template.substitute(data=data))
- @app.route('/todo/add', methods=["GET","POST"])
- def add():
- if request.method == "POST":
- id = request.form['id']
- task_name = request.form['task_name']
- task_description = request.form['task_description']
- check = open("todolists_names.txt","r").readlines()
- id_list = []
- for l in check:
- (existent_id, name, description) = l.strip('\n').split('-', 2)
- id_list.append(int(existent_id))
- if int(id) in id_list:
- return('Sorry! Please use a unique ID. <a href="/todo/view">Go Back.</a>')
- else:
- f = open("todolists_names.txt","a")
- f.write(f"{id} - {task_name} - {task_description}" + "\n")
- f.close()
- return redirect('view')
- else:
- return "No Accept GET! Go Get your own!"
- @app.route('/todo/delete/<some_id>', methods=["GET"])
- def delete(some_id):
- existing_IDs = []
- for l in open("todolists_names.txt").readlines():
- (existent_id, name, description) = l.strip('\n').split('-', 2)
- existing_IDs.append(int(existent_id))
- if request.method == "GET" and (int(some_id) in existing_IDs):
- with open("todolists_names.txt","r+") as f:
- new_f = f.readlines()
- f.seek(0)
- for line in new_f:
- if some_id not in line:
- f.write(line)
- f.truncate()
- return redirect('../view')
- else:
- return('Hmm. Invalid ID#!<a href="../view">Go Back.</a>')
- #return redirect('view')
- @app.route('/todo/update/<id>', methods=["GET","POST"])
- def change(id):
- existing_IDs = []
- for l in open("todolists_names.txt").readlines():
- (existent_id, name, description) = l.strip('\n').split('-', 2)
- existing_IDs.append(int(existent_id))
- if request.method == "GET" and (int(id) in existing_IDs):
- return(change_template.substitute(id=id))
- elif request.method == "POST":
- task_name = request.form['task_name']
- task_description = request.form['task_description']
- with open("todolists_names.txt","r+") as f:
- new_f = f.readlines()
- f.seek(0)
- for line in new_f:
- if id not in line:
- f.write(line)
- f.truncate()
- f.write(f"{id} - {task_name} - {task_description}" + "\n")
- return redirect('../view')
- else:
- return('Hmm. Invalid ID#!<a href="../view">Go Back.</a>')
- if __name__ == "__main__":
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement