-
Notifications
You must be signed in to change notification settings - Fork 113
feat(admin): add organization update API and schemas #1338
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b9e31f2
feat(admin): add organization update API and schemas
sachin9919 05f5b2a
errors resolved
sachin9919 9692f52
fix(admin): add validation for organization update schema
sachin9919 3d4c486
new line added in required file
sachin9919 4fc7f63
fix(admin): prevent whitespace-only organization updates
sachin9919 a954f11
feat(admin): add audit logging for organization updates
sachin9919 c09d1c3
refactor(admin): remove audit logging changes from org update API
sachin9919 f904045
fix(admin): remove unused audit log import
sachin9919 3368b69
style(admin): fix migration docstring spacing
sachin9919 106b5f4
style(admin): add newline at end of file
sachin9919 c65aa05
style(admin): add newline at end of file
sachin9919 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from ninja import Router | ||
| from ninja.errors import HttpError | ||
|
|
||
| from ddpui.auth import has_permission | ||
| from ddpui.models.org import Org | ||
| from ddpui.schemas.admin_org_schema import UpdateOrganizationSchema | ||
|
|
||
| admin_org_router = Router() | ||
|
|
||
|
|
||
| @admin_org_router.get("/v1/organizations") | ||
| @admin_org_router.get("/v1/organizations/") | ||
| @has_permission(["can_manage_organization"]) | ||
| def get_admin_organizations(request, name: str | None = None): | ||
| """List organizations with optional filtering.""" | ||
| queryset = Org.objects.all() | ||
|
|
||
| if name: | ||
| queryset = queryset.filter(name__icontains=name) | ||
|
|
||
| orgs = list(queryset.values("id", "name", "slug")) | ||
|
|
||
| return { | ||
| "success": True, | ||
| "count": len(orgs), | ||
| "data": orgs, | ||
| } | ||
|
|
||
|
|
||
| @admin_org_router.get("/v1/organizations/{org_id}") | ||
| @admin_org_router.get("/v1/organizations/{org_id}/") | ||
| @has_permission(["can_manage_organization"]) | ||
| def get_single_org(request, org_id: int): | ||
| """Get single organization by ID.""" | ||
| org = ( | ||
| Org.objects.filter(id=org_id) | ||
| .values("id", "name", "slug") | ||
| .first() | ||
| ) | ||
|
|
||
| if not org: | ||
| raise HttpError(404, "Organization not found") | ||
|
|
||
| return { | ||
| "success": True, | ||
| "data": org, | ||
| } | ||
|
|
||
|
|
||
| @admin_org_router.put("/v1/organizations/{org_id}") | ||
| @admin_org_router.put("/v1/organizations/{org_id}/") | ||
| @has_permission(["can_manage_organization"]) | ||
| def update_organization( | ||
| request, | ||
| org_id: int, | ||
| payload: UpdateOrganizationSchema, | ||
| ): | ||
| """Update organization details.""" | ||
| org = Org.objects.filter(id=org_id).first() | ||
|
|
||
| if not org: | ||
| raise HttpError(404, "Organization not found") | ||
|
|
||
| existing_slug = ( | ||
| Org.objects.filter(slug=payload.slug) | ||
| .exclude(id=org_id) | ||
| .exists() | ||
| ) | ||
|
|
||
| if existing_slug: | ||
| raise HttpError(400, "Slug already exists") | ||
|
|
||
| org.name = payload.name | ||
| org.slug = payload.slug | ||
| org.save() | ||
|
|
||
| return { | ||
| "success": True, | ||
| "message": "Organization updated successfully", | ||
| "data": { | ||
| "id": org.id, | ||
| "name": org.name, | ||
| "slug": org.slug, | ||
| }, | ||
| } | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from ninja import Schema | ||
|
|
||
|
|
||
| class UpdateOrganizationSchema(Schema): | ||
| """Schema for updating organization details.""" | ||
|
|
||
| name: str | ||
| slug: str | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: DalgoT4D/DDP_backend
Length of output: 50377
Slug uniqueness check is non-atomic and race-prone.
The
Org.slugfield has no database-level uniqueness constraint (unique=TrueorUniqueConstraint). The pre-check with.exists()and the write with.save()are separate operations, allowing concurrent requests to both pass validation and create duplicate slugs.Add a database constraint and wrap the write in atomic transaction handling with
IntegrityErrorcatching. Also update the HTTP status code to 409 (Conflict) to properly reflect the error semantics.💡 Suggested hardening
Also add to the
Orgmodel (ddpui/models/org.py):class Org(models.Model): name = models.CharField(max_length=50) - slug = models.CharField(max_length=20, null=True) + slug = models.CharField(max_length=20, null=True, unique=True)🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. I’ve noted the race-condition concern around slug uniqueness validation. Since this PR focuses on introducing the initial admin update APIs, I’d prefer to keep database-level uniqueness constraints and transactional handling as a follow-up improvement to avoid expanding the migration scope significantly in this change set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sachin9919, understood — thanks for the context. Deferring database-level constraints and transactional handling to a follow-up is a reasonable call to keep this PR focused.Would you like me to open a GitHub issue to track the follow-up work? It would cover:
unique=True(or aUniqueConstraint) toOrg.slugalong with the corresponding migration.org.save()intransaction.atomic()and catchingIntegrityErrorto returnHTTP 409on slug conflicts in the update endpoint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the schema validation to reject whitespace-only values for
nameandslugusingfield_validator, ensuring inputs are properly stripped and validated before update operations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 683
@sachin9919, the implementation looks solid. A few notes from reviewing the updated file:Correct layering —
Field(..., min_length=1)rejects empty strings at Pydantic's field level, andmust_not_be_blankthen catches whitespace-only inputs (e.g." "). The two guards complement each other well.Silent normalisation — because the validator returns
value.strip(), inputs like" admin "will be silently saved as"admin". This is usually desirable, but worth being aware of (and potentially documenting) so callers know the API normalises values rather than rejecting them.Classmethod pattern —
@field_validator+@classmethodis the correct Pydantic v2 pattern; no issues there.The race-condition / atomicity concern from the earlier review thread is still tracked as a follow-up. Would you still like me to open a GitHub issue to track:
unique=TruetoOrg.slugwith a migration.org.save()intransaction.atomic()and catchingIntegrityError→HTTP 409?