dashboards.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from flask import Blueprint, render_template, request, flash, redirect, url_for
  2. from flask_login import login_required, current_user
  3. from werkzeug.security import generate_password_hash, check_password_hash
  4. from .models import User, Art
  5. from . import db
  6. from . import dispatch
  7. from app.lib import clean_file as cf
  8. from app.lib import tools
  9. dashboards = Blueprint('dashboards', __name__)
  10. # Main Pages
  11. @dashboards.route('/', methods=['GET', 'POST'])
  12. def market():
  13. return render_template('market.html', user=current_user)
  14. @dashboards.route('/profile', methods=['GET', 'POST'])
  15. @login_required
  16. def profile():
  17. # fetch post request
  18. if request.method == "POST":
  19. cpasswd = request.form.get('current_password')
  20. passwd = request.form.get('password')
  21. passwd_con = request.form.get('password_confirm')
  22. # password check before update
  23. if passwd and passwd_con and cpasswd:
  24. if passwd == passwd_con and check_password_hash(current_user.password, cpasswd):
  25. npasswd_dbcall = User.query.filter_by(id=current_user.id).first()
  26. npasswd_dbcall.password = generate_password_hash(passwd_con, method='sha256')
  27. db.session.commit()
  28. flash('Updated password!', category='success')
  29. else:
  30. flash('Password update failed!', category='error')
  31. my_art = Art.query.filter_by(owner=current_user.id).all()
  32. my_creation = Art.query.filter_by(creator=current_user.id).all()
  33. return render_template('profile.html', user = current_user, my_art = my_art, my_creation = my_creation)
  34. @dashboards.route('/create_art', methods=['GET', 'POST'])
  35. @login_required
  36. def create():
  37. # check POST req
  38. if request.method == "POST":
  39. new_art = request.files.get('art_img')
  40. art_name = request.form.get('art_name')
  41. art_desc = request.form.get('description')
  42. min_price = request.form.get('minimum_price')
  43. buyout_price = request.form.get('buyout_price')
  44. close_date = request.form.get('closing_date')
  45. if tools.check_fields([new_art, art_name, min_price, buyout_price, close_date]):
  46. if new_art and new_art.filename != '' and cf.allowed_file(new_art.filename):
  47. designated_fn = cf.sanitize(new_art.filename)
  48. new_art.save(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  49. dispatch.mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date)
  50. return render_template('create_art.html', user=current_user)
  51. # Pop Ups
  52. @dashboards.route('/modal_home')
  53. @login_required
  54. def modal_home():
  55. return render_template('detail_art_for_home.html', user=current_user)
  56. @dashboards.route('/modal_profile')
  57. @login_required
  58. def modal_profile():
  59. return render_template('detail_art_for_profile.html', user=current_user)