Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 268 additions & 1 deletion app/routes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
Routes principales pour DevPlan AI Generator
"""

from flask import Blueprint, render_template, current_app, jsonify
from flask import Blueprint, render_template, current_app, jsonify, request, flash
import os
import json
from ..services import OpenAIService, ValidationService, SchemaGenerator
from ..utils.exceptions import OpenAIException, ValidationException, DevPlanException

# Création du blueprint
main_bp = Blueprint('main', __name__)
Expand Down Expand Up @@ -101,6 +104,270 @@ def config_page():
title="Configuration",
description="Configurez votre instance DevPlan")

# ==================== ROUTES PR #2 - OpenAI Service ====================

@main_bp.route('/openai-test')
def openai_test():
"""Page de test OpenAI"""
return render_template('openai_test.html',
title="Test OpenAI",
description="Tester la connexion et configuration OpenAI")

@main_bp.route('/api/openai/test', methods=['POST'])
def test_openai_connection():
"""API pour tester la connexion OpenAI"""
try:
openai_service = OpenAIService()

# Test de connexion
result = openai_service.test_connection()

return jsonify({
'success': True,
'data': result,
'message': 'Connexion OpenAI réussie'
}), 200

except OpenAIException as e:
current_app.logger.error(f"Erreur OpenAI: {str(e)}")
return jsonify({
'success': False,
'error': str(e),
'type': 'openai_error'
}), 400

except Exception as e:
current_app.logger.error(f"Erreur test OpenAI: {str(e)}")
return jsonify({
'success': False,
'error': f"Erreur interne: {str(e)}",
'type': 'internal_error'
}), 500

@main_bp.route('/api/openai/generate-plan', methods=['POST'])
def generate_development_plan():
"""API pour générer un plan de développement avec OpenAI"""
try:
# Récupérer et valider les données
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'Aucune donnée fournie'
}), 400

# Validation des données
validation_service = ValidationService()
validation_result = validation_service.validate_project_data(data)

if not validation_result['is_valid']:
return jsonify({
'success': False,
'error': 'Données invalides',
'validation_errors': validation_result['errors']
}), 400

# Génération du plan avec OpenAI
openai_service = OpenAIService()
plan = openai_service.generate_development_plan(validation_result['cleaned_data'])

return jsonify({
'success': True,
'data': {
'content': plan,
'project_data': validation_result['cleaned_data']
},
'message': 'Plan de développement généré avec succès'
}), 200

except ValidationException as e:
return jsonify({
'success': False,
'error': str(e),
'type': 'validation_error'
}), 400

except OpenAIException as e:
current_app.logger.error(f"Erreur OpenAI: {str(e)}")
return jsonify({
'success': False,
'error': str(e),
'type': 'openai_error'
}), 500

except Exception as e:
current_app.logger.error(f"Erreur génération plan: {str(e)}")
return jsonify({
'success': False,
'error': f"Erreur interne: {str(e)}",
'type': 'internal_error'
}), 500

@main_bp.route('/api/openai/config', methods=['GET', 'POST'])
def openai_config():
"""API pour la configuration OpenAI"""
if request.method == 'GET':
# Retourner la configuration actuelle (sans les secrets)
return jsonify({
'success': True,
'data': {
'api_key_configured': bool(current_app.config.get('OPENAI_API_KEY')),
'organization_configured': bool(current_app.config.get('OPENAI_ORGANIZATION')),
'model': current_app.config.get('OPENAI_MODEL', 'gpt-3.5-turbo')
}
}), 200

elif request.method == 'POST':
# Mettre à jour la configuration (fonctionnalité future)
return jsonify({
'success': False,
'message': 'Configuration dynamique non implémentée'
}), 501

# ==================== ROUTES PR #3 - Schema Generator ====================

@main_bp.route('/api/schema/generate-detailed', methods=['POST'])
def generate_detailed_schema():
"""API pour générer un schéma technique détaillé"""
try:
# Récupérer et valider les données
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'Aucune donnée fournie'
}), 400

# Validation des données
validation_service = ValidationService()
validation_result = validation_service.validate_project_data(data)

if not validation_result['is_valid']:
return jsonify({
'success': False,
'error': 'Données invalides',
'validation_errors': validation_result['errors']
}), 400

# Génération du schéma détaillé
schema_generator = SchemaGenerator()
detailed_schema = schema_generator.generate_detailed_schema(validation_result['cleaned_data'])

# Conversion en dictionnaire pour JSON
schema_dict = schema_generator.export_schema_to_dict(detailed_schema)

return jsonify({
'success': True,
'data': {
'schema': schema_dict,
'project_data': validation_result['cleaned_data']
},
'message': 'Schéma technique généré avec succès'
}), 200

except ValidationException as e:
return jsonify({
'success': False,
'error': str(e),
'type': 'validation_error'
}), 400

except DevPlanException as e:
current_app.logger.error(f"Erreur génération schéma: {str(e)}")
return jsonify({
'success': False,
'error': str(e),
'type': 'schema_generation_error'
}), 500

except Exception as e:
current_app.logger.error(f"Erreur interne génération schéma: {str(e)}")
return jsonify({
'success': False,
'error': f"Erreur interne: {str(e)}",
'type': 'internal_error'
}), 500

@main_bp.route('/api/schema/analyze-complexity', methods=['POST'])
def analyze_project_complexity():
"""API pour analyser la complexité d'un projet"""
try:
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'Aucune donnée fournie'
}), 400

schema_generator = SchemaGenerator()
complexity = schema_generator.analyze_project_complexity(data)

return jsonify({
'success': True,
'data': {
'complexity': complexity.value,
'complexity_name': complexity.name
}
}), 200

except Exception as e:
current_app.logger.error(f"Erreur analyse complexité: {str(e)}")
return jsonify({
'success': False,
'error': f"Erreur: {str(e)}"
}), 500

@main_bp.route('/api/schema/tech-recommendations', methods=['POST'])
def get_tech_recommendations():
"""API pour obtenir des recommandations technologiques"""
try:
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'Aucune donnée fournie'
}), 400

schema_generator = SchemaGenerator()
complexity = schema_generator.analyze_project_complexity(data)
recommendations = schema_generator.generate_tech_recommendations(data, complexity)

# Conversion des recommandations en dictionnaire
recommendations_dict = []
for rec in recommendations:
recommendations_dict.append({
'name': rec.name,
'category': rec.category.value,
'version': rec.version,
'reason': rec.reason,
'alternatives': rec.alternatives,
'learning_curve': rec.learning_curve,
'popularity_score': rec.popularity_score,
'maintenance_cost': rec.maintenance_cost
})

return jsonify({
'success': True,
'data': {
'complexity': complexity.value,
'recommendations': recommendations_dict
}
}), 200

except Exception as e:
current_app.logger.error(f"Erreur recommandations tech: {str(e)}")
return jsonify({
'success': False,
'error': f"Erreur: {str(e)}"
}), 500

@main_bp.route('/schema-advanced')
def schema_advanced():
"""Page du générateur de schémas avancés (PR #3)"""
return render_template('schema_advanced.html',
title="Schémas Avancés",
description="Génération de schémas techniques détaillés avec architecture et estimations")

# Routes d'erreur personnalisées pour ce blueprint
@main_bp.app_errorhandler(404)
def not_found(error):
Expand Down
15 changes: 15 additions & 0 deletions app/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
"""
Module services pour DevPlan AI Generator.
Contient tous les services de l'application.
"""

from .openai_service import OpenAIService
from .validation_service import ValidationService
from .schema_generator import SchemaGenerator

__all__ = [
'OpenAIService',
'ValidationService',
'SchemaGenerator'
]
Loading