function revert_text_section
Flask API endpoint that reverts a text section to a specific previous version, verifying user ownership before performing the revert operation.
/tf/active/vicechatdev/vice_ai/new_app.py
729 - 753
moderate
Purpose
This endpoint allows authenticated users to restore a text section to a previously saved version. It validates that the requesting user owns the text section, performs the revert operation through the text section service, and returns the updated text section data. This is useful for undoing unwanted changes or recovering previous content states in a document versioning system.
Source Code
def revert_text_section(section_id, version_id):
"""Revert text section to a specific version"""
user_email = get_current_user()
# Verify ownership
text_section = text_section_service.get_text_section(section_id)
if not text_section or text_section.owner != user_email:
return jsonify({'error': 'Text section not found or access denied'}), 404
try:
success = text_section_service.revert_to_version(section_id, version_id, user_email)
if success:
# Get updated text section
updated_section = text_section_service.get_text_section(section_id)
return jsonify({
'success': True,
'text_section': updated_section.to_dict()
})
else:
return jsonify({'error': 'Failed to revert to version'}), 500
except Exception as e:
logger.error(f"Error reverting text section: {e}")
return jsonify({'error': str(e)}), 400
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
section_id |
- | - | positional_or_keyword |
version_id |
- | - | positional_or_keyword |
Parameter Details
section_id: Unique identifier (string) of the text section to be reverted. This is extracted from the URL path and used to locate the specific text section in the database.
version_id: Unique identifier (string) of the version to revert to. This is extracted from the URL path and specifies which historical version should be restored as the current version.
Return Value
Returns a Flask JSON response tuple. On success (200): {'success': True, 'text_section': <dict with updated section data>}. On not found/access denied (404): {'error': 'Text section not found or access denied'}. On revert failure (500): {'error': 'Failed to revert to version'}. On exception (400): {'error': <error message string>}.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Usage Example
# Example API call using requests library
import requests
# Assuming user is authenticated with session/token
session = requests.Session()
# Login first to establish authentication
# session.post('https://api.example.com/login', json={'credentials': '...'})
section_id = 'abc123-section-id'
version_id = 'xyz789-version-id'
response = session.post(
f'https://api.example.com/api/text-sections/{section_id}/revert/{version_id}'
)
if response.status_code == 200:
data = response.json()
if data['success']:
updated_section = data['text_section']
print(f"Reverted to version {version_id}")
print(f"Current content: {updated_section['content']}")
else:
error = response.json().get('error')
print(f"Error: {error}")
Best Practices
- Always verify user ownership before allowing revert operations to prevent unauthorized access
- Use try-except blocks to handle service-level exceptions gracefully
- Log errors with sufficient context for debugging (section_id, version_id, user_email)
- Return appropriate HTTP status codes (404 for not found, 500 for server errors, 400 for bad requests)
- Fetch and return the updated text section after successful revert to provide immediate feedback
- Ensure the text_section_service.revert_to_version method is transactional to prevent partial updates
- Consider implementing rate limiting to prevent abuse of version revert operations
- Validate that version_id exists and belongs to the specified section_id before attempting revert
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_text_section_versions 80.9% similar
-
function revert_document_to_version 80.6% similar
-
function delete_text_section 77.6% similar
-
function update_text_section 75.7% similar
-
function get_text_section 74.6% similar