function api_update_section
REST API endpoint that updates an existing section within a document, allowing modification of title, content, type, and level properties with authentication and authorization checks.
/tf/active/vicechatdev/vice_ai/complex_app.py
884 - 919
moderate
Purpose
This Flask route handler provides a secure API endpoint for updating document sections. It validates user authentication, verifies document ownership, checks section existence, and updates specified section properties (title, content, type, level) before persisting changes. It's designed for document management systems where users need to edit sections of their documents through a RESTful API.
Source Code
def api_update_section(doc_id, section_id):
"""Update a section"""
try:
document = get_document(doc_id)
if not document:
return jsonify({'error': 'Document not found'}), 404
if document.author != get_user_id():
return jsonify({'error': 'Access denied'}), 403
section = document.get_section(section_id)
if not section:
return jsonify({'error': 'Section not found'}), 404
data = request.get_json()
# Update section properties
if 'title' in data:
section.title = data['title']
if 'content' in data:
section.content = data['content']
if 'type' in data:
section.type = data['type']
if 'level' in data:
section.level = data['level']
section.updated_at = datetime.now()
save_document(document)
return jsonify({
'section': section.to_dict(),
'message': 'Section updated successfully'
})
except Exception as e:
logger.error(f"Update section error: {e}")
return jsonify({'error': 'Failed to update section'}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
doc_id |
- | - | positional_or_keyword |
section_id |
- | - | positional_or_keyword |
Parameter Details
doc_id: String identifier for the document containing the section to update. Extracted from the URL path. Used to retrieve the document from storage.
section_id: String identifier for the specific section within the document to update. Extracted from the URL path. Used to locate the section within the document structure.
Return Value
Returns a JSON response tuple. On success (200): {'section': dict, 'message': str} containing the updated section data and success message. On document not found (404): {'error': 'Document not found'}. On access denied (403): {'error': 'Access denied'}. On section not found (404): {'error': 'Section not found'}. On server error (500): {'error': 'Failed to update section'}. The section dict includes all section properties via to_dict() method.
Dependencies
flaskloggingdatetime
Required Imports
from flask import jsonify
from flask import request
from datetime import datetime
Usage Example
# Client-side usage example (using requests library)
import requests
# Assuming authentication token is already obtained
headers = {'Authorization': 'Bearer YOUR_TOKEN', 'Content-Type': 'application/json'}
# Update section data
update_data = {
'title': 'Updated Section Title',
'content': 'New section content with updated information',
'type': 'paragraph',
'level': 2
}
# Make PUT request to update section
response = requests.put(
'http://your-api.com/api/documents/doc123/sections/section456',
json=update_data,
headers=headers
)
if response.status_code == 200:
result = response.json()
print(f"Section updated: {result['section']['title']}")
print(f"Message: {result['message']}")
else:
print(f"Error: {response.json()['error']}")
Best Practices
- Always include authentication token in request headers when calling this endpoint
- Only the document author can update sections - ensure proper user authentication
- Provide only the fields you want to update in the request body; omitted fields remain unchanged
- Handle all possible HTTP status codes (200, 403, 404, 500) in client code
- The updated_at timestamp is automatically set server-side; don't include it in request data
- Validate input data on client-side before sending to reduce unnecessary API calls
- Use appropriate error handling for network failures and server errors
- The section object must have a to_dict() method for serialization
- Ensure document and section IDs are valid before making the request to avoid 404 errors
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_create_section 84.7% similar
-
function update_text_section 83.0% similar
-
function api_replace_section_content 81.7% similar
-
function api_delete_section 81.5% similar
-
function update_data_section_content 81.4% similar