logic.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, send_file
  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. import csv
  11. import subprocess as sp
  12. from .iconfetch import *
  13. logic = Blueprint('logic', __name__)
  14. # User Home Profile
  15. @logic.route('/', methods=['GET', 'POST'])
  16. @login_required
  17. def home():
  18. # generate csv of bookmarks
  19. def generate_csv():
  20. header = ['name', 'link', 'group', 'status']
  21. alphanumeric = string.ascii_letters + string.digits
  22. ralphanum = ''.join(secrets.choice(alphanumeric) for i in range(16))
  23. open(f'app/static/generated/{ralphanum}.csv', 'a').close
  24. with open(f'app/static/generated/{ralphanum}.csv', 'w', encoding='UTF8', newline='') as f:
  25. writer = csv.writer(f, delimiter=',', quotechar='"')
  26. writer.writerow(header)
  27. for bg, bml in bb_dic.items():
  28. for bm in bml:
  29. writer.writerow([bm.name, bm.link, bg, bm.status])
  30. p1 = sp.run(f'curl --upload-file app/static/generated/{ralphanum}.csv https://transfer.raqnet.org/',
  31. shell=True,
  32. capture_output=True,
  33. text=True
  34. )
  35. # clean up
  36. p2 = sp.run(f'rm app/static/generated/{ralphanum}.csv', shell=True)
  37. return redirect(p1.stdout, code=302)
  38. # initialize dictionary of bookmarks and groups
  39. # owned groups
  40. bgroups = Groups.query.filter_by(owner=current_user.id).all()
  41. # owned bookmarks
  42. bmarks = Bookmark.query.filter_by(owner=current_user.id).all()
  43. # populate bookmark dict for show
  44. bb_dic = dict() # dict of lists
  45. for bg in bgroups:
  46. inter_list = list()
  47. for bm in bmarks:
  48. if bm.group == bg.group:
  49. inter_list.append(bm)
  50. bb_dic[bg.group] = inter_list
  51. # POST request method
  52. if request.method == "POST":
  53. username = request.form.get('username')
  54. passwd = request.form.get('password')
  55. passwdc = request.form.get('password_confirm')
  56. new_pic = request.files.get('profile_image')
  57. dl_bookml = request.form.get('dl_bookml')
  58. if username:
  59. nusern_dbcall = User.query.filter_by(id=current_user.id).first()
  60. nusern_dbcall.username = username
  61. db.session.commit()
  62. flash('Username updated!', category='success')
  63. elif passwd and passwdc:
  64. if passwd == passwdc:
  65. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  66. npasswd_dbcall.password = generate_password_hash(passwdc, method='sha256')
  67. db.session.commit()
  68. flash('Password successfully updated', category='success')
  69. else:
  70. flash('Passwords must match!', category='error')
  71. elif new_pic:
  72. # generate random filename for uploaded file
  73. alphanumeric = string.ascii_letters + string.digits
  74. ralphanum = ''.join(secrets.choice(alphanumeric) for i in range(16))
  75. new_pic_dbcall = User.query.filter_by(id=current_user.id).first()
  76. if '.png' in new_pic.filename:
  77. new_pic.save(f'app/static/uploads/{ralphanum}.png')
  78. new_pic_dbcall.profile_image = f'{ralphanum}.png'
  79. elif '.jpg' in new_pic.filename or 'jpeg' in new_pic.filename:
  80. new_pic.save(f'app/static/uploads/{ralphanum}.jpeg')
  81. new_pic_dbcall.profile_image = f'{ralphanum}.jpeg'
  82. db.session.commit()
  83. flash('Profile Pic updated!', category='success')
  84. elif dl_bookml:
  85. return generate_csv()
  86. return render_template('home.html', user=current_user, bdic=bb_dic)
  87. # Public Bookmarks
  88. @logic.route('/shared', methods=['GET', 'POST'])
  89. @login_required
  90. def shared():
  91. # owned groups
  92. bgroups = Groups.query.filter_by(owner=current_user.id).all()
  93. # owned bookmarks
  94. bmarks = Bookmark.query.filter_by(owner=current_user.id).all()
  95. bb_dic = dict() # dict of lists
  96. for bg in bgroups:
  97. inter_list = list()
  98. for bm in bmarks:
  99. if bm.group == bg.group and bm.status == 'public':
  100. inter_list.append(bm)
  101. if len(inter_list) != 0:
  102. bb_dic[bg.group] = inter_list
  103. return render_template('shared.html', user=current_user, bdic=bb_dic)
  104. # Create new bookmark
  105. @logic.route('/create', methods=['GET', 'POST'])
  106. @login_required
  107. def create():
  108. # load bookmark groups
  109. bgroups = Groups.query.filter_by(owner=current_user.id).all()
  110. if request.method == 'POST':
  111. wlink = request.form.get('web_link')
  112. wname = request.form.get('web_name')
  113. wgroup = request.form.get('web_group')
  114. ngroup = request.form.get('new_group')
  115. wstatus = request.form.get('share_public_private')
  116. if wlink and wname and wstatus:
  117. # fetch/check icon
  118. sitecon = check_icon(wlink)
  119. if not sitecon:
  120. sitecon = fetch_icon(wlink)
  121. # add bookmark to database
  122. if wgroup == 'cng-555' and ngroup != '':
  123. wgroup = ngroup
  124. new_link = Bookmark(owner=current_user.id,
  125. status=wstatus,
  126. name=wname,
  127. group=wgroup,
  128. link=wlink,
  129. icon=sitecon
  130. )
  131. group_check = Groups.query.filter_by(group=wgroup).first()
  132. if not group_check:
  133. new_group = Groups(owner=current_user.id, group=wgroup)
  134. db.session.add(new_group)
  135. db.session.add(new_link)
  136. db.session.commit()
  137. flash('New Bookmark added!', category='success')
  138. return render_template('create_bookmark.html', user=current_user, bgroups=bgroups)
  139. # Account management
  140. @logic.route('/login', methods=['GET', 'POST'])
  141. def login():
  142. if request.method == 'POST':
  143. email = request.form.get('email')
  144. challenge_passwd = request.form.get('passwd_login')
  145. user = User.query.filter_by(email=email).first()
  146. if user:
  147. if check_password_hash(user.password, challenge_passwd):
  148. flash('Successful Login!', category='success')
  149. login_user(user, remember=True)
  150. return redirect(url_for('logic.home'))
  151. else:
  152. flash('Unsucessful Login!', category='error')
  153. else:
  154. flash('Unsucessful Login!', category='error')
  155. return render_template('login.html', user=current_user)
  156. @logic.route('/logout')
  157. @login_required
  158. def logout():
  159. logout_user()
  160. return redirect(url_for('logic.login'))
  161. @logic.route('/register', methods=['GET', 'POST'])
  162. def register():
  163. pass_list = list()
  164. if request.method == 'POST':
  165. email = request.form.get('email')
  166. username = request.form.get('username')
  167. passwd_1 = request.form.get('passwd_1')
  168. passwd_2 = request.form.get('passwd_2')
  169. # Basic User Input Checks
  170. email_check = User.query.filter_by(email=email).first()
  171. if len(email) < 1:
  172. flash('Your Email must be longer than 0 characters.', category='error')
  173. elif email_check:
  174. flash('This Email is already taken', category='error')
  175. else:
  176. pass_list.append('p')
  177. if len(username) < 1:
  178. flash('Username must be something', category='error')
  179. else:
  180. pass_list.append('p')
  181. if len(passwd_1) < 8 or len(passwd_2) < 8:
  182. flash('Your Password must be longer than or equal to 8 characters.', category='error')
  183. else:
  184. if passwd_1 != passwd_2:
  185. flash('Your Passwords must match!', category='error')
  186. else:
  187. if len(pass_list) == 2:
  188. new_user = User(email=email,
  189. username=username,
  190. password=generate_password_hash(passwd_2, method='sha256')
  191. )
  192. db.session.add(new_user)
  193. db.session.commit()
  194. flash('Account Registration Successful!', category='success')
  195. return redirect(url_for('logic.home'))
  196. else:
  197. flash('Registration Failed', category='error')
  198. return render_template("register.html", user=current_user)