function create_text_section
Flask API endpoint that creates a new text section with specified title, type, level, and content for an authenticated user.
/tf/active/vicechatdev/vice_ai/new_app.py
563 - 597
moderate
Purpose
This endpoint handles POST requests to create text sections in a document management system. It validates user authentication, parses request data, validates section type, and delegates creation to a text section service. It supports flexible section types (text, heading, etc.) and hierarchical levels for document organization.
Source Code
def create_text_section():
"""Create a new text section"""
user_email = get_current_user()
data = request.get_json()
try:
# Validate required fields
title = data.get('title', 'Untitled Section')
section_type_str = data.get('type', 'text')
level = int(data.get('level', 1))
content = data.get('content', '')
# Parse section type
try:
section_type = SectionType(section_type_str)
except ValueError:
section_type = SectionType.TEXT
# Create text section
text_section = text_section_service.create_text_section(
owner=user_email,
title=title,
section_type=section_type,
level=level,
initial_content=content
)
return jsonify({
'success': True,
'text_section': text_section.to_dict()
})
except Exception as e:
logger.error(f"Error creating text section: {e}")
return jsonify({'error': str(e)}), 400
Return Value
Returns a JSON response with status code. On success (200): {'success': True, 'text_section': <dict representation of created TextSection>}. On error (400): {'error': <error message string>}. The text_section dictionary contains all properties of the created section including id, title, type, level, content, timestamps, etc.
Dependencies
flaskloggingtyping
Required Imports
from flask import request, jsonify
from models import SectionType
import logging
Usage Example
# Example API call using requests library
import requests
# Assuming authentication is handled via session/headers
url = 'https://your-app.com/api/text_sections'
headers = {'Authorization': 'Bearer YOUR_TOKEN'}
payload = {
'title': 'Introduction',
'type': 'heading',
'level': 1,
'content': 'This is the introduction section'
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
result = response.json()
section_id = result['text_section']['id']
print(f'Created section with ID: {section_id}')
else:
print(f'Error: {response.json()["error"]}')
Best Practices
- Always include authentication headers when calling this endpoint
- Validate section type values on client side to match SectionType enum values before sending
- Handle both success and error responses appropriately
- The endpoint accepts both '/api/text_sections' and '/api/text-sections' routes (kebab-case and underscore)
- Level should be a positive integer representing document hierarchy (1 for top-level, 2 for subsection, etc.)
- Invalid section types will default to 'text' type rather than failing
- All fields are optional with sensible defaults, but providing meaningful values is recommended
- Error responses include descriptive messages for debugging
- The function logs errors for server-side debugging while returning user-friendly error messages
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_text_section_for_document 87.9% similar
-
function api_create_section 83.6% similar
-
function create_data_section 81.8% similar
-
function add_existing_section_to_document 80.2% similar
-
function update_text_section 79.0% similar