Skip to content
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: bulk update estimate endpoint #755

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apiserver/plane/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
EstimateViewSet,
EstimatePointViewSet,
ProjectEstimatePointEndpoint,
BulkCreateEstimatePointEndpoint,
BulkEstimatePointEndpoint,
## End Estimates
# Shortcuts
ShortCutViewSet,
Expand Down Expand Up @@ -536,8 +536,8 @@
name="project-estimate-points",
),
path(
"workspaces/<str:slug>/projects/<uuid:project_id>/estimates/<uuid:estimate_id>/bulk-create-estimate-points/",
BulkCreateEstimatePointEndpoint.as_view(),
"workspaces/<str:slug>/projects/<uuid:project_id>/estimates/<uuid:estimate_id>/bulk-estimate-points/",
BulkEstimatePointEndpoint.as_view(),
name="bulk-create-estimate-points",
),
# End States ##
Expand Down
2 changes: 1 addition & 1 deletion apiserver/plane/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@
EstimateViewSet,
EstimatePointViewSet,
ProjectEstimatePointEndpoint,
BulkCreateEstimatePointEndpoint,
BulkEstimatePointEndpoint,
)
63 changes: 62 additions & 1 deletion apiserver/plane/api/views/estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def partial_update(self, request, slug, project_id, estimate_id, pk):


class ProjectEstimatePointEndpoint(BaseAPIView):
permission_classes = [
ProjectEntityPermission,
]

def get(self, request, slug, project_id):
try:
project = Project.objects.get(workspace__slug=slug, pk=project_id)
Expand All @@ -137,7 +141,11 @@ def get(self, request, slug, project_id):
)


class BulkCreateEstimatePointEndpoint(BaseAPIView):
class BulkEstimatePointEndpoint(BaseAPIView):
permission_classes = [
ProjectEntityPermission,
]

def post(self, request, slug, project_id, estimate_id):
try:
estimate = Estimate.objects.get(
Expand All @@ -161,6 +169,8 @@ def post(self, request, slug, project_id, estimate_id):
description=estimate_point.get("description", ""),
project_id=project_id,
workspace_id=estimate.workspace_id,
created_by=request.user,
updated_by=request.user,
)
for estimate_point in estimate_points
],
Expand All @@ -182,3 +192,54 @@ def post(self, request, slug, project_id, estimate_id):
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)

def patch(self, request, slug, project_id, estimate_id):
try:
if not len(request.data.get("estimate_points", [])):
return Response(
{"error": "Estimate points are required"},
status=status.HTTP_400_BAD_REQUEST,
)

estimate_points_data = request.data.get("estimate_points", [])

estimate_points = EstimatePoint.objects.filter(
pk__in=[
estimate_point.get("id") for estimate_point in estimate_points_data
],
workspace__slug=slug,
project_id=project_id,
estimate_id=estimate_id,
)

print(estimate_points)
updated_estimate_points = []
for estimate_point in estimate_points:
# Find the data for that estimate point
estimate_point_data = [
point
for point in estimate_points_data
if point.get("id") == str(estimate_point.id)
]
print(estimate_point_data)
if len(estimate_point_data):
estimate_point.value = estimate_point_data[0].get(
"value", estimate_point.value
)
updated_estimate_points.append(estimate_point)

EstimatePoint.objects.bulk_update(
updated_estimate_points, ["value"], batch_size=10
)
serializer = EstimatePointSerializer(estimate_points, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Estimate.DoesNotExist:
return Response(
{"error": "Estimate does not exist"}, status=status.HTTP_400_BAD_REQUEST
)
except Exception as e:
print(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)