🔍 Code Extractor

function get_user_uploaded_documents

Maturity: 34

Retrieves all uploaded documents associated with a specific user from a thread-safe global application state dictionary.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
195 - 198
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_uploaded_document_v1 87.8% similar

    Retrieves a specific uploaded document for a given user from a thread-safe global application state dictionary.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_user_documents 84.5% similar

    Thread-safe function that retrieves all documents associated with a specific user email from a global document storage dictionary.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function get_uploaded_document 84.3% similar

    Retrieves a specific uploaded document from the application state for a given user and document ID, returning document metadata and content in a thread-safe manner.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function remove_uploaded_document 79.7% similar

    Removes a specific uploaded document from the application state for a given user, with thread-safe locking and automatic cleanup of empty user entries.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function store_uploaded_document_v1 79.0% similar

    Stores an uploaded document in a thread-safe global application state dictionary, organizing documents by user email and document ID with metadata including name, content, file type, and upload timestamp.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse