Dify-Learning / routes.py
AtZa
Upload 16 files
7cb4836 verified
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'))