-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
192 lines (165 loc) · 5.84 KB
/
Copy pathroutes.py
File metadata and controls
192 lines (165 loc) · 5.84 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, session
from models import db, Deck, Card
from forms import DeckForm, CardForm
import random
bp = Blueprint('main', __name__)
# ============= DECK ROUTES =============
@bp.route('/')
def index():
decks = Deck.query.order_by(Deck.created_at.desc()).all()
total_cards = sum(deck.card_count for deck in decks)
return render_template('index.html', decks=decks, total_cards=total_cards)
@bp.route('/decks/create', methods=['POST'])
def create_deck():
form = DeckForm()
if form.validate_on_submit():
deck = Deck(
title=form.title.data,
description=form.description.data
)
db.session.add(deck)
db.session.commit()
flash('Deck created successfully!', 'success')
return redirect(url_for('main.deck_detail', id=deck.id))
else:
for field, errors in form.errors.items():
for error in errors:
flash(error, 'error')
return redirect(url_for('main.index'))
@bp.route('/decks/<int:id>')
def deck_detail(id):
deck = Deck.query.get_or_404(id)
form = CardForm()
return render_template('deck.html', deck=deck, form=form)
@bp.route('/decks/<int:id>/edit', methods=['POST'])
def edit_deck(id):
deck = Deck.query.get_or_404(id)
form = DeckForm()
if form.validate_on_submit():
deck.title = form.title.data
deck.description = form.description.data
db.session.commit()
flash('Deck updated successfully!', 'success')
else:
for field, errors in form.errors.items():
for error in errors:
flash(error, 'error')
return redirect(url_for('main.deck_detail', id=id))
@bp.route('/decks/<int:id>/delete', methods=['POST'])
def delete_deck(id):
deck = Deck.query.get_or_404(id)
db.session.delete(deck)
db.session.commit()
flash('Deck deleted successfully!', 'success')
return redirect(url_for('main.index'))
# ============= CARD ROUTES =============
@bp.route('/decks/<int:id>/cards/add', methods=['POST'])
def add_card(id):
deck = Deck.query.get_or_404(id)
form = CardForm()
if form.validate_on_submit():
card = Card(
deck_id=deck.id,
front=form.front.data,
back=form.back.data
)
db.session.add(card)
db.session.commit()
flash('Card added successfully!', 'success')
else:
for field, errors in form.errors.items():
for error in errors:
flash(error, 'error')
return redirect(url_for('main.deck_detail', id=id))
@bp.route('/cards/<int:id>/edit', methods=['POST'])
def edit_card(id):
card = Card.query.get_or_404(id)
form = CardForm()
if form.validate_on_submit():
card.front = form.front.data
card.back = form.back.data
db.session.commit()
flash('Card updated successfully!', 'success')
else:
for field, errors in form.errors.items():
for error in errors:
flash(error, 'error')
return redirect(url_for('main.deck_detail', id=card.deck_id))
@bp.route('/cards/<int:id>/delete', methods=['POST'])
def delete_card(id):
card = Card.query.get_or_404(id)
deck_id = card.deck_id
db.session.delete(card)
db.session.commit()
flash('Card deleted successfully!', 'success')
return redirect(url_for('main.deck_detail', id=deck_id))
# ============= STUDY/QUIZ ROUTES =============
@bp.route('/decks/<int:id>/study')
def study(id):
deck = Deck.query.get_or_404(id)
if not deck.cards:
flash('This deck has no cards yet. Add some cards first!', 'error')
return redirect(url_for('main.deck_detail', id=id))
# Shuffle cards for study session
cards = list(deck.cards)
random.shuffle(cards)
# Initialize session for tracking answers
session['study_session'] = {
'deck_id': deck.id,
'correct': 0,
'incorrect': 0,
'missed_cards': []
}
# Convert cards to JSON-serializable format
cards_json = [{
'id': card.id,
'front': card.front,
'back': card.back
} for card in cards]
return render_template('study.html', deck=deck, cards_json=cards_json)
@bp.route('/decks/<int:id>/study/answer', methods=['POST'])
def study_answer(id):
data = request.get_json()
card_id = data.get('card_id')
correct = data.get('correct', False)
card = Card.query.get_or_404(card_id)
# Update card statistics
card.times_seen += 1
if correct:
card.times_correct += 1
db.session.commit()
# Update session tracking
if 'study_session' in session:
study_data = session['study_session']
if correct:
study_data['correct'] += 1
else:
study_data['incorrect'] += 1
study_data['missed_cards'].append({
'front': card.front,
'back': card.back
})
session['study_session'] = study_data
return jsonify({
'success': True,
'times_seen': card.times_seen,
'times_correct': card.times_correct
})
@bp.route('/decks/<int:id>/results')
def results(id):
deck = Deck.query.get_or_404(id)
# Get session data
study_data = session.get('study_session', {
'correct': 0,
'incorrect': 0,
'missed_cards': []
})
total = study_data['correct'] + study_data['incorrect']
percent = int((study_data['correct'] / total * 100)) if total > 0 else 0
return render_template('results.html',
deck=deck,
correct=study_data['correct'],
incorrect=study_data['incorrect'],
total=total,
percent=percent,
missed_cards=study_data['missed_cards'])