OWASP / OWASP/Nest

Refactor and simplify Event.parse_dates for improved readability and maintainability

Open
#3,556 1 comment 0 reactions 0 assignees View on GitHub
bug enhancement question
Dominant language
Python
Stars
451
Forks
702
Avg merge
22h 59m
Merged PRs (30d)
91

Description

## Is your feature request related to a problem? Please describe.

The `parse_dates` static method in the `Event` model currently contains complex logic with multiple nested `try/except` blocks and inline regular expression compilation. This makes the method difficult to read, reason about, and safely modify.

As the method is responsible for parsing date strings that may include ranges and multiple formats, its high cyclomatic complexity increases the risk of subtle bugs when changes are made. There is also an existing TODO comment in the code explicitly suggesting that this method should be refactored when possible.

Continuing with the current implementation makes the logic harder to test in isolation and increases maintenance cost over time.

## Describe the solution you'd like

Refactor `Event.parse_dates` to improve readability and maintainability by:
- Extracting regular expression patterns into module-level constants.
- Splitting the logic into smaller, focused helper functions (e.g. handling ranges, single dates, or format normalization).
- Reducing nested control flow where possible to make the date-parsing logic easier to follow and test.

The refactor should preserve existing behavior while making the code easier to understand and extend.

## Describe alternatives you've considered

Leaving the method as-is and continuing to make incremental changes when needed. However, this keeps the complexity concentrated in a single function and makes future modifications more error-prone.

## Are you going to work on implementing this?

- [x] Yes
- [ ] No

## Additional context

The method already includes a TODO indicating the need for refactoring. This change would primarily target code quality and long-term maintainability rather than introducing new behavior.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.