-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
135 lines (121 loc) · 3.53 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# views.py
'''
Main py component with all functions in it to view main page, add, update and delete records.
Login and logout functionalities
'''
# System Module Imports
from flask import Flask, render_template, request, session, \
flash, redirect, url_for, g, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError
from functools import wraps
import datetime
# Custom Module imports
from models import Notes, db
from forms import AddNoteForm, RegisterForm, LoginForm
app = Flask(__name__)
app.config.from_object('_config')
db.init_app(app)
db.app = app
# login required function
def login_required(test):
@wraps(test)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return test(*args, **kwargs)
else:
flash('You need to login first')
return redirect(url_for('login'))
return wrap
# login function
@app.route("/", methods=['GET', 'POST'])
def login():
error = None
status_code = 202
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME'] or \
request.form['password'] != app.config['PASSWORD']:
error = 'Invalid credentials, please try again'
status_code = 401
else:
session['logged_in'] = True
session.pop('_flashes', None)
return redirect(url_for('main'))
return render_template('login.html', error=error), status_code
# logout function
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You are logged out')
return redirect(url_for('login'))
# main landing page
@app.route("/main", methods=['GET', 'POST'])
@login_required
def main():
user = 1
callfrom = request.args.get('callfrom')
if callfrom == 'select':
note_id = request.args.get('note_id')
notes = db.session.query(Notes) \
.filter_by(user_id=user, note_id=note_id).order_by(Notes.note_id.desc())
for p in notes:
title=p.title
detail=p.detail
break
return jsonify({'title':title, 'detail':detail, note_id:note_id})
elif callfrom == 'new':
title = ''
detail = ''
note_id = None
return jsonify({'title':title, 'detail':detail, 'note_id':note_id})
else:
notes = db.session.query(Notes) \
.filter_by(user_id=user).order_by(Notes.note_id.desc())
for p in notes:
title = p.title
detail = p.detail
note_id = p.note_id
break
return render_template('main.html',note_id=note_id, notes=notes, title=title, detail=detail)
# Item Update
@app.route("/update", methods=['GET', 'POST'])
@login_required
def update():
# user = session['username']
user = 1
title = request.form['title']
detail = request.form['detail']
note_id = request.form['note_id']
db.session.query(Notes).filter_by(note_id=note_id).update({"title": title, "detail": detail})
db.session.commit()
return jsonify({'var': 'test'})
# Item Select
@app.route("/select", methods=['GET'])
@login_required
def select():
note_id = request.args.get('note_id')
return redirect(url_for('main', note_id=note_id, callfrom="select"))
# Item Add
@app.route('/add',methods=['POST'])
@login_required
def add():
form = AddNoteForm(request.form)
if request.method == 'POST':
# if form.validate_on_submit():
new_task = Notes(form.title.data,
form.detail.data,1,
"10/12/2016",
# datetime.datetime.utcnow(),
"1")
db.session.add(new_task)
db.session.commit()
return jsonify({'var1': "Entry added"})
return redirect(url_for('main'))
# Item Delete
@app.route('/delete',methods=['POST'])
@login_required
def delete():
note_id = request.form['note_id']
db.session.query(Notes).filter_by(note_id=note_id).delete()
db.session.commit()
return jsonify({ 'var1': "Entry deleted"})