Skip to content

Commit

Permalink
Fixed: issue with remote_addr metadata kwarg (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
signebedi committed Mar 23, 2024
1 parent 587de56 commit 35d4a69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
29 changes: 17 additions & 12 deletions libreforms_fastapi/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,25 +370,26 @@ async def api_form_create(form_name: str, background_tasks: BackgroundTasks, req
# Ugh, I'd like to find a more efficient way to get the user data. But alas, that
# the sqlalchemy-signing table is not optimized alongside the user model...
user = session.query(User).filter_by(api_key=key).first()

# Get request details

# Set the document_id here, and pass to the DocumentDatabase
document_id = str(ObjectId())

metadata={
DocumentDatabase.document_id_field: document_id,
DocumentDatabase.created_by_field: user.username,
DocumentDatabase.last_editor_field: user.username,
}

# Add the remote addr host if enabled
if config.COLLECT_USAGE_STATISTICS:
endpoint = request.url.path
remote_addr = request.client.host
metadata[DocumentDatabase.ip_address_field] = request.client.host

# Process the validated form submission as needed
# document_id = DocumentDatabase.create_document(form_name=form_name, json_data=form_data.model_dump_json())
document_id = str(ObjectId())
background_tasks.add_task(
DocumentDatabase.create_document,
form_name=form_name,
json_data=json_data,
metadata={
DocumentDatabase.document_id_field: document_id,
DocumentDatabase.created_by_field: user.username,
DocumentDatabase.last_editor_field: user.username,
# DocumentDatabase.ip_address_field: remote_addr,
},
metadata=metadata
)

# Send email
Expand All @@ -403,6 +404,10 @@ async def api_form_create(form_name: str, background_tasks: BackgroundTasks, req

# Write this query to the TransactionLog
if config.COLLECT_USAGE_STATISTICS:

endpoint = request.url.path
remote_addr = request.client.host

background_tasks.add_task(
write_api_call_to_transaction_log,
api_key=key,
Expand Down
2 changes: 1 addition & 1 deletion libreforms_fastapi/utils/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def get_form_names(config_path=config.FORM_CONFIG_PATH):
return form_config.keys()

def get_form_config(form_name, config_path=config.FORM_CONFIG_PATH):

"""Yields a single config dict for the form name passed"""
# Try to open config_path and if not existent or empty, use example config
form_config = example_form_config # Default to example_form_config

Expand Down

0 comments on commit 35d4a69

Please sign in to comment.