feat: bulk create endpoint for estimate points (#708)

* feat: bulk create endpoint for estimate points

* dev: remove integrity logic and update url
This commit is contained in:
pablohashescobar 2023-04-06 13:59:17 +05:30 committed by GitHub
parent 105428894f
commit 6a579f85ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 2 deletions

View File

@ -83,6 +83,7 @@ from plane.api.views import (
EstimateViewSet,
EstimatePointViewSet,
ProjectEstimatePointEndpoint,
BulkCreateEstimatePointEndpoint,
## End Estimates
# Shortcuts
ShortCutViewSet,
@ -534,6 +535,11 @@ urlpatterns = [
ProjectEstimatePointEndpoint.as_view(),
name="project-estimate-points",
),
path(
"workspaces/<str:slug>/projects/<uuid:project_id>/estimates/<uuid:estimate_id>/bulk-create-estimate-points/",
BulkCreateEstimatePointEndpoint.as_view(),
name="bulk-create-estimate-points",
),
# End States ##
# Shortcuts
path(
@ -753,7 +759,7 @@ urlpatterns = [
name="project-issue-labels",
),
path(
"workspaces/<str:slug>/projects/<uuid:project_id>/bulk-create-labels/",
"workspaces/<str:slug>/projects/<uuid:project_id>/lk-create-labels/",
BulkCreateIssueLabelsEndpoint.as_view(),
name="project-bulk-labels",
),

View File

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

View File

@ -135,3 +135,50 @@ class ProjectEstimatePointEndpoint(BaseAPIView):
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
class BulkCreateEstimatePointEndpoint(BaseAPIView):
def post(self, request, slug, project_id, estimate_id):
try:
estimate = Estimate.objects.get(
pk=estimate_id, workspace__slug=slug, project=project_id
)
estimate_points = request.data.get("estimate_points", [])
if not len(estimate_points) or len(estimate_points) > 8:
return Response(
{"error": "Estimate points are required"},
status=status.HTTP_400_BAD_REQUEST,
)
estimate_points = EstimatePoint.objects.bulk_create(
[
EstimatePoint(
estimate=estimate,
key=estimate_point.get("key", 0),
value=estimate_point.get("value", ""),
description=estimate_point.get("description", ""),
project_id=project_id,
workspace_id=estimate.workspace_id,
)
for estimate_point in estimate_points
],
batch_size=10,
ignore_conflicts=True,
)
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:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)

View File

@ -27,7 +27,6 @@ class EstimatePoint(ProjectBaseModel):
"db.Estimate",
on_delete=models.CASCADE,
related_name="points",
limit_choices_to={"estimate__points__count__lt": 10},
)
key = models.IntegerField(
default=0, validators=[MinValueValidator(0), MaxValueValidator(7)]