PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/components/view.py

https://gitlab.com/IDNatte/flask-codepolitan
Python | 223 lines | 198 code | 25 blank | 0 comment | 35 complexity | b5ec93c892ffeb720692aa3dfdf28398 MD5 | raw file
  1. from components import blogContent
  2. from components import dbhandler
  3. from components import app
  4. from components import userLogin
  5. from datetime import timedelta
  6. from pymongo import ASCENDING
  7. from pymongo import DESCENDING
  8. from flask import url_for
  9. from flask import render_template
  10. from flask import Session
  11. from flask import session
  12. from flask import redirect
  13. from flask import request
  14. import time
  15. @app.before_request
  16. def sessionPerm():
  17. session.permanent = True
  18. resetTime = app.permanent_session_lifetime = timedelta(hours=24)
  19. @app.route('/index/page')
  20. @app.route('/')
  21. def index():
  22. if session.get('logged_in'):
  23. dataUname = session.get('username')
  24. sessionAdminAuth = userLogin.sessionAdminManager(dataUname)
  25. if sessionAdminAuth == True:
  26. return redirect(url_for('admin'))
  27. else:
  28. return redirect(url_for('member'))
  29. else:
  30. content = blogContent.find().count()
  31. contentCheck = content
  32. data = blogContent.find().sort('post_title', ASCENDING)
  33. return render_template('index.html', data=data, contentCheck=contentCheck, next=next)
  34. @app.route('/write_posts')
  35. def input():
  36. if session.get('logged_in'):
  37. data = blogContent.blog_collection.find()
  38. return render_template('input.html', data=data)
  39. else:
  40. return redirect(url_for('login'))
  41. @app.route('/process', methods=['POST'])
  42. def process():
  43. title = request.form.get('posts_title')
  44. content = request.form.get('posts_content')
  45. desc = request.form.get('posts_descript')
  46. posted_by = session.get('username')
  47. date_posts = time.strftime("%x")
  48. time_posts = time.strftime("%X")
  49. insert = blogContent.insert({'post_title':title, 'post_desc':desc, 'post_content':content, 'date_post':date_posts, 'time_posts':time_posts, 'posted_by':posted_by})
  50. return redirect('/')
  51. @app.route('/delete/', methods=['GET', 'POST'])
  52. def delete():
  53. if session.get('logged_in'):
  54. posts_title = request.form.get('delete_title')
  55. post_id = request.form.get('post_id')
  56. operation = dbhandler.DatabaseHandler()
  57. databaseOperation = operation.databaseBlogDeleteItem(post_id, posts_title)
  58. return redirect('/')
  59. else:
  60. return redirect(url_for('error'))
  61. @app.route('/content/<posts_title>/')
  62. def Edit(posts_title):
  63. data = blogContent.find_one({'post_title':posts_title})
  64. return render_template('edit.html', data=data)
  65. @app.route('/content/<posts_title>/edit', methods = ['POST'])
  66. def Edit_process(posts_title):
  67. title = request.form.get('posts_title')
  68. content = request.form.get('posts_content')
  69. desc = request.form.get('posts_descript')
  70. limiter = posts_title
  71. dbset = blogContent.update({'post_title':limiter}, {'$set':{'post_title':title, 'post_desc':desc, 'post_content':content}})
  72. return redirect('/')
  73. @app.route('/content/<posts_title>/read/')
  74. def readarticle(posts_title):
  75. data = blogContent.find_one({'post_title':posts_title})
  76. return render_template('readarticle.html', data=data)
  77. @app.route('/content/search', methods=['POST'])
  78. def searchcontent():
  79. dataSearch = request.form.get('search_content')
  80. result = blogContent.find({'$text':{'$search':dataSearch}})
  81. resultData = result.count()
  82. checkContent = bool(resultData)
  83. checkUserLevel = userLogin.userLevel(session.get('username'))
  84. if checkContent == True:
  85. return render_template('searchres.html', result=result, checkUserLevel=checkUserLevel)
  86. elif checkContent == False:
  87. return render_template('file404.html', dataSearch=dataSearch)
  88. @app.route('/login', methods = ['GET', 'POST'])
  89. def login():
  90. username = request.form.get('user_name')
  91. passwd = request.form.get('user_passwd')
  92. if request.method == 'POST':
  93. userAdminAuth = userLogin.checkUserAdmin(username, passwd)
  94. userMemberAuth = userLogin.checkUserMember(username, passwd)
  95. dataStatusAuth = userLogin.checkStatus(username, '1')
  96. if not session.get('logged_in'):
  97. if userAdminAuth == True and userMemberAuth == False:
  98. userLogin.setLoginStatus(username)
  99. if dataStatusAuth == False:
  100. session['username'] = username
  101. session['logged_in'] = True
  102. return redirect(url_for('admin'))
  103. elif dataStatusAuth == True:
  104. error3 = 'User with username "%s" has been login in other device' %(username)
  105. return render_template('login.html', error3=error3)
  106. elif userAdminAuth == False and userMemberAuth == True:
  107. userLogin.setLoginStatus(username)
  108. if dataStatusAuth == False:
  109. session['username'] = username
  110. session['logged_in'] = True
  111. return redirect(url_for('member'))
  112. elif dataStatusAuth == True:
  113. error3 = 'User with username "%s" has been login in other device' %(username)
  114. return render_template('login.html', error3=error3)
  115. else:
  116. error2 = 'Invalid username and password'
  117. return render_template('login.html', error2=error2)
  118. else:
  119. error1 = 'You has been logged in as %s' % (session.get('username'))
  120. return render_template('login.html', error1=error1)
  121. return render_template('login.html')
  122. @app.route('/admin')
  123. def admin():
  124. if session.get('logged_in'):
  125. uname = session.get('username')
  126. content = blogContent.find().count()
  127. userAuth = userLogin.userLevel(uname)
  128. contentCheck = content
  129. data = blogContent.find().sort('_id', DESCENDING)
  130. return render_template('index.html', uname=uname, data=data, contentCheck=contentCheck, userAuth=userAuth)
  131. else:
  132. return redirect(url_for('error'))
  133. @app.route('/user')
  134. def member():
  135. if session.get('logged_in'):
  136. uname = session.get('username')
  137. data = blogContent.find().sort('_id', DESCENDING)
  138. content = blogContent.find().count()
  139. userAuth = userLogin.userLevel(uname)
  140. contentCheck = content
  141. return render_template('index.html', uname=uname, data=data, contentCheck=contentCheck, userAuth=userAuth)
  142. else:
  143. return redirect(url_for('error'))
  144. @app.route('/logout')
  145. def logout():
  146. username = session.get('username')
  147. userLogin.resetLoginStatus(username)
  148. session.pop('logged_in', None)
  149. session.pop('forbidden_login_member', None)
  150. session.pop('forbidden_login_admin', None)
  151. session.clear()
  152. return redirect(url_for('index'))
  153. @app.route('/error')
  154. def error():
  155. return render_template('error403.html')
  156. @app.route('/u/<user_name>')
  157. def user(user_name):
  158. if session.get('logged_in'):
  159. dataCheckAuthLevel = userLogin.sessionAdminManager(user_name)
  160. if dataCheckAuthLevel == True:
  161. dataPost = blogContent.find().sort('_id', DESCENDING)
  162. dataUser = userLogin.user_collection.find()
  163. checkContent = blogContent.find().count()
  164. userCheck = userLogin.userLevel(user_name)
  165. userCount = userLogin.user_collection.find().count()
  166. return render_template('dashboard.html', dataUser=dataUser, dataPost=dataPost, checkContent=checkContent, userCheck=userCheck, userCount=userCount)
  167. else:
  168. mypost = blogContent.find().sort('_id', DESCENDING)
  169. user =session.get('username')
  170. return render_template('dashboard.html', mypost=mypost, user=user)
  171. else:
  172. return redirect(url_for('error'))
  173. @app.route('/regist', methods=['GET', 'POST'])
  174. def register():
  175. if request.method == 'POST':
  176. regist_username = request.form.get('user_name')
  177. regist_passwd = request.form.get('user_password')
  178. regist_mail = request.form.get('user_mail')
  179. checkUserData = userLogin.checkUserMember(regist_username, regist_passwd)
  180. conf_passwd = request.form.get('user_passwd_confirm')
  181. if conf_passwd == regist_passwd:
  182. if checkUserData == True:
  183. error = 'Account has been registered'
  184. return render_template('regist.html', error=error)
  185. else:
  186. dataRegist = userLogin.userRegist(regist_username, regist_passwd, regist_mail)
  187. success = 'Register account success'
  188. return render_template('login.html', success=success)
  189. else:
  190. error2 = 'Password not match'
  191. return render_template('regist.html', error2=error2)
  192. return render_template('regist.html')
  193. @app.route('/u/delete', methods=['GET', 'POST'])
  194. def deluser():
  195. userGet = request.form.get('delete_user')
  196. emailGet = request.form.get('user_email')
  197. userDeleteAccount = userLogin.delUser(userGet, emailGet)
  198. return redirect('/')