function revert_document_to_version
Flask API endpoint that reverts a document to a specific previous version after verifying user ownership and authentication.
/tf/active/vicechatdev/vice_ai/new_app.py
1198 - 1224
moderate
Purpose
This endpoint allows authenticated users to restore a document to a previous version from its version history. It validates that the requesting user owns the document before performing the revert operation, ensuring data security and proper access control. The function handles errors gracefully and returns appropriate HTTP status codes and JSON responses.
Source Code
def revert_document_to_version(document_id, version_id):
"""Revert a document to a specific version"""
user_email = get_current_user()
# Verify document ownership
document = document_service.get_document(document_id)
if not document or document.owner != user_email:
return jsonify({'error': 'Document not found or access denied'}), 404
try:
success = document_service.revert_document_to_version(
document_id,
version_id,
user_email
)
if success:
return jsonify({
'success': True,
'message': 'Document reverted successfully'
})
else:
return jsonify({'error': 'Failed to revert document'}), 500
except Exception as e:
logger.error(f"Error reverting document: {e}")
return jsonify({'error': str(e)}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
document_id |
- | - | positional_or_keyword |
version_id |
- | - | positional_or_keyword |
Parameter Details
document_id: Unique identifier (likely UUID string) of the document to be reverted. This is extracted from the URL path parameter and used to locate the document in the system.
version_id: Unique identifier (likely UUID string) of the specific version to revert to. This is extracted from the URL path parameter and references a historical version of the document stored in the version history.
Return Value
Returns a Flask JSON response tuple. On success (200): {'success': True, 'message': 'Document reverted successfully'}. On document not found or access denied (404): {'error': 'Document not found or access denied'}. On revert failure (500): {'error': 'Failed to revert document'}. On exception (500): {'error': '<error message>'}. Each return includes an HTTP status code as the second element of the tuple.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Usage Example
# Example API call using requests library
import requests
# Assuming the Flask app is running on localhost:5000
url = 'http://localhost:5000/api/documents/abc123-document-id/revert/xyz789-version-id'
headers = {
'Authorization': 'Bearer <auth_token>',
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers)
if response.status_code == 200:
result = response.json()
print(f"Success: {result['message']}")
elif response.status_code == 404:
print(f"Error: {response.json()['error']}")
else:
print(f"Failed: {response.json()['error']}")
Best Practices
- Always verify document ownership before allowing revert operations to prevent unauthorized access
- Use try-except blocks to handle potential errors during the revert operation
- Log errors with sufficient detail for debugging while avoiding sensitive information exposure
- Return appropriate HTTP status codes (404 for not found, 500 for server errors)
- Ensure the require_auth decorator is properly implemented to prevent unauthenticated access
- Consider implementing additional validation for version_id to ensure it belongs to the specified document
- The function relies on document_service.revert_document_to_version() - ensure this service method creates a new version entry when reverting
- Consider adding rate limiting to prevent abuse of the revert functionality
- Implement audit logging to track who reverted documents and when for compliance purposes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function revert_text_section 80.6% similar
-
function get_document_v3 79.8% similar
-
function create_document_v7 75.1% similar
-
function delete_document_v1 72.4% similar
-
function api_delete_document 69.6% similar