function get_uploaded_document_v1
Retrieves a specific uploaded document for a given user from a thread-safe global application state dictionary.
/tf/active/vicechatdev/vice_ai/complex_app.py
548 - 552
simple
Purpose
This function provides thread-safe access to user-uploaded documents stored in the application state. It uses a lock mechanism to prevent race conditions when multiple threads access the uploaded documents dictionary. The function looks up documents by user email and document ID, returning None if either the user or document doesn't exist.
Source Code
def get_uploaded_document(user_email, document_id):
"""Get a specific uploaded document"""
with app_state['locks']['uploaded_documents']:
user_docs = app_state['uploaded_documents'].get(user_email, {})
return user_docs.get(document_id)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
document_id |
- | - | positional_or_keyword |
Parameter Details
user_email: String representing the user's email address, used as the primary key to identify the user's document collection in the app_state dictionary. Expected to be a valid email string.
document_id: String or UUID representing the unique identifier of the document to retrieve. This is used as the secondary key within the user's document collection.
Return Value
Returns the document object associated with the given user_email and document_id if found, otherwise returns None. The exact structure of the document object depends on how documents are stored in the application state, but typically includes metadata like filename, upload timestamp, file path, and document content or reference.
Dependencies
threading
Required Imports
from threading import Lock
Usage Example
import threading
# Initialize app_state (typically done at application startup)
app_state = {
'locks': {
'uploaded_documents': threading.Lock()
},
'uploaded_documents': {
'user@example.com': {
'doc123': {
'filename': 'report.pdf',
'upload_time': '2024-01-15T10:30:00',
'file_path': '/uploads/doc123.pdf'
}
}
}
}
# Retrieve a document
document = get_uploaded_document('user@example.com', 'doc123')
if document:
print(f"Found document: {document['filename']}")
else:
print("Document not found")
# Attempt to retrieve non-existent document
missing_doc = get_uploaded_document('user@example.com', 'doc999')
print(missing_doc) # Output: None
Best Practices
- Ensure app_state is properly initialized before calling this function
- The function uses a context manager (with statement) for automatic lock release, preventing deadlocks
- Returns None for missing users or documents, so always check the return value before using it
- This function is read-only and thread-safe, suitable for concurrent access scenarios
- Consider implementing error handling or logging if document retrieval failures need to be tracked
- The nested .get() calls ensure no KeyError is raised for missing keys
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_uploaded_document 95.1% similar
-
function get_user_uploaded_documents 87.8% similar
-
function get_user_documents 82.7% similar
-
function store_uploaded_document 79.7% similar
-
function store_uploaded_document_v1 78.5% similar