File size: 4,027 Bytes
7cb4836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import render_template, request, jsonify, session, redirect, url_for
from app import app, db
from models import User, Tutorial, Progress, Project
from content.tutorials import get_tutorial_content, get_all_tutorials
from content.projects import get_project_content, get_all_projects
import json
import markdown

@app.route('/')
def index():
    tutorials = get_all_tutorials()
    projects = get_all_projects()
    
    # Get user progress if logged in
    user_progress = {}
    if 'user_id' in session:
        user_id = session['user_id']
        progress_records = Progress.query.filter_by(user_id=user_id).all()
        user_progress = {p.tutorial_id: p for p in progress_records}
    
    return render_template('index.html', 
                         tutorials=tutorials, 
                         projects=projects,
                         user_progress=user_progress)

@app.route('/tutorial/<slug>')
def tutorial(slug):
    tutorial_data = get_tutorial_content(slug)
    if not tutorial_data:
        return "Tutorial not found", 404
    
    # Get or create progress for this tutorial
    user_progress = None
    if 'user_id' in session:
        user_id = session['user_id']
        tutorial_id = tutorial_data.get('id', 0)
        user_progress = Progress.query.filter_by(
            user_id=user_id, 
            tutorial_id=tutorial_id
        ).first()
        
        if not user_progress:
            user_progress = Progress(
                user_id=user_id,
                tutorial_id=tutorial_id,
                current_step=0
            )
            db.session.add(user_progress)
            db.session.commit()
    
    return render_template('tutorial.html', 
                         tutorial=tutorial_data,
                         user_progress=user_progress)

@app.route('/project/<slug>')
def project(slug):
    project_data = get_project_content(slug)
    if not project_data:
        return "Project not found", 404
    
    # Convert markdown content to HTML for proper display
    if project_data.get('sections'):
        for section in project_data['sections']:
            if section.get('steps'):
                for step in section['steps']:
                    if step.get('content'):
                        # Convert markdown to HTML with proper formatting
                        step['content'] = markdown.markdown(step['content'], extensions=['extra', 'nl2br'])
    
    return render_template('project.html', project=project_data)

@app.route('/progress')
def progress():
    if 'user_id' not in session:
        return redirect(url_for('login'))
    
    user_id = session['user_id']
    user = User.query.get(user_id)
    progress_records = Progress.query.filter_by(user_id=user_id).all()
    
    tutorials = get_all_tutorials()
    progress_data = []
    
    for tutorial in tutorials:
        progress_record = next((p for p in progress_records if p.tutorial_id == tutorial['id']), None)
        progress_data.append({
            'tutorial': tutorial,
            'progress': progress_record
        })
    
    return render_template('progress.html', 
                         user=user,
                         progress_data=progress_data)

@app.route('/api/progress/update', methods=['POST'])
def update_progress():
    # Progress tracking disabled - return success without saving
    return jsonify({'success': True})

@app.route('/login')
def login():
    # Simple session-based login for demo
    if 'user_id' not in session:
        # Create or get demo user
        demo_user = User.query.filter_by(username='demo_user').first()
        if not demo_user:
            demo_user = User(
                username='demo_user',
                email='demo@example.com'
            )
            db.session.add(demo_user)
            db.session.commit()
        
        session['user_id'] = demo_user.id
    
    return redirect(url_for('index'))

@app.route('/logout')
def logout():
    session.pop('user_id', None)
    return redirect(url_for('index'))