Skip to content

feat: add specialty validation for doctor signup#21

Open
M0stafa077 wants to merge 1 commit into
mainfrom
feat/signup/doctor-specialty
Open

feat: add specialty validation for doctor signup#21
M0stafa077 wants to merge 1 commit into
mainfrom
feat/signup/doctor-specialty

Conversation

@M0stafa077

@M0stafa077 M0stafa077 commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added specialty field requirement to doctor registration
  • Improvements

    • Date of birth and phone number are now optional during signup

@coderabbitai

coderabbitai Bot commented Apr 16, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This pull request adds a Django migration altering date_of_birth and phone_number fields to include validators and null/blank properties, removes an unused TokenPair dataclass from serializers, adds specialty field to the signup serializer with conditional doctor-role validation, and refactors the User model import in views to use get_user_model() instead of direct import.

Changes

Cohort / File(s) Summary
Django Migration
users/migrations/0007_alter_user_date_of_birth_alter_user_phone_number.py
Alters date_of_birth and phone_number fields to be nullable/blank with field validators; depends on prior migration 0006_user_optional_phone_and_dob.
Signup Serializer Updates
users/serializers.py
Removes unused TokenPair dataclass; adds "specialty" field to signup metadata; implements conditional validation requiring non-blank specialty when role is "doctor".
User Model Import Refactor
users/views.py
Replaces direct import of User from users.models with get_user_model() call; repositions assignment after Group imports without changing functional behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • youssef2272002salah
  • mohamedhamdy746

Poem

🐰 A doctor's specialty now takes its place,
In signup forms with validators' grace,
Fields grow flexible, null and pristine,
While imports dance—get_user_model's seen!
Clean code hops forward, one step more.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: adding specialty field validation for doctor signup, which is directly addressed in the SignupSerializer changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/signup/doctor-specialty

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
users/serializers.py (1)

116-129: Consider exposing specialty in UserMeSerializer.

Now that doctor signup captures specialty, clients hitting /me (and the signup response, which wraps UserMeSerializer) won't see it. If the frontend needs to display a doctor's specialty, add it to the fields list 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

📥 Commits

Reviewing files that changed from the base of the PR and between f97f93b and 8c2be24.

📒 Files selected for processing (3)
  • users/migrations/0007_alter_user_date_of_birth_alter_user_phone_number.py
  • users/serializers.py
  • users/views.py

Comment thread users/serializers.py
"phone_number",
"date_of_birth",
"role",
"specialty",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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 attrs

Also 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.

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