logic.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Site Back-End Logic
  2. from hmac import new
  3. from unicodedata import category
  4. from flask import Blueprint, render_template, request, flash, redirect, url_for
  5. from flask_login import login_user, login_required, logout_user, current_user
  6. from werkzeug.security import generate_password_hash, check_password_hash
  7. from .models import User, Bookmark, Groups
  8. from . import db
  9. import string, secrets
  10. logic = Blueprint('logic', __name__)
  11. @logic.route('/', methods=['GET', 'POST'])
  12. @login_required
  13. def home():
  14. # owned groups
  15. bgroups = Groups.query.filter_by(owner=current_user.id).all()
  16. # owned bookmarks
  17. bmarks = Bookmark.query.filter_by(owner=current_user.id).all()
  18. bb_dic = dict() # dict of lists
  19. for bg in bgroups:
  20. inter_list = list()
  21. for bm in bmarks:
  22. if bm.group == bg.group:
  23. inter_list.append(bm)
  24. bb_dic[bg.group] = inter_list
  25. if request.method == "POST":
  26. username = request.form.get('username')
  27. passwd = request.form.get('password')
  28. passwdc = request.form.get('password_confirm')
  29. new_pic = request.files.get('profile_image')
  30. if username:
  31. nusern_dbcall = User.query.filter_by(id=current_user.id).first()
  32. nusern_dbcall.username = username
  33. db.session.commit()
  34. flash('Username updated!', category='success')
  35. elif passwd and passwdc:
  36. if passwd == passwdc:
  37. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  38. npasswd_dbcall.password = generate_password_hash(passwdc, method='sha256')
  39. db.session.commit()
  40. flash('Password successfully updated', category='success')
  41. else:
  42. flash('Passwords must match!', category='error')
  43. elif new_pic:
  44. # generate random filename for uploaded file
  45. alphanumeric = string.ascii_letters + string.digits
  46. ralphanum = ''.join(secrets.choice(alphanumeric) for i in range(16))
  47. new_pic_dbcall = User.query.filter_by(id=current_user.id).first()
  48. if '.png' in new_pic.filename:
  49. new_pic.save(f'app/static/uploads/{ralphanum}.png')
  50. new_pic_dbcall.profile_image = f'{ralphanum}.png'
  51. elif '.jpg' in new_pic.filename or 'jpeg' in new_pic.filename:
  52. new_pic.save(f'app/static/uploads/{ralphanum}.jpeg')
  53. new_pic_dbcall.profile_image = f'{ralphanum}.jpeg'
  54. db.session.commit()
  55. flash('Profile Pic updated!', category='success')
  56. return render_template('home.html', user=current_user, bdic=bb_dic)
  57. @logic.route('/create', methods=['GET', 'POST'])
  58. @login_required
  59. def create():
  60. bgroups = Groups.query.filter_by(owner=current_user.id).all()
  61. if request.method == 'POST':
  62. wlink = request.form.get('web_link')
  63. wname = request.form.get('web_name')
  64. wgroup = request.form.get('web_group')
  65. ngroup = request.form.get('new_group')
  66. wstatus = request.form.get('share_public_private')
  67. if wlink and wname and wstatus:
  68. if wgroup == 'cng-555' and ngroup != '':
  69. wgroup = ngroup
  70. new_link = Bookmark(owner=current_user.id, status=wstatus, name=wname, group=wgroup, link=wlink)
  71. group_check = Groups.query.filter_by(group=wgroup).first()
  72. if not group_check:
  73. new_group = Groups(owner=current_user.id, group=wgroup)
  74. db.session.add(new_group)
  75. db.session.add(new_link)
  76. db.session.commit()
  77. flash('New Bookmark added!', category='success')
  78. return render_template('create_bookmark.html', user=current_user, bgroups=bgroups)
  79. @logic.route('/shared', methods=['GET'])
  80. @login_required
  81. def shared():
  82. pass
  83. return render_template('shared.html', user=current_user)
  84. @logic.route('/login', methods=['GET', 'POST'])
  85. def login():
  86. if request.method == 'POST':
  87. email = request.form.get('email')
  88. challenge_passwd = request.form.get('passwd_login')
  89. user = User.query.filter_by(email=email).first()
  90. if user:
  91. if check_password_hash(user.password, challenge_passwd):
  92. flash('Successful Login!', category='success')
  93. login_user(user, remember=True)
  94. return redirect(url_for('logic.home'))
  95. else:
  96. flash('Unsucessful Login!', category='error')
  97. else:
  98. flash('Unsucessful Login!', category='error')
  99. return render_template('login.html', user=current_user)
  100. @logic.route('/logout')
  101. @login_required
  102. def logout():
  103. logout_user()
  104. return redirect(url_for('logic.login'))
  105. @logic.route('/register', methods=['GET', 'POST'])
  106. def register():
  107. pass_list = list()
  108. if request.method == 'POST':
  109. email = request.form.get('email')
  110. username = request.form.get('username')
  111. passwd_1 = request.form.get('passwd_1')
  112. passwd_2 = request.form.get('passwd_2')
  113. # Basic User Input Checks
  114. email_check = User.query.filter_by(email=email).first()
  115. if len(email) < 1:
  116. flash('Your Email must be longer than 0 characters.', category='error')
  117. elif email_check:
  118. flash('This Email is already taken', category='error')
  119. else:
  120. pass_list.append('p')
  121. if len(username) < 1:
  122. flash('Username must be something', category='error')
  123. else:
  124. pass_list.append('p')
  125. if len(passwd_1) < 8 or len(passwd_2) < 8:
  126. flash('Your Password must be longer than or equal to 8 characters.', category='error')
  127. else:
  128. if passwd_1 != passwd_2:
  129. flash('Your Passwords must match!', category='error')
  130. else:
  131. if len(pass_list) == 2:
  132. new_user = User(email=email, username=username, password=generate_password_hash(passwd_2, method='sha256'))
  133. db.session.add(new_user)
  134. db.session.commit()
  135. flash('Account Registration Successful!', category='success')
  136. return redirect(url_for('logic.home'))
  137. else:
  138. flash('Registration Failed', category='error')
  139. return render_template("register.html", user=current_user)