Skip to content

refactor: consolidate Redis config, validators, tests, and serializer logic #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2025

Conversation

RandomProgramm3r
Copy link
Owner

This commit groups several improvements and refactorings across the project:

  • Redis Configuration

    • Replace the single REDIS_URL variable with distinct REDIS_HOST and REDIS_PORT environment variables for clearer, more flexible setup across environments.
    • Remove the incorrect custom save method on the User model that improperly set last_login; rely on Django’s authentication backend to manage this field.
  • Validators

    • Move validators.py (user-related validation logic) from the core project directory into the user application for better modularization and discoverability.
  • Tests

    • Add a comprehensive test suite for all password validators in user/validators.py covering:
      • UppercaseValidator
      • LowercaseValidator
      • NumericValidator
      • SpecialCharacterValidator
      • AsciiValidator
    • Each validator is tested for successful validation, appropriate failure messages, and correct help text.
  • Serializers & Manager Logic

    • Simplify PromoManager filtering by restructuring age and country query conditions.
    • Streamline Promo model properties (is_available, get_available_unique_codes) for readability and consistency.
    • Refactor serializers (business, core, user) to remove redundant validations already handled by DRF, simplify complex conditionals (e.g., MultiCountryField), and assume authenticated context in BaseUserPromoSerializer.
    • Update settings paths for password validators to reflect their new location in the user app.

This commit refactors the Redis connection settings to use distinct `REDIS_HOST` and `REDIS_PORT` environment variables instead of a single `REDIS_URL`. This change improves configuration clarity and flexibility across environments.

Additionally, it removes an incorrect custom `save` method from the `User` model. The previous implementation improperly set the `last_login` field upon user creation. This logic is flawed, as the `last_login` field should be managed by Django's authentication backend during the actual login process.
The `validators.py` file, containing user-related validation logic, has been moved from the core project directory to the `user` application directory.
This commit introduces a new test suite for the password validation logic in `user/validators.py`.

The tests cover all existing validator classes:
- `UppercaseValidator`
 - `LowercaseValidator`
 - `NumericValidator`
 - `SpecialCharacterValidator`
 - `AsciiValidator`

Each validator is tested for successful validation, failure cases with appropriate error messages, and correct help text generation. This ensures the reliability and correctness of the password validation system.
This commit introduces several refactoring improvements across the codebase to enhance clarity, remove redundancy, and simplify complex logic.

Key changes include:
- **PromoManager:** Simplified the filtering logic for promo codes by restructuring the query conditions for age and country, making it more readable.
- **Promo Model:** Streamlined property methods (`is_available`, `get_available_unique_codes`) for better readability and  consistency.
- **Serializers (`business`, `core`, `user`):**
- Removed unnecessary validation checks that are already handled by Django Rest Framework's default behavior (e.g., required fields).
- Simplified complex conditional logic and improved handling of query parameters (`MultiCountryField`).
- Made user-context checks in `BaseUserPromoSerializer` more direct and less defensive, assuming an authenticated request  context.
- **Settings:** Updated paths for password validators to reflect their new location in the `user` application.
@RandomProgramm3r RandomProgramm3r merged commit 2d44288 into main Jul 25, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant