Skip to content

refactor(core, business, serializers): centralize base serializers and streamline promo/user logic #59

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 17, 2025

Conversation

RandomProgramm3r
Copy link
Owner

This commit consolidates shared serializer components into the core app, removes duplication across apps, and optimizes promo-related view and update logic.

Key changes:

  • Centralized serializers

    • Moved BaseUserSerializer, BaseUserPromoSerializer, BaseCommentSerializer, BaseCompanyPromoSerializer (formerly BasePromoSerializer), TargetSerializer, and CountryField into core/serializers.py for cross‑app reuse.
    • Updated SignUpSerializer, UserProfileSerializer, PromoFeedSerializer, UserPromoDetailSerializer, and related serializers in user and business to inherit from these core base classes.
  • Simplified view/query logic

    • CompanyPromoListCreateView: Validates query params directly in get_queryset and removes unnecessary overrides of list().
    • PromoDetailSerializer#update(): Calls parent update first, then applies target changes to ensure atomic writes and avoid extra saves.
  • Improved pagination & naming

    • Refactored UserFeedQuerySerializer to extend the core BaseLimitOffsetPaginationSerializer.
    • Renamed BasePromoSerializer to BaseCompanyPromoSerializer for clarity.
  • Enhanced maintainability

    • Extracted cache invalidation in UserProfileSerializer to a private _invalidate_cache() method.
    • Defined PromoManager.with_related_fields as a tuple for clearer only() semantics.

 This commit centralizes several core serializer components into the `core` app to promote reusability and improve code organization across the project.

Key changes:
- **Moved Serializers to Core:** `BasePromoSerializer`, `TargetSerializer`, and `CountryField` have been moved from the `business` app to `core.serializers`. This allows them to be reused by other apps, such as `user`.
- **Simplified User Serializers:** The `UserFeedQuerySerializer` now inherits from the `BaseLimitOffsetPaginationSerializer`, and the `OtherFieldSerializer` now uses the
     centralized `core.serializers.CountryField`, reducing code duplication and simplifying validation.
This commit refactors the `business` app's views and serializers to improve efficiency and clarify the logic for handling promo data.

Key changes:
 - **Simplified Query Validation:** The `CompanyPromoListCreateView` now validates query parameters directly within the `get_queryset` method, removing the need for an intermediate `list` method override and instance variables.
- **Optimized Target Updates:** The `PromoDetailSerializer` now correctly handles updates to the `target` field by calling the parent `update` method first and then saving the `target` field specifically. This ensures atomicity and prevents unnecessary model saves.
 - **Improved QuerySet Definition:** The `PromoManager` now defines `with_related_fields` as a tuple to clearly specify the fields for `only()`, making the query more  readable and maintainable.
Refactor user, promo, and comment serializers to improve maintainability and reduce redundant code by using base classes.

 - Introduce `BaseUserSerializer` to share common user fields and logic between `SignUpSerializer` and `UserProfileSerializer`.
 - Create `BaseUserPromoSerializer` to consolidate common fields and methods for `PromoFeedSerializer` and `UserPromoDetailSerializer`.
- Implement `BaseCommentSerializer` to streamline `CommentSerializer`, `CommentCreateSerializer`, and `CommentUpdateSerializer`.
- Simplify the update logic in `UserProfileSerializer` and extract cache invalidation into a private `_invalidate_cache` method.
Relocate base serializers for users, promos, and comments from the `user` and `business` apps to the central `core` app. This change promotes code reuse and establishes a more logical and scalable project structure.

- Moved `BaseUserSerializer`, `BaseUserPromoSerializer`, and `BaseCommentSerializer` to `core/serializers.py`.
- Renamed `BasePromoSerializer` to `BaseCompanyPromoSerializer` for clarity.
- Updated all dependent serializers in the `user` and `business` apps to inherit from the new centralized base classes in `core`.
@RandomProgramm3r RandomProgramm3r merged commit b4acdc3 into main Jul 17, 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