refactor(core, business, serializers): centralize base serializers and streamline promo/user logic#59
Merged
RandomProgramm3r merged 4 commits intomainfrom Jul 17, 2025
Merged
Conversation
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit consolidates shared serializer components into the
coreapp, removes duplication across apps, and optimizes promo-related view and update logic.Key changes:
Centralized serializers
BaseUserSerializer,BaseUserPromoSerializer,BaseCommentSerializer,BaseCompanyPromoSerializer(formerlyBasePromoSerializer),TargetSerializer, andCountryFieldintocore/serializers.pyfor cross‑app reuse.SignUpSerializer,UserProfileSerializer,PromoFeedSerializer,UserPromoDetailSerializer, and related serializers inuserandbusinessto inherit from these core base classes.Simplified view/query logic
CompanyPromoListCreateView: Validates query params directly inget_querysetand removes unnecessary overrides oflist().PromoDetailSerializer#update(): Calls parent update first, then appliestargetchanges to ensure atomic writes and avoid extra saves.Improved pagination & naming
UserFeedQuerySerializerto extend the coreBaseLimitOffsetPaginationSerializer.BasePromoSerializertoBaseCompanyPromoSerializerfor clarity.Enhanced maintainability
UserProfileSerializerto a private_invalidate_cache()method.PromoManager.with_related_fieldsas a tuple for cleareronly()semantics.