feat: bulk update estimate endpoint (#755)

This commit is contained in:
pablohashescobar 2023-04-10 10:58:09 +05:30 committed by GitHub
parent eac4b21ead
commit c80968bb23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 5 deletions

View File

@ -83,7 +83,7 @@ from plane.api.views import (
EstimateViewSet, EstimateViewSet,
EstimatePointViewSet, EstimatePointViewSet,
ProjectEstimatePointEndpoint, ProjectEstimatePointEndpoint,
BulkCreateEstimatePointEndpoint, BulkEstimatePointEndpoint,
## End Estimates ## End Estimates
# Shortcuts # Shortcuts
ShortCutViewSet, ShortCutViewSet,
@ -536,8 +536,8 @@ urlpatterns = [
name="project-estimate-points", name="project-estimate-points",
), ),
path( path(
"workspaces/<str:slug>/projects/<uuid:project_id>/estimates/<uuid:estimate_id>/bulk-create-estimate-points/", "workspaces/<str:slug>/projects/<uuid:project_id>/estimates/<uuid:estimate_id>/bulk-estimate-points/",
BulkCreateEstimatePointEndpoint.as_view(), BulkEstimatePointEndpoint.as_view(),
name="bulk-create-estimate-points", name="bulk-create-estimate-points",
), ),
# End States ## # End States ##

View File

@ -135,5 +135,5 @@ from .estimate import (
EstimateViewSet, EstimateViewSet,
EstimatePointViewSet, EstimatePointViewSet,
ProjectEstimatePointEndpoint, ProjectEstimatePointEndpoint,
BulkCreateEstimatePointEndpoint, BulkEstimatePointEndpoint,
) )

View File

@ -117,6 +117,10 @@ class EstimatePointViewSet(BaseViewSet):
class ProjectEstimatePointEndpoint(BaseAPIView): class ProjectEstimatePointEndpoint(BaseAPIView):
permission_classes = [
ProjectEntityPermission,
]
def get(self, request, slug, project_id): def get(self, request, slug, project_id):
try: try:
project = Project.objects.get(workspace__slug=slug, pk=project_id) project = Project.objects.get(workspace__slug=slug, pk=project_id)
@ -137,7 +141,11 @@ class ProjectEstimatePointEndpoint(BaseAPIView):
) )
class BulkCreateEstimatePointEndpoint(BaseAPIView): class BulkEstimatePointEndpoint(BaseAPIView):
permission_classes = [
ProjectEntityPermission,
]
def post(self, request, slug, project_id, estimate_id): def post(self, request, slug, project_id, estimate_id):
try: try:
estimate = Estimate.objects.get( estimate = Estimate.objects.get(
@ -161,6 +169,8 @@ class BulkCreateEstimatePointEndpoint(BaseAPIView):
description=estimate_point.get("description", ""), description=estimate_point.get("description", ""),
project_id=project_id, project_id=project_id,
workspace_id=estimate.workspace_id, workspace_id=estimate.workspace_id,
created_by=request.user,
updated_by=request.user,
) )
for estimate_point in estimate_points for estimate_point in estimate_points
], ],
@ -182,3 +192,54 @@ class BulkCreateEstimatePointEndpoint(BaseAPIView):
{"error": "Something went wrong please try again later"}, {"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST, 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,
)