Skip to content
Draft
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
4 changes: 2 additions & 2 deletions hypha/apply/activity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def get_absolute_url(self):
class Activity(models.Model):
timestamp = models.DateTimeField()
type = models.CharField(choices=ACTIVITY_TYPES.items(), max_length=30)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

source_content_type = models.ForeignKey(
ContentType,
Expand Down Expand Up @@ -372,7 +372,7 @@ class Event(models.Model):
when = models.DateTimeField(auto_now_add=True)
type = models.CharField(_("verb"), choices=MESSAGES.choices, max_length=50)
by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True
)
content_type = models.ForeignKey(
ContentType, blank=True, null=True, on_delete=models.CASCADE
Expand Down
1 change: 1 addition & 0 deletions hypha/apply/activity/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MESSAGES(TextChoices):
OPENED_SEALED = "OPENED_SEALED", _("opened sealed submission")
REVIEW_OPINION = "REVIEW_OPINION", _("reviewed opinion")
DELETE_SUBMISSION = "DELETE_SUBMISSION", _("deleted submission")
ANONYMIZE_SUBMISSION = "ANONYMIZE_SUBMISSION", _("anonymized submission")
DELETE_REVIEW = "DELETE_REVIEW", _("deleted review")
DELETE_REVIEW_OPINION = "DELETE_REVIEW_OPINION", _("deleted review opinion")
CREATED_PROJECT = "CREATED_PROJECT", _("created project")
Expand Down
23 changes: 23 additions & 0 deletions hypha/apply/funds/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import nh3
from django import forms
from django.conf import settings
from django.db.models import Q
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -495,6 +496,28 @@ class Meta:
fields = ["invited_user_email", "submission"]


class DeleteSubmissionForm(forms.Form):
# Alpine.js code added as an attribute to update confirmation text (ie. when a user has to type `delete` to finalize deletion)
anon_or_delete = forms.ChoiceField(
choices=[
("ANONYMIZE", "Anonymize submission"),
("DELETE", "Delete submission"),
],
widget=forms.RadioSelect(
attrs={
"class": "text-sm radio-sm",
"@click": "mode = $el.value.toLowerCase()",
}
),
initial="ANONYMIZE",
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not settings.SUBMISSION_SKELETONING_ENABLED:
del self.fields["anon_or_delete"]


class EditCoApplicantForm(forms.ModelForm):
role = forms.ChoiceField(
choices=CoApplicantRole.choices, label="Role", required=False
Expand Down
89 changes: 0 additions & 89 deletions hypha/apply/funds/management/commands/drafts_cleanup.py

This file was deleted.

134 changes: 134 additions & 0 deletions hypha/apply/funds/management/commands/submission_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import argparse
from datetime import timedelta

from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils import timezone

from hypha.apply.funds.models.submissions import ApplicationSubmission
from hypha.apply.funds.workflows import DRAFT_STATE


def check_not_negative_or_zero(value) -> int:
"""Used to validate a provided days argument

Args:
value: Argument to be validated

Returns:
int: value that is > 0

Raises:
argparse.ArgumentTypeError: if not non-negative integer or 0
"""
try:
ivalue = int(value)
except ValueError:
ivalue = -1

if ivalue <= 0:
raise argparse.ArgumentTypeError(
f'"{value}" is an invalid non-negative integer value'
)
return ivalue


class Command(BaseCommand):
help = "Clean up submissions by deleting or anonymizing them. NOTE: Drafts will only be deleted if specified."

def add_arguments(self, parser):
parser.add_argument(
"--drafts",
action="store",
type=check_not_negative_or_zero,
help="Delete drafts that were created before the provided day threshold",
required=False,
)

parser.add_argument(
"--submissions",
action="store",
type=check_not_negative_or_zero,
help="Anonymize submissions that haven't seen activity after the provided day threshold",
required=False,
)

parser.add_argument(
"--delete",
action="store_false",
dest="interactive",
help="Delete submissions instead of anonymizing them. NOTE: Drafts will always be deleted, not anonymized",
)

parser.add_argument(
"--noinput",
"--no-input",
action="store_false",
dest="interactive",
help="Do not prompt the user for confirmation",
)

@transaction.atomic
def handle(self, *args, **options):
interactive = options["interactive"]

if not options["drafts"] and not options["submissions"]:
self.stdout.write(
"Please use either --drafts [days] OR --submissions [days]"
)

if drafts_time_threshold := options["drafts"]:
older_than_date = timezone.now() - timedelta(days=drafts_time_threshold)

old_drafts = ApplicationSubmission.objects.filter(
status=DRAFT_STATE, draft_revision__timestamp__date__lte=older_than_date
)

if draft_count := old_drafts.count():
if interactive:
confirm = input(
f"This action will permanently delete {draft_count} draft{'s' if draft_count != 1 else ''}.\nAre you sure you want to do this?\n\nType 'yes' to continue, or 'no' to cancel: "
)
else:
confirm = "yes"

if confirm == "yes":
old_drafts.delete()
self.stdout.write(
f"{draft_count} draft{'s' if draft_count != 1 else ''} deleted."
)
else:
self.stdout.write("Draft deletion cancelled.")
else:
self.stdout.write(
f"No drafts older than {drafts_time_threshold} day{'s' if drafts_time_threshold > 1 else ''} exist. Skipping!"
)

if sub_time_threshold := options["submissions"]:
older_than_date = timezone.now() - timedelta(days=sub_time_threshold)

old_subs = (
ApplicationSubmission.objects.with_latest_update()
.filter(last_update__date__lte=older_than_date)
.exclude(status=DRAFT_STATE)
)

if sub_count := old_subs.count():
if interactive:
confirm = input(
f"This action will permanently anonymize {sub_count} submission{'s' if sub_count != 1 else ''}.\nAre you sure you want to do this?\n\nType 'yes' to continue, or 'no' to cancel: "
)
else:
confirm = "yes"

if confirm == "yes":
old_subs.delete()
self.stdout.write(
f"{sub_count} submission{'s' if sub_count != 1 else ''} anonymized."
)
else:
self.stdout.write("Submission deletion cancelled.")
else:
self.stdout.write(
f"No submissions older than {sub_time_threshold} day{'s' if sub_time_threshold > 1 else ''} exist. Skipping!"
)
3 changes: 2 additions & 1 deletion hypha/apply/funds/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from .reminders import Reminder
from .reviewer_role import ReviewerRole, ReviewerSettings
from .screening import ScreeningStatus
from .submissions import ApplicationSubmission
from .submissions import ApplicationSubmission, ApplicationSubmissionSkeleton

__all__ = [
"ApplicationForm",
"ApplicationRevision",
"ApplicationSettings",
"ApplicationSubmission",
"ApplicationSubmissionSkeleton",
"AssignedReviewers",
"Reminder",
"ReviewerRole",
Expand Down
Loading
Loading