-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[WEB-2092] chore: soft delete operation #5244
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d8958f0
chore: soft delete opration
NarayanBavisetti c72202f
chore: merge conflicts
NarayanBavisetti fed3aa8
chore: migration files
NarayanBavisetti 83afaf9
chore: celery time change
NarayanBavisetti 9eab91e
chore: changed the deletion time
NarayanBavisetti 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 hidden or 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 hidden or 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,158 @@ | ||
| # Django imports | ||
| from django.utils import timezone | ||
| from django.apps import apps | ||
| from django.core.exceptions import ObjectDoesNotExist | ||
|
|
||
| # Third party imports | ||
| from celery import shared_task | ||
|
|
||
|
|
||
| @shared_task | ||
| def soft_delete_related_objects( | ||
| app_label, model_name, instance_pk, using=None | ||
| ): | ||
| model_class = apps.get_model(app_label, model_name) | ||
| instance = model_class.all_objects.get(pk=instance_pk) | ||
| related_fields = instance._meta.get_fields() | ||
| for field in related_fields: | ||
| if field.one_to_many or field.one_to_one or field.many_to_many: | ||
| try: | ||
| if field.one_to_many or field.many_to_many: | ||
| related_objects = getattr(instance, field.name).all() | ||
| elif field.one_to_one: | ||
| related_object = getattr(instance, field.name) | ||
| related_objects = ( | ||
| [related_object] if related_object is not None else [] | ||
| ) | ||
| for obj in related_objects: | ||
| if obj: | ||
| obj.deleted_at = timezone.now() | ||
| obj.save(using=using) | ||
| except ObjectDoesNotExist: | ||
| pass | ||
|
|
||
|
|
||
| # @shared_task | ||
| def restore_related_objects(app_label, model_name, instance_pk, using=None): | ||
| pass | ||
|
|
||
|
|
||
| @shared_task | ||
| def hard_delete(): | ||
|
|
||
| from plane.db.models import ( | ||
| Workspace, | ||
| Project, | ||
| Cycle, | ||
| Module, | ||
| Issue, | ||
| Page, | ||
| IssueView, | ||
| Label, | ||
| State, | ||
| IssueActivity, | ||
| IssueComment, | ||
| IssueLink, | ||
| IssueReaction, | ||
| UserFavorite, | ||
| ModuleIssue, | ||
| CycleIssue, | ||
| Estimate, | ||
| EstimatePoint | ||
| ) | ||
|
|
||
| # check delete workspace | ||
| _ = Workspace.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete project | ||
| _ = Project.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete cycle | ||
| _ = Cycle.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete module | ||
| _ = Module.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete issue | ||
| _ = Issue.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete page | ||
| _ = Page.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete view | ||
| _ = IssueView.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete label | ||
| _ = Label.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # check delete state | ||
| _ = State.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = IssueActivity.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = IssueComment.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = IssueLink.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = IssueReaction.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = UserFavorite.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = ModuleIssue.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = CycleIssue.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = Estimate.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| _ = EstimatePoint.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| # at last, check for every thing which ever is left and delete it | ||
| # Get all Django models | ||
| all_models = apps.get_models() | ||
|
|
||
| # Iterate through all models | ||
| for model in all_models: | ||
| # Check if the model has a 'deleted_at' field | ||
| if hasattr(model, "deleted_at"): | ||
| # Get all instances where 'deleted_at' is greater than 30 days ago | ||
| _ = model.all_objects.filter( | ||
| deleted_at__lt=timezone.now() - timezone.timedelta(days=30) | ||
| ).delete() | ||
|
|
||
| return | ||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.