🔍 Code Extractor

function remove_uploaded_document_v1

Maturity: 39

Removes a specific uploaded document from a user's document collection in the application state, with thread-safe locking and automatic cleanup of empty user entries.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
554 - 562
Complexity:
simple

Purpose

This function provides thread-safe deletion of uploaded documents from a shared application state dictionary. It removes a document identified by document_id from a specific user's collection (identified by user_email). The function also performs housekeeping by removing the user's entry entirely if they have no remaining documents after deletion. This is typically used in document management systems where users can upload and later remove their documents.

Source Code

def remove_uploaded_document(user_email, document_id):
    """Remove an uploaded document"""
    with app_state['locks']['uploaded_documents']:
        if user_email in app_state['uploaded_documents']:
            if document_id in app_state['uploaded_documents'][user_email]:
                del app_state['uploaded_documents'][user_email][document_id]
                # Clean up empty user entries
                if not app_state['uploaded_documents'][user_email]:
                    del app_state['uploaded_documents'][user_email]

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 that exists as a key in app_state['uploaded_documents'].

document_id: String or unique identifier representing the specific document to be removed from the user's collection. This should correspond to an existing document key within the user's document dictionary.

Return Value

This function returns None (no explicit return statement). It performs side effects by modifying the app_state['uploaded_documents'] dictionary in place. Success is implicit - if the user_email and document_id exist, they are removed; if they don't exist, the function silently does nothing.

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': {
            'doc123': {'filename': 'report.pdf', 'path': '/uploads/report.pdf'},
            'doc456': {'filename': 'data.csv', 'path': '/uploads/data.csv'}
        }
    }
}

# Remove a specific document
remove_uploaded_document('user@example.com', 'doc123')

# After removal, app_state['uploaded_documents']['user@example.com'] will only contain 'doc456'

# Remove the last document for a user
remove_uploaded_document('user@example.com', 'doc456')

# After this, 'user@example.com' key is automatically removed from app_state['uploaded_documents']

Best Practices

  • Always ensure app_state is properly initialized before calling this function
  • The function uses a lock for thread safety - ensure the lock object exists in app_state['locks']['uploaded_documents']
  • This function silently succeeds even if the user_email or document_id doesn't exist - consider adding validation or logging if you need to track failed deletions
  • The function automatically cleans up empty user entries to prevent memory bloat
  • Consider implementing additional cleanup logic (like deleting actual files from disk) before calling this function if documents are stored on the filesystem
  • In a production environment, consider adding error handling and logging to track document removal operations
  • If using this in a web application, ensure proper authentication and authorization checks are performed before allowing document deletion

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function remove_uploaded_document 94.8% 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 clear_user_uploaded_documents 85.4% similar

    Removes all uploaded documents associated with a specific user from the application state in a thread-safe manner.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function remove_user_document 82.9% similar

    Removes a specific document for a user by deleting the file from the filesystem and removing its metadata from the in-memory storage structure.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function get_uploaded_document_v1 76.4% 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_uploaded_documents 75.2% similar

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

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