fix: setup_config accepts multiple values for list fields#562
fix: setup_config accepts multiple values for list fields#562nishantxscooby wants to merge 4 commits intocertego:developfrom
Conversation
- Enhanced parse_field_value() to handle quoted values with spaces - Supports single quotes, double quotes, and mixed formats - Maintains backward compatibility with unquoted values - Added 6 comprehensive unit tests + 1 integration test - All 25 tests pass (18 existing + 7 new) - Code formatted with Black, isort, and flake8 Fixes certego#499
|
Hello @Lorygold, sorry for the delay, I just got busy with my university's exam. I have made all the required changes, let me know if it's gtg. Thankyou :) |
|
@nishantxscooby some checks have to be added, 'cause if I run the command multiple times, also the already existing values are appended: @Noble-47 fyi |
- Modified append logic to filter out existing values before adding - Prevents duplicate entries when command runs multiple times - Safe for automation and configuration management tools - Added test_setup_config_append_idempotent to verify behavior - All 26 tests pass (19 setup_config + 7 other commands) Fixes issue reported by @Lorygold in PR certego#562
- Modified append logic to filter out existing values before adding - Prevents duplicate entries when command runs multiple times - Safe for automation and configuration management tools - Added test_setup_config_append_idempotent to verify behavior - All 26 tests pass (19 setup_config + 7 other commands) Fixes issue reported by @Lorygold in PR certego#562
|
@Lorygold Good catch! 🎯 I've added a fix to make the append mode idempotent. Now running the command multiple times won't create duplicates. What changed:The append logic now filters out existing values before adding: new_values = [v for v in value if v not in current]
current += new_values |
…yError - Validate array field values against AlertDetectionType.choices before saving - Raise clear CommandError with invalid values and valid choices list - Prevents database IntegrityError from check constraint violation - Addresses feedback from Noble-47 on PR certego#562
…yError - Import AlertDetectionType from impossible_travel.constants - Validate array field values against AlertDetectionType.choices before saving - Raise clear CommandError with invalid values and valid choices list - Prevents database IntegrityError from check constraint violation - Addresses feedback from Noble-47 on PR certego#562
|
@Noble-47 Hi! Sorry, I just realised that the exception handling was missing validation for invalid config values. I've just pushed a fix that adds pre-save validation for Example output:$ python3 manage.py setup_config -a filtered_alerts_types="INVALID_VALUE"
CommandError: Invalid values in 'filtered_alerts_types': ['INVALID_VALUE'].
Valid choices are: ['New Device', 'Imp Travel', 'New Country', 'User Risk Threshold', 'Anonymous IP Login', 'Atypical Country'] |





🎯 Description
Fixes #499
The
setup_configDjango management command now correctly parses and accepts multiple values with spaces for list-type (ArrayField) configuration fields in a single invocation.Problem
Previously, attempting to pass multiple values with spaces would fail:
Solution
Enhanced the
parse_field_value()function with quote-aware CSV parsing and fixed validation logic to properly handle list values:🔧 Changes Made
Modified Files
buffalogs/impossible_travel/management/commands/setup_config.pyparse_field_value()with manual quote-aware CSV parsing[...]are presentbuffalogs/impossible_travel/tests/task/test_management_commands.py-a), override (-o), remove (-r)✅ Testing
Test Results
Manual Test Coverage
setup_configcommand does not accept multiple values for list fields #499 works perfectlyTest Commands
🔄 Backward Compatibility
✅ 100% backward compatible - all existing command syntax continues to work:
# All these still work exactly as before ./manage.py setup_config -a ignored_users=admin ./manage.py setup_config -o allowed_countries=[Italy,Romania] ./manage.py setup_config -r vip_users=user1📚 Implementation Details
Quote-Aware CSV Parsing
The fix uses manual character-by-character parsing to handle quoted CSV values:
Validation Fix
Fixed validation logic to validate the complete value for list fields instead of iterating through individual elements. ArrayField validators expect to receive the entire list, not individual strings.
Before (❌ Bug):
After (✅ Fixed):
🎯 Impact
Before:
After:
📋 Checklist
developbranchsetup_configcommand does not accept multiple values for list fields #499🔧 UPDATE: Idempotent Append Mode (2026-02-07)
Issue Reported by @Lorygold
Running the command multiple times was appending duplicate values:
Fix Applied
Modified the append logic to filter out existing values before adding:
Benefits
test_setup_config_append_idempotent(all 26 tests pass)Test Results