Skip to content

Commit c92fe61

Browse files
[WEB-2600] fix: estimate point deletion (#5762)
* chore: only delete the cascade fields * chore: logged the issue activity
1 parent 7bb0400 commit c92fe61

File tree

4 files changed

+87
-23
lines changed

4 files changed

+87
-23
lines changed

apiserver/plane/app/views/estimate/base.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import random
22
import string
3+
import json
4+
5+
# Django imports
6+
from django.utils import timezone
37

48
# Third party imports
59
from rest_framework.response import Response
@@ -19,6 +23,7 @@
1923
EstimateReadSerializer,
2024
)
2125
from plane.utils.cache import invalidate_cache
26+
from plane.bgtasks.issue_activities_task import issue_activity
2227

2328

2429
def generate_random_name(length=10):
@@ -249,11 +254,66 @@ def destroy(
249254
)
250255
# update all the issues with the new estimate
251256
if new_estimate_id:
252-
_ = Issue.objects.filter(
257+
issues = Issue.objects.filter(
258+
project_id=project_id,
259+
workspace__slug=slug,
260+
estimate_point_id=estimate_point_id,
261+
)
262+
for issue in issues:
263+
issue_activity.delay(
264+
type="issue.activity.updated",
265+
requested_data=json.dumps(
266+
{
267+
"estimate_point": (
268+
str(new_estimate_id)
269+
if new_estimate_id
270+
else None
271+
),
272+
}
273+
),
274+
actor_id=str(request.user.id),
275+
issue_id=issue.id,
276+
project_id=str(project_id),
277+
current_instance=json.dumps(
278+
{
279+
"estimate_point": (
280+
str(issue.estimate_point_id)
281+
if issue.estimate_point_id
282+
else None
283+
),
284+
}
285+
),
286+
epoch=int(timezone.now().timestamp()),
287+
)
288+
issues.update(estimate_point_id=new_estimate_id)
289+
else:
290+
issues = Issue.objects.filter(
253291
project_id=project_id,
254292
workspace__slug=slug,
255293
estimate_point_id=estimate_point_id,
256-
).update(estimate_point_id=new_estimate_id)
294+
)
295+
for issue in issues:
296+
issue_activity.delay(
297+
type="issue.activity.updated",
298+
requested_data=json.dumps(
299+
{
300+
"estimate_point": None,
301+
}
302+
),
303+
actor_id=str(request.user.id),
304+
issue_id=issue.id,
305+
project_id=str(project_id),
306+
current_instance=json.dumps(
307+
{
308+
"estimate_point": (
309+
str(issue.estimate_point_id)
310+
if issue.estimate_point_id
311+
else None
312+
),
313+
}
314+
),
315+
epoch=int(timezone.now().timestamp()),
316+
)
257317

258318
# delete the estimate point
259319
old_estimate_point = EstimatePoint.objects.filter(

apiserver/plane/app/views/project/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def create(self, request, slug):
413413
status=status.HTTP_410_GONE,
414414
)
415415

416-
@allow_permission([ROLE.ADMIN, ROLE.MEMBER], level="WORKSPACE")
416+
@allow_permission([ROLE.ADMIN])
417417
def partial_update(self, request, slug, pk=None):
418418
try:
419419
workspace = Workspace.objects.get(slug=slug)

apiserver/plane/bgtasks/deletion_task.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.utils import timezone
33
from django.apps import apps
44
from django.conf import settings
5+
from django.db import models
56
from django.core.exceptions import ObjectDoesNotExist
67

78
# Third party imports
@@ -18,17 +19,25 @@ def soft_delete_related_objects(
1819
for field in related_fields:
1920
if field.one_to_many or field.one_to_one:
2021
try:
21-
if field.one_to_many:
22-
related_objects = getattr(instance, field.name).all()
23-
elif field.one_to_one:
24-
related_object = getattr(instance, field.name)
25-
related_objects = (
26-
[related_object] if related_object is not None else []
27-
)
28-
for obj in related_objects:
29-
if obj:
30-
obj.deleted_at = timezone.now()
31-
obj.save(using=using)
22+
# Check if the field has CASCADE on delete
23+
if (
24+
hasattr(field.remote_field, "on_delete")
25+
and field.remote_field.on_delete == models.CASCADE
26+
):
27+
if field.one_to_many:
28+
related_objects = getattr(instance, field.name).all()
29+
elif field.one_to_one:
30+
related_object = getattr(instance, field.name)
31+
related_objects = (
32+
[related_object]
33+
if related_object is not None
34+
else []
35+
)
36+
37+
for obj in related_objects:
38+
if obj:
39+
obj.deleted_at = timezone.now()
40+
obj.save(using=using)
3241
except ObjectDoesNotExist:
3342
pass
3443

@@ -154,8 +163,7 @@ def hard_delete():
154163
if hasattr(model, "deleted_at"):
155164
# Get all instances where 'deleted_at' is greater than 30 days ago
156165
_ = model.all_objects.filter(
157-
deleted_at__lt=timezone.now()
158-
- timezone.timedelta(days=days)
166+
deleted_at__lt=timezone.now() - timezone.timedelta(days=days)
159167
).delete()
160168

161169
return

apiserver/plane/bgtasks/issue_activities_task.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def track_estimate_points(
465465
IssueActivity(
466466
issue_id=issue_id,
467467
actor_id=actor_id,
468-
verb="updated",
468+
verb="removed" if new_estimate is None else "updated",
469469
old_identifier=(
470470
current_instance.get("estimate_point")
471471
if current_instance.get("estimate_point") is not None
@@ -1700,16 +1700,12 @@ def issue_activity(
17001700
event=(
17011701
"issue_comment"
17021702
if activity.field == "comment"
1703-
else "inbox_issue"
1704-
if inbox
1705-
else "issue"
1703+
else "inbox_issue" if inbox else "issue"
17061704
),
17071705
event_id=(
17081706
activity.issue_comment_id
17091707
if activity.field == "comment"
1710-
else inbox
1711-
if inbox
1712-
else activity.issue_id
1708+
else inbox if inbox else activity.issue_id
17131709
),
17141710
verb=activity.verb,
17151711
field=(

0 commit comments

Comments
 (0)