-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat(shared-views): Add groupsearchview starred endpoint for reordering #87347
Merged
MichaelSun48
merged 4 commits into
master
from
msun/sharedViews/addStarredOrderEndpoint
Mar 19, 2025
+364
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01a8871
Add groupsearchview starred endpoint for reordering
MichaelSun48 a7bfe9d
Update src/sentry/issues/endpoints/organization_group_search_view_sta…
MichaelSun48 60e0d32
Rename endpoints, remove unnecessary try catches
MichaelSun48 903e82e
Refactor to ONLY reorder. Use base manager instead of ad-hoc logic
MichaelSun48 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 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 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
74 changes: 74 additions & 0 deletions
74
src/sentry/issues/endpoints/organization_group_search_view_starred_order.py
This file contains 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,74 @@ | ||
from django.db import IntegrityError, router, transaction | ||
from rest_framework import serializers, status | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
|
||
from sentry import features | ||
from sentry.api.api_owners import ApiOwner | ||
from sentry.api.api_publish_status import ApiPublishStatus | ||
from sentry.api.base import region_silo_endpoint | ||
from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPermission | ||
from sentry.models.groupsearchview import GroupSearchView, GroupSearchViewVisibility | ||
from sentry.models.groupsearchviewstarred import GroupSearchViewStarred | ||
from sentry.models.organization import Organization | ||
|
||
|
||
class MemberPermission(OrganizationPermission): | ||
scope_map = { | ||
"PUT": ["member:read", "member:write"], | ||
} | ||
|
||
|
||
class GroupSearchViewStarredOrderSerializer(serializers.Serializer): | ||
view_ids = serializers.ListField(child=serializers.IntegerField(), required=True, min_length=0) | ||
|
||
def validate_view_ids(self, view_ids): | ||
if len(view_ids) != len(set(view_ids)): | ||
raise serializers.ValidationError("Single view cannot take up multiple positions") | ||
|
||
gsvs = GroupSearchView.objects.filter( | ||
organization=self.context["organization"], id__in=view_ids | ||
) | ||
# This should never happen, but we can check just in case | ||
if any( | ||
gsv.user_id != self.context["user"].id | ||
and gsv.visibility != GroupSearchViewVisibility.ORGANIZATION | ||
for gsv in gsvs | ||
): | ||
raise serializers.ValidationError("You do not have access to one or more views") | ||
|
||
return view_ids | ||
|
||
|
||
@region_silo_endpoint | ||
class OrganizationGroupSearchViewStarredOrderEndpoint(OrganizationEndpoint): | ||
publish_status = {"PUT": ApiPublishStatus.EXPERIMENTAL} | ||
owner = ApiOwner.ISSUES | ||
permission_classes = (MemberPermission,) | ||
|
||
def put(self, request: Request, organization: Organization) -> Response: | ||
if not features.has("organizations:issue-view-sharing", organization, actor=request.user): | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
|
||
serializer = GroupSearchViewStarredOrderSerializer( | ||
data=request.data, context={"organization": organization, "user": request.user} | ||
) | ||
|
||
if not serializer.is_valid(): | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
view_ids = serializer.validated_data["view_ids"] | ||
|
||
try: | ||
with transaction.atomic(using=router.db_for_write(GroupSearchViewStarred)): | ||
GroupSearchViewStarred.objects.reorder_starred_views( | ||
organization=organization, | ||
user_id=request.user.id, | ||
new_view_positions=view_ids, | ||
) | ||
except IntegrityError as e: | ||
return Response(status=status.HTTP_400_BAD_REQUEST, data={"detail": e.args[0]}) | ||
except ValueError as e: | ||
return Response(status=status.HTTP_400_BAD_REQUEST, data={"detail": e.args[0]}) | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) |
This file contains 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 |
---|---|---|
@@ -1,10 +1,52 @@ | ||
from typing import ClassVar | ||
|
||
from django.db import models | ||
from django.db.models import UniqueConstraint | ||
|
||
from sentry.backup.scopes import RelocationScope | ||
from sentry.db.models import FlexibleForeignKey, region_silo_model | ||
from sentry.db.models.base import DefaultFieldsModel | ||
from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey | ||
from sentry.db.models.manager.base import BaseManager | ||
from sentry.models.organization import Organization | ||
|
||
|
||
class GroupSearchViewStarredManager(BaseManager["GroupSearchViewStarred"]): | ||
def reorder_starred_views( | ||
self, organization: Organization, user_id: int, new_view_positions: list[int] | ||
): | ||
""" | ||
Reorders the positions of starred views for a user in an organization. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice explanatory comment! |
||
Does NOT add or remove starred views. | ||
|
||
Args: | ||
organization: The organization the views belong to | ||
user_id: The ID of the user whose starred views are being reordered | ||
new_view_positions: List of view IDs in their new order | ||
|
||
Raises: | ||
ValueError: If there's a mismatch between existing starred views and the provided list | ||
""" | ||
existing_starred_views = self.filter( | ||
organization=organization, | ||
user_id=user_id, | ||
) | ||
|
||
existing_view_ids = {view.group_search_view_id for view in existing_starred_views} | ||
new_view_ids = set(new_view_positions) | ||
|
||
if existing_view_ids != new_view_ids: | ||
raise ValueError("Mismatch between existing and provided starred views.") | ||
|
||
position_map = {view_id: idx for idx, view_id in enumerate(new_view_positions)} | ||
|
||
views_to_update = list(existing_starred_views) | ||
|
||
for view in views_to_update: | ||
view.position = position_map[view.group_search_view_id] | ||
|
||
if views_to_update: | ||
self.bulk_update(views_to_update, ["position"]) | ||
|
||
|
||
@region_silo_model | ||
|
@@ -17,6 +59,8 @@ class GroupSearchViewStarred(DefaultFieldsModel): | |
|
||
position = models.PositiveSmallIntegerField() | ||
|
||
objects: ClassVar[GroupSearchViewStarredManager] = GroupSearchViewStarredManager() | ||
|
||
class Meta: | ||
app_label = "sentry" | ||
db_table = "sentry_groupsearchviewstarred" | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Hmm I wonder if this transaction is necessary anymore since we are just doing a single bulk update