function index
Flask route handler that serves as the main landing page for authenticated users, displaying their documents and text sections in a workspace interface.
/tf/active/vicechatdev/vice_ai/new_app.py
480 - 496
simple
Purpose
This function acts as the primary entry point for the application's main workspace. It enforces authentication by redirecting unauthenticated users to the login page, retrieves the current user's documents and text sections from the database, and renders the workspace template with all necessary user data and configuration flags.
Source Code
def index():
"""Main index page"""
if not is_authenticated():
return redirect(url_for('login'))
user_email = get_current_user()
# Get user's documents and text sections
documents = document_service.get_user_documents(user_email)
text_sections = text_section_service.get_user_text_sections(user_email)
return render_template('new_workspace.html',
user_email=user_email,
user_name=get_user_name(),
documents=documents,
text_sections=text_sections,
rag_available=RAG_AVAILABLE)
Return Value
Returns a Flask Response object. If the user is not authenticated, returns a redirect response to the login page. If authenticated, returns a rendered HTML template ('new_workspace.html') populated with the user's email, name, documents, text sections, and RAG availability status.
Dependencies
flaskmodelsservices
Required Imports
from flask import render_template
from flask import redirect
from flask import url_for
Usage Example
# This function is used as a Flask route handler
# Typically accessed via browser navigation to the root URL
# In your Flask app setup:
from flask import Flask
app = Flask(__name__)
# Assuming authentication and service dependencies are configured:
# - is_authenticated() checks session/token
# - get_current_user() returns user email from session
# - get_user_name() returns user display name
# - document_service and text_section_service are initialized
# - RAG_AVAILABLE is set based on configuration
@app.route('/')
def index():
if not is_authenticated():
return redirect(url_for('login'))
user_email = get_current_user()
documents = document_service.get_user_documents(user_email)
text_sections = text_section_service.get_user_text_sections(user_email)
return render_template('new_workspace.html',
user_email=user_email,
user_name=get_user_name(),
documents=documents,
text_sections=text_sections,
rag_available=RAG_AVAILABLE)
# User accesses: http://localhost:5000/
# If authenticated: sees workspace with their documents
# If not authenticated: redirected to login page
Best Practices
- Always check authentication before rendering protected pages to prevent unauthorized access
- Ensure is_authenticated(), get_current_user(), and get_user_name() functions are properly implemented and secured
- Handle potential exceptions from document_service and text_section_service methods (e.g., database connection errors)
- Consider adding error handling for cases where user data cannot be retrieved
- Ensure the 'new_workspace.html' template properly handles empty documents or text_sections lists
- Use Flask's session management securely with proper secret key configuration
- Consider implementing CSRF protection for forms in the workspace template
- Cache user data appropriately to reduce database queries on frequent page loads
- Log authentication failures and redirects for security monitoring
- Ensure RAG_AVAILABLE is properly configured based on system capabilities
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function index_v4 88.1% similar
-
function document_workspace 87.6% similar
-
function workspace 86.7% similar
-
function index_v1 85.8% similar
-
function login_v3 81.8% similar