Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. from flask import Flask
  2. from flask import render_template
  3. from flask import request
  4. from flask import redirect
  5. from string import Template
  6.  
  7. app = Flask(__name__)
  8.  
  9. @app.route('/')
  10. def index():
  11. greeting = "Hello World"
  12. return render_template("index.html") #greeting=greeting)
  13.  
  14. @app.route('/login')
  15. def login():
  16. return render_template("login.html")
  17.  
  18. @app.route('/register')
  19. def register():
  20. return render_template("register.html")
  21.  
  22. table_template = Template("""
  23. <table style="border:1px solid black">
  24. <tr>
  25. <td style="border:1px solid black">ID</td>
  26. <td style="border:1px solid black">Name</td>
  27. <td style="border:1px solid black">Description</td>
  28. <td style="border:1px solid black">Action</td>
  29. <td style="border:1px solid black">List Name:</td>
  30. </tr>
  31. ${data}
  32. </table>
  33. """)
  34.  
  35. row_template = Template("""
  36. <tr>
  37. <td style="border:1px solid black">${id}</td>
  38. <td style="border:1px solid black">${name}</td>
  39. <td style="border:1px solid black">${description}</td>
  40. <td style="border:1px solid black"><a href="/todo/delete/${id}">Delete</a></td>
  41. <td style="border:1px solid black"><a href="/todo/update/${id}">Update</a></td>
  42. </tr>
  43. """)
  44.  
  45. add_row_template = Template("""
  46. <form method="POST" action="/todo/add">
  47. Table Name: Test! |
  48. ID #:<input type="number" name="id"></input>
  49. Task Name:<input name="task_name"></input>
  50. Task Description:<input name="task_description"></input>
  51. <input type="submit" name="send" value="Create task">
  52. </form>
  53. """)
  54.  
  55. change_template = Template("""
  56. <form method="POST" action="/todo/update/${id}">
  57. ID # Being Edited:<h2>${id}</h2>
  58. Task Name:<input name="task_name"></input>
  59. Task Description:<input name="task_description"></input>
  60. <input type="submit" name="send" value="Update Task">
  61. </form>
  62. """)
  63.  
  64. @app.route('/todo/view', methods=['GET'])
  65. def todolist():
  66. html = ''
  67. data = ''
  68. lines = open('todolists_names.txt').readlines()
  69. for l in lines:
  70. (id, name, description) = l.strip('\n').split('-', 2)
  71. data += row_template.substitute(id=id, name=name, description=description)
  72. # a = open("todolists_names.txt","r")
  73. # b = a.readlines()
  74. # for name in b:
  75. # blah = open("templates/test.txt","a")
  76. # blah.write(TODOLIST_TEMPLATE.substitute(list_name=name))
  77. return (add_row_template.substitute(table_name="test")+
  78. table_template.substitute(data=data))
  79.  
  80. @app.route('/todo/add', methods=["GET","POST"])
  81. def add():
  82. if request.method == "POST":
  83. id = request.form['id']
  84. task_name = request.form['task_name']
  85. task_description = request.form['task_description']
  86. check = open("todolists_names.txt","r").readlines()
  87. id_list = []
  88. for l in check:
  89. (existent_id, name, description) = l.strip('\n').split('-', 2)
  90. id_list.append(int(existent_id))
  91. if int(id) in id_list:
  92. return('Sorry! Please use a unique ID. <a href="/todo/view">Go Back.</a>')
  93. else:
  94. f = open("todolists_names.txt","a")
  95. f.write(f"{id} - {task_name} - {task_description}" + "\n")
  96. f.close()
  97. return redirect('view')
  98. else:
  99. return "No Accept GET! Go Get your own!"
  100.  
  101. @app.route('/todo/delete/<some_id>', methods=["GET"])
  102. def delete(some_id):
  103. existing_IDs = []
  104. for l in open("todolists_names.txt").readlines():
  105. (existent_id, name, description) = l.strip('\n').split('-', 2)
  106. existing_IDs.append(int(existent_id))
  107.  
  108. if request.method == "GET" and (int(some_id) in existing_IDs):
  109. with open("todolists_names.txt","r+") as f:
  110. new_f = f.readlines()
  111. f.seek(0)
  112. for line in new_f:
  113. if some_id not in line:
  114. f.write(line)
  115. f.truncate()
  116. return redirect('../view')
  117. else:
  118. return('Hmm. Invalid ID#!<a href="../view">Go Back.</a>')
  119. #return redirect('view')
  120.  
  121.  
  122. @app.route('/todo/update/<id>', methods=["GET","POST"])
  123. def change(id):
  124. existing_IDs = []
  125. for l in open("todolists_names.txt").readlines():
  126. (existent_id, name, description) = l.strip('\n').split('-', 2)
  127. existing_IDs.append(int(existent_id))
  128.  
  129. if request.method == "GET" and (int(id) in existing_IDs):
  130. return(change_template.substitute(id=id))
  131.  
  132. elif request.method == "POST":
  133. task_name = request.form['task_name']
  134. task_description = request.form['task_description']
  135. with open("todolists_names.txt","r+") as f:
  136. new_f = f.readlines()
  137. f.seek(0)
  138. for line in new_f:
  139. if id not in line:
  140. f.write(line)
  141. f.truncate()
  142. f.write(f"{id} - {task_name} - {task_description}" + "\n")
  143. return redirect('../view')
  144. else:
  145. return('Hmm. Invalid ID#!<a href="../view">Go Back.</a>')
  146.  
  147. if __name__ == "__main__":
  148. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement