-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
104 lines (76 loc) · 3.16 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
from re import S
from flask import Blueprint, render_template, request, flash
from flask_login import login_user, logout_user, current_user
from flask_login.utils import login_required
from werkzeug.utils import redirect
from .models import User, Deck, Card
from . import db
from werkzeug.security import generate_password_hash, check_password_hash
import requests
from datetime import datetime
views = Blueprint("views", __name__)
BASE = 'http://127.0.0.1:5000'
@views.route('/dashboard', methods = ['GET', 'POST'])
@login_required
def home():
data = requests.get(BASE+f'/api/deck/{current_user.username}')
return render_template('dashboard.html', decks=data.json(), user=current_user.username)
@views.route('/', methods=['GET'])
def landing():
return render_template('landing.html')
@views.route('/review/<string:deck>', methods = ['GET', 'POST'])
@login_required
def review(deck):
t = datetime.now()
d=Deck.query.filter_by(deck_name=deck).first()
d.last_rev=t
db.session.commit()
data=requests.get(BASE+ f'/api/{current_user.username}/{deck}/card')
if data:
return render_template('review.html', front = data.json()['front'], back = data.json()['back'], deck=data.json()['deck'], card_id=data.json()['card_id'])
else:
return render_template('review.html', deck=deck, data=True)
@views.route('/review/<string:deck>/<int:card_id>', methods = ['GET', 'POST'])
def score(deck,card_id):
if request.method == 'POST':
c = Card.query.filter_by(card_id=card_id).first()
s = int(request.form.get('score'))
c.score = s
db.session.commit()
d= Deck.query.filter_by(deck_name=deck, user=current_user.username).first()
cn = Card.query.filter_by(card_id=card_id).count()
d.score = (d.score + c.score)/cn
db.session.commit()
return redirect(f'/review/{deck}')
@views.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get("username")
password = request.form.get("password")
user = User.query.filter_by(username=username).first()
if user:
if check_password_hash(user.password, password):
login_user(user, remember=True)
return redirect('/dashboard')
else:
flash('Password is incorrect.', category='error')
else:
flash('Username does not exist.', category='error')
return render_template('login.html')
@views.route('/create', methods = ['GET', 'POST'])
def register():
return render_template('create.html')
@views.route('/<string:deck>/addcard', methods=['GET', 'POST'])
def addcard(deck):
return render_template('addcard.html')
@views.route('/<string:user>/deck/<string:deck>/delete', methods=['GET','POST'])
def deletedeck(deck, user):
d= Deck.query.filter_by(deck_name=deck, user=user).first()
db.session.delete(d)
db.session.commit()
return redirect('/dashboard')
@views.route('/logout')
@login_required
def logout():
logout_user()
return redirect('/')