Skip to content

Instantly share code, notes, and snippets.

@Kd-Here

Kd-Here/views.py Secret

Created March 5, 2023 15:53
Show Gist options
  • Save Kd-Here/301e11101e3e853be482db4d00c233dc to your computer and use it in GitHub Desktop.
Save Kd-Here/301e11101e3e853be482db4d00c233dc to your computer and use it in GitHub Desktop.
from flask import Blueprint,render_template,request,flash,jsonify
from flask_login import login_required,current_user
import json
from .models import Note
from . import db
views = Blueprint('views',__name__)
@views.route('/',methods=['GET',"POST"])
@login_required #This decorator means you can't enter in homepage unless you are login
def home():
if request.method == "POST":
note = request.form.get('note')
if len(note) < 1:
flash('Note too short',category='error')
else:
new_node = Note(userNote=note,user_id=current_user.id)
db.session.add(new_node)
db.session.commit()
flash("Note added!",category='success')
return render_template('home.html',user=current_user)
@views.route('/delete-note',methods=["POST"])
def delete_note():
note = json.loads(request.data)
noteId = note['noteId']
note = Note.query.get(noteId)
if note:
if note.user_id == current_user.id:
# To verify only current signin user is deleting his notes not other ones
db.session.delete(note)
db.session.commit()
return jsonify({})
"""
when user delete a note it's done by post method so
request.data is for reteriving the data which is deleted & we are loading
only that data with help of json.loads(request.data)
noteId = note['noteId']
Here we are storing the noteId that was passed with fetch in js file
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment