feat: add specialty validation for doctor signup#21
Conversation
📝 WalkthroughWalkthroughThis pull request adds a Django migration altering Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
users/serializers.py (1)
116-129: Consider exposingspecialtyinUserMeSerializer.Now that doctor signup captures
specialty, clients hitting/me(and the signup response, which wrapsUserMeSerializer) won't see it. If the frontend needs to display a doctor's specialty, add it to thefieldslist here.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@users/serializers.py` around lines 116 - 129, The UserMeSerializer currently omits the doctor's specialty; update UserMeSerializer to expose it by adding "specialty" to the Meta.fields list (or, if specialty is stored on a related object, add a SerializerMethodField named specialty and implement get_specialty to return the appropriate value), ensuring the "specialty" attribute is serialized in responses from UserMeSerializer (and therefore in signup responses that reuse it).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@users/serializers.py`:
- Line 56: The serializer currently allows a non-doctor to submit a specialty
and it gets persisted; update the serializer's validate and/or create flow to
remove the "specialty" key from validated_data unless the role equals the doctor
role (e.g., Role.DOCTOR or "doctor"), so only doctors keep specialty;
specifically, inside the validate(self, attrs) or create(self, validated_data)
methods remove validated_data.pop("specialty", None) when attrs.get("role") (or
validated_data.get("role")) is not the doctor role before calling
User.objects.create_user(**validated_data), and apply the same logic to any
duplicate handling around lines referenced (66-70) to ensure non-doctor roles
cannot persist stale specialty values.
---
Nitpick comments:
In `@users/serializers.py`:
- Around line 116-129: The UserMeSerializer currently omits the doctor's
specialty; update UserMeSerializer to expose it by adding "specialty" to the
Meta.fields list (or, if specialty is stored on a related object, add a
SerializerMethodField named specialty and implement get_specialty to return the
appropriate value), ensuring the "specialty" attribute is serialized in
responses from UserMeSerializer (and therefore in signup responses that reuse
it).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b6ff8596-2743-4424-91cf-3e264de4a31a
📒 Files selected for processing (3)
users/migrations/0007_alter_user_date_of_birth_alter_user_phone_number.pyusers/serializers.pyusers/views.py
| "phone_number", | ||
| "date_of_birth", | ||
| "role", | ||
| "specialty", |
There was a problem hiding this comment.
Strip specialty for non-doctor roles to prevent stale data.
The new validate only enforces presence for doctors, but specialty is accepted from any caller and passed through create() into User.objects.create_user(**validated_data). A patient/receptionist can supply a specialty value and it will be persisted on the user. Consider dropping it for non-doctor roles:
♻️ Proposed change
def validate(self, attrs: dict) -> dict:
role: str = attrs.get("role", "")
- if role == "doctor" and not str(attrs.get("specialty") or "").strip():
- raise serializers.ValidationError({"specialty": "specialty is required for doctor signup."})
+ if role == "doctor":
+ if not str(attrs.get("specialty") or "").strip():
+ raise serializers.ValidationError({"specialty": "specialty is required for doctor signup."})
+ else:
+ attrs.pop("specialty", None)
return attrsAlso applies to: 66-70
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@users/serializers.py` at line 56, The serializer currently allows a
non-doctor to submit a specialty and it gets persisted; update the serializer's
validate and/or create flow to remove the "specialty" key from validated_data
unless the role equals the doctor role (e.g., Role.DOCTOR or "doctor"), so only
doctors keep specialty; specifically, inside the validate(self, attrs) or
create(self, validated_data) methods remove validated_data.pop("specialty",
None) when attrs.get("role") (or validated_data.get("role")) is not the doctor
role before calling User.objects.create_user(**validated_data), and apply the
same logic to any duplicate handling around lines referenced (66-70) to ensure
non-doctor roles cannot persist stale specialty values.
Summary by CodeRabbit
New Features
Improvements