Skip to content

refactor(user, validators): extract PromoActivationService & simplify password validators #62

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 3 commits into from
Jul 23, 2025

Conversation

RandomProgramm3r
Copy link
Owner

This commit consolidates and improves core user-related logic by:

  • Extracting promo code activation functionality from PromoActivateView into a new PromoActivationService in user/services.py, which now handles:
    • User targeting (age, country) checks
    • Promo active status verification
    • Anti‑fraud validation
    • Atomic issuance for common and unique promo codes
    • Granular exceptions (PromoActivationError, TargetingError, etc.) for clearer API error responses
  • Simplifying password validation by:
    • Removing the BaseCountPasswordValidator abstract class in favor of standalone validators
    • Renaming classes for brevity (e.g. AsciiValidator, UppercaseValidator)
    • Leveraging built‑in methods like isascii() and isupper() for clarity and performance
    • Using ngettext to enhance internationalized, pluralized error messages
    • Updating settings.py to reference new validator names and options

Both the view and validator integrations have been streamlined for maintainability and adherence to Django conventions.

This commit refactors the promo code activation logic by extracting it from the `PromoActivateView` into a dedicated `PromoActivationService`.

Key changes:
- Created `user/services.py` to house the new service class.
- The `PromoActivationService` now encapsulates all business logic for activating a promo code, including:
- User targeting validation (age, country).
- Promotion active status check.
- Anti-fraud system verification.
- Atomic issuance of common or unique promo codes.
- Introduced a set of custom, specific exceptions (`PromoActivationError`, `TargetingError`, etc.) to provide clearer error feedback to the API client.
- The `PromoActivateView` is now significantly simplified, delegating all logic to the service and handling its success or failure responses.
This commit refactors the password validation classes to improve clarity, reduce complexity, and align better with Django conventions.

The key changes include:
 - Removed the `BaseCountPasswordValidator` abstract base class, as it added unnecessary complexity. Each validator is now a self-contained class.
 - Simplified the logic within each validator, using more direct and efficient methods for character checks (e.g., `isascii()`, `isupper()`).
 - Renamed validator classes for conciseness (e.g., `ASCIIOnlyPasswordValidator` to `AsciiValidator`).
 - Improved internationalization and pluralization of error messages by using `ngettext`.
 - Updated `settings.py` to reflect the new validator class names and their simplified options.
@RandomProgramm3r RandomProgramm3r merged commit 801fe45 into main Jul 23, 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