-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
39 lines (32 loc) · 1.08 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from flask import Blueprint, render_template, request, flash, jsonify
from flask_login import login_required, current_user
from .models import Notes
from . import db
import json
views = Blueprint('views', __name__)
@views.route('/', methods=['GET', 'POST'])
@login_required
def index():
if request.method == 'POST':
note = request.form['note']
if len(note) < 1:
flash("The note length can't less then 1 character", category='error')
else:
new_note = Notes(
note = note,
usr_id = current_user.user_id
)
db.session.add(new_note)
db.session.commit()
flash('Note created', category='success')
return render_template('index.html', user=current_user)
@views.route('/delete-note', methods=['POST'])
def delete_note():
note = json.loads(request.data)
note_id = note['noteid']
note = Notes.query.get(int(note_id))
if note:
if note.usr_id == current_user.user_id:
db.session.delete(note)
db.session.commit()
return jsonify({})