function get_user_uploaded_documents
Retrieves all uploaded documents associated with a specific user from a thread-safe global application state dictionary.
/tf/active/vicechatdev/vice_ai/complex_app.py
195 - 198
simple
Purpose
This function provides thread-safe access to user-specific uploaded documents stored in the application state. It uses a lock mechanism to prevent race conditions when multiple threads access the uploaded documents dictionary simultaneously. The function is designed for multi-user document management systems where each user's uploaded documents need to be tracked and retrieved independently.
Source Code
def get_user_uploaded_documents(user_email):
"""Get all uploaded documents for a user"""
with app_state['locks']['uploaded_documents']:
return app_state['uploaded_documents'].get(user_email, {})
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
Parameter Details
user_email: String representing the user's email address, used as a unique identifier to look up the user's uploaded documents in the application state dictionary. Expected to be a valid email string that matches the key format used when storing documents.
Return Value
Returns a dictionary containing all uploaded documents for the specified user. If the user has no uploaded documents or the user_email is not found in the system, returns an empty dictionary {}. The structure of the returned dictionary depends on how documents are stored in app_state['uploaded_documents'], but typically contains document metadata such as filenames, upload timestamps, file paths, or document IDs.
Dependencies
threading
Required Imports
from threading import Lock
Usage Example
from threading import Lock
# Initialize app_state (typically done at application startup)
app_state = {
'locks': {
'uploaded_documents': Lock()
},
'uploaded_documents': {
'user@example.com': {
'doc1.pdf': {'path': '/uploads/doc1.pdf', 'timestamp': '2024-01-15'},
'doc2.docx': {'path': '/uploads/doc2.docx', 'timestamp': '2024-01-16'}
}
}
}
# Retrieve user's uploaded documents
user_docs = get_user_uploaded_documents('user@example.com')
print(user_docs)
# Output: {'doc1.pdf': {'path': '/uploads/doc1.pdf', 'timestamp': '2024-01-15'}, 'doc2.docx': {'path': '/uploads/doc2.docx', 'timestamp': '2024-01-16'}}
# For a user with no documents
empty_docs = get_user_uploaded_documents('newuser@example.com')
print(empty_docs)
# Output: {}
Best Practices
- Ensure app_state is properly initialized before calling this function to avoid KeyError exceptions
- The function assumes app_state is a global variable accessible in the function's scope
- Always use this function instead of directly accessing app_state['uploaded_documents'] to maintain thread safety
- The lock is automatically released after the with block, even if an exception occurs
- Consider validating the user_email parameter format before calling this function to ensure data consistency
- The returned dictionary is a reference to the actual data structure; modifications to it will affect the global state
- For read-only operations, consider returning a copy of the dictionary to prevent unintended modifications
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_uploaded_document_v1 87.8% similar
-
function get_user_documents 84.5% similar
-
function get_uploaded_document 84.3% similar
-
function remove_uploaded_document 79.7% similar
-
function store_uploaded_document_v1 79.0% similar