Conversation
|
I'm not assigned to this, but I just wanna say, this PR looks great! |
There was a problem hiding this comment.
🔍 Peer Review: Amazing Work on This PR!
Hey Dorian 👋 — I just finished reviewing your PR titled "Chat refactorization #149".
Here’s my feedback:
✅ What Looks Great
- The code is indeed modular, readable, and maintainable, great job!
- The core implementation of the API endpoints + CRUD for enabling chat functionality works as expected.
- All tests passed successfully.
- The tests are logical and well structured. I reviewed
apps/chat/tests.pyand can confirm that they reflect appropriate usage and expected behavior. - Nice utilization of AI to assist you with refactoring your models.py, and with code structure in other areas.
From my review:
- The move to custom managers (
ChatManager,MessageManager) is clean and follows Django best practices. - The serializers are well-structured and make good use of nested serializers and
read_onlyfields. - Including tests for chat creation, duplicate prevention, message posting, and permission enforcement adds great value and shows strong test coverage.
💡 Suggestions for Improvement (if any)
These are ideas for future improvements.
- Consider the few comments provided, many can be fixed or added in the next PR.
- (Optional suggestion for views.py) Implement pagination in messages method of ChatViewSet. Namely because in
ChatViewSet.messages(), if chats grow large, pagination might be worth considering to avoid performance hits. - You could add a few negative test cases in a future PR. For instance, sending a message in a chat you're not part of, or creating a chat with yourself.
🧪 Tested This Feature
I ran the following test steps:
- ✅ Reviewed and verified each modified file:
managers.py,models.py,serializers.py,tests.py,urls.py, andviews.py. - ✅ Followed the step-by-step testing guide: All tests passed successfully.
- ✅ Checked the Chat model in the Django admin panel for correct behavior.
🔄 Next Steps
- Coordinate with frontend teammates to integrate this with the frontend chat UI.
- Add validation-focused and permission-based negative test cases in the next iteration.
🚀 Final Thoughts
Awesome work, Dorian! The PR is in great shape to be merged. You're making strong contributions to the project. If anything is unclear, please let me know. Aside of the few comments/suggestions I left, this PR will be approved. Keep up the great work.
-- Ethan
alliekameda
left a comment
There was a problem hiding this comment.
🔍 Peer Review: Splendid Work on This PR!
Dory, hi!! 👋 :wcatjam: — just finished reviewing your PR "Chat refactorization #149". Here’s my feedback:
✅ What Looks Great
- The core chat and messaging features are implemented cleanly.
- Excellent use of
ModelViewSetand token-based authentication. - Clear separation of business logic into
managers.py. Meta,related_name, and serializers are all nicely structured.- Thorough test coverage. Both success cases and interactions between components are covered.
💡 Suggestions for Improvement
models.py
- Consider enforcing the
user1 < user2constraint via aclean()method to ensure consistent ordering beyondget_or_create_chat.
def clean(self):
if self.user1.id > self.user2.id:
self.user1, self.user2 = self.user2, self.user1- Clean up commented-out code like the legacy
save()method for brevity (can always retrieve from Git history).
managers.py
- Use a helper like
normalize_users()to clarify the user ordering logic.
def normalize_users(self, user1, user2):
return (user2, user1) if user1.id > user2.id else (user1, user2)- Wrap message creation and chat update inside a
transaction.atomic()block to avoid partial updates if something fails.
from django.db import transaction
def create_message(self, chat, sender, content):
with transaction.atomic():
message = self.create(chat=chat, sender=sender, content=content)
chat.updatedAt = now()
chat.save()
return messageviews.py
- You might override
create()instead ofperform_create()for more control and consistent return handling. - Add a validation check to prevent users from creating chats with themselves.
if user1 == user2:
raise serializers.ValidationError("Cannot create chat with yourself. :(")- Paginate message histories like Ethan suggested with
PageNumberPagination.
from rest_framework.pagination import PageNumberPagination
class MessagePagination(PageNumberPagination):
page_size = 20serializers.py
- Add validation for empty or whitespace-only messages in
validate_content.
def validate_content(self, value):
if not value.strip():
raise serializers.ValidationError("Message cannot be empty.")
return value- Use
PrimaryKeyRelatedField(read_only=True)for user fields to avoid exposing full user objects unnecessarily.
user1 = serializers.PrimaryKeyRelatedField(read_only=True)tests.py
- Like Ethan suggested, consider adding edge case tests for:
- Empty or invalid message content.
- Creating chats with yourself.
- Accessing a chat the user isn’t part of.
- You could also DRY up test setup using a user factory or helper method to reduce duplication.
def create_user(self, username):
return User.objects.create_user(username=username, password='testpass')urls.py
- The explicit
send_messageandchat_list/chat_detailendpoints might be redundant givenModelViewSet, consider simplifying. - Adding an
app_name = 'chat'helps with reverse URL resolution and namespacing down the line.
🧪 Tested This Feature
I ran the following test steps:
- ✅ Created chats between different users and verified uniqueness logic.
- ✅ Sent messages and verified timestamps and order.
- ✅ Hit edge cases (e.g., duplicate chat, chat with self).
- ✅ Reviewed API response structures for clarity and correctness.
🔄 Next Steps
- Let me know if you want help implementing the optional suggestions (pagination, validation helpers, cleanup).
- Once updated or confirmed, I’ll gladly re-review and approve for merge!
🚀 Final Thoughts
This PR is solid, readable, testable, and will scale well. Thanks for all your effort, Dory!! ≽(•⩊ •マ≼ ✨
Co-authored-by: Ethan Zambrano <161177620+EthanZ23@users.noreply.github.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #149 +/- ##
=======================================
Coverage ? 57.05%
=======================================
Files ? 14
Lines ? 475
Branches ? 37
=======================================
Hits ? 271
Misses ? 196
Partials ? 8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
alliekameda
left a comment
There was a problem hiding this comment.
Everything still looks good :)
EthanZ23
left a comment
There was a problem hiding this comment.
✅ Approved
Thanks for quickly addressing the feedback! The updates/additions were made efficiently, and I appreciate how you clearly answered all my questions. Everything looks great, solid work and clean implementation throughout.
Profile Models Extended
Refactors existing
ChatandMessagemodels and views to more appropriately match the given issue.Overview
What does this PR do?
Refactors existing
ChatandMessagemodels and views to more appropriately match the given issue. Associated files have been changed.Key Features & Changes
What has been added or changed?
✔ Chat and Message models have been changed
✔ Database querying responsibility yielded from models.py to managers.py
✔ Views: Reworked the CRUD endpoints by using Django's existing ModelViewSet.
✔ Serializers for the corresponding models
Why This Is Needed
What problem does this solve?
To more appropriately handle the Chat and Message models, as the previous iteration was rudimentary. This new implementation is more streamlined.
Related Issue
🔗 This PR addresses Issue #26
Implementation Details
How was this feature developed?
Django
How to Test This PR
Step-by-step testing guide
backendfolder.env/Scripts/activate. For Windows (PowerShell) users, this is.\env\Scripts\Activate.ps1.pip install -r requirements.txtpython manage.py migratepython manage.py test apps.chatapps/chat/tests.pyand ensure that the tests make sense, and reflect appropriate usage.Files Added/Modified
📂 List of important files changed in this PR
backend/apps/chat/managers.pybackend/apps/chat/models.pymanagers.pybackend/apps/chat/serializers.pybackend/apps/chat/tests.pybackend/apps/chat/urls.pybackend/apps/chat/views.pyAI Code Usage
🤖 Was AI-generated code used in this PR?
Checklist for Reviewers
Things to check before approving this PR:
Next Steps & Future Enhancements
What improvements can be made after merging?