Skip to content

Conversation

RandomProgramm3r
Copy link
Owner

Introduces a shared pagination serializer and consolidates promo-related serializers,
validation logic, and country-code handling to improve structure, DRY-ness, and maintainability.

Key changes:

  • Core pagination

    • Add BaseLimitOffsetPaginationSerializer to centralize limit/offset validation for all endpoints.
  • Promo serializers overhaul

    • Collapse PromoValidator into serializers to co‑locate validation and data representation.
    • Create BasePromoSerializer to extract common fields and methods, reducing duplication across create, detail, and read-only serializers.
    • Implement CountryField and MultiCountryField for reusable ISO 3166‑1 α‑2 code validation.
    • Simplify PromoListQuerySerializer by inheriting from the base pagination serializer and using MultiCountryField.
  • Enhanced promo validation

    • Support partial updates (PATCH), inferring mode from instance when absent in request data.
    • Prevent lowering max_count below the current used_count.
    • Ensure get_used_codes_count returns accurate counts for COMMON‑mode promotions.

Introduced `BaseLimitOffsetPaginationSerializer` to handle the validation of `limit` and `offset` query parameters.
This commit refactors the promotion-related serializers to improve structure, validation, and maintainability, following DRF best practices.

Key changes:
- Removed the separate `PromoValidator` class and integrated its validation logic directly into the serializers. This co-locates validation with the data representation, making the code easier to follow.
- Introduced `BasePromoSerializer` to consolidate common fields and methods, reducing code duplication across create, detail, and read-only serializers.
- Implemented custom `CountryField` and `MultiCountryField` to provide robust, reusable validation for ISO 3166-1 alpha-2 country codes.
- Simplified `PromoListQuerySerializer` by leveraging the new `MultiCountryField` and inheriting from the base pagination serializer, removing manual parameter validation.
This commit improves the validation logic within the promo serializers and refines how promotion data is handled and displayed.

Key changes:
- **Improved Validation:** The validation logic now correctly handles partial updates (`PATCH`) by checking the `mode` from the instance if it's not provided in the request data. It also prevents `max_count` from being set lower than the current `used_count` for a promo.
- **Accurate Used Count:** The `get_used_codes_count` method on the `Promo` model now correctly returns the `used_count` for promotions in `COMMON` mode.
@RandomProgramm3r RandomProgramm3r merged commit d33d22e into main Jul 15, 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