From 6da9a500c31e888b2d28f09bd48093c4ab3e81fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 5 Jul 2021 09:46:51 +0200
Subject: [PATCH] misc: remove "anonymise forms" action from admin sidebar
 (#55231)

---
 tests/admin_pages/test_form.py | 68 -----------------------------
 wcs/admin/forms.py             | 78 ----------------------------------
 2 files changed, 146 deletions(-)

diff --git a/tests/admin_pages/test_form.py b/tests/admin_pages/test_form.py
index 3070976bc..6338f8346 100644
--- a/tests/admin_pages/test_form.py
+++ b/tests/admin_pages/test_form.py
@@ -1,6 +1,5 @@
 # -*- coding: utf-8 -*-
 
-import datetime
 import io
 import os
 import re
@@ -2324,73 +2323,6 @@ def test_form_legacy_int_id(pub):
     assert resp.forms[0]['role_id'].value == role.id
 
 
-def test_form_anonymise(pub):
-    create_superuser(pub)
-    create_role(pub)
-
-    FormDef.wipe()
-    formdef = FormDef()
-    formdef.name = 'form title'
-    formdef.fields = []
-    formdef.store()
-
-    data_class = formdef.data_class()
-    data_class.wipe()
-    for i in range(10):
-        formdata = data_class()
-        formdata.just_created()
-        if i < 3:
-            formdata.status = 'wf-new'
-        else:
-            formdata.status = 'wf-rejected'
-        formdata.store()
-
-    app = login(get_app(pub))
-    resp = app.get('/backoffice/forms/1/')
-    resp = resp.click(href='anonymise')
-    resp = resp.form.submit('cancel')
-    assert len([x for x in formdef.data_class().select() if x.anonymised]) == 0
-    assert resp.location == 'http://example.net/backoffice/forms/1/'
-
-    resp = app.get('/backoffice/forms/1/')
-    resp = resp.click(href='anonymise')
-    resp = resp.form.submit('submit')
-    assert resp.location == 'http://example.net/backoffice/forms/1/'
-    assert len([x for x in formdef.data_class().select() if x.anonymised]) == 0
-
-    resp = app.get('/backoffice/forms/1/')
-    resp = resp.click(href='anonymise')
-    resp.form['before_request_date'].value = (
-        datetime.datetime.today() + datetime.timedelta(days=1)
-    ).strftime('%Y-%m-%d')
-    resp = resp.form.submit('submit')
-    assert resp.location == 'http://example.net/backoffice/forms/1/'
-    assert len([x for x in formdef.data_class().select() if x.anonymised]) == 7
-
-    data_class.wipe()
-    for i in range(110):
-        formdata = data_class()
-        formdata.just_created()
-        if i < 3:
-            formdata.status = 'wf-rejected'
-        else:
-            formdata.status = 'wf-finished'
-        formdata.store()
-
-    resp = app.get('/backoffice/forms/1/')
-    resp = resp.click(href='anonymise')
-    resp.form['before_request_date'].value = (
-        datetime.datetime.today() + datetime.timedelta(days=1)
-    ).strftime('%Y-%m-%d')
-    resp.form['endpoints$elementfinished'].checked = False
-    resp = resp.form.submit('submit')
-    assert '?job=' in resp.location
-    resp = resp.follow()
-    assert 'job-status' in resp.text
-    assert 'completed' in resp.text
-    assert len([x for x in formdef.data_class().select() if x.anonymised]) == 3
-
-
 def test_form_public_url(pub):
     create_superuser(pub)
     create_role(pub)
diff --git a/wcs/admin/forms.py b/wcs/admin/forms.py
index c96795fbe..d91418a1a 100644
--- a/wcs/admin/forms.py
+++ b/wcs/admin/forms.py
@@ -16,7 +16,6 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, see <http://www.gnu.org/licenses/>.
 
-import datetime
 import difflib
 import io
 import tarfile
@@ -866,7 +865,6 @@ class FormDefPage(Directory):
         if get_publisher().snapshot_class:
             r += htmltext('<li><a rel="popup" href="history/save">%s</a></li>') % _('Save snapshot')
             r += htmltext('<li><a href="history/">%s</a></li>') % _('History')
-        r += htmltext('<li><a href="anonymise">%s</a></li>') % _('Anonymise forms')
         if not get_publisher().is_using_postgresql():
             r += htmltext('<li><a href="archive">%s</a></li>') % _('Archive')
         r += htmltext('</ul>')
@@ -1575,82 +1573,6 @@ class FormDefPage(Directory):
         )
         return job.file_content
 
-    def anonymise(self):
-        endpoints = []
-        for status in self.formdef.workflow.get_endpoint_status():
-            endpoints.append((str(status.id), status.name, str(status.id)))
-
-        form = Form(enctype='multipart/form-data')
-        form.add(
-            DateWidget,
-            'before_request_date',
-            title=_('Forms ended before'),
-            value=datetime.date.today() - datetime.timedelta(30),
-            required=True,
-        )
-        form.add(
-            CheckboxesWidget,
-            'endpoints',
-            title=_('Status of the forms to anonymise'),
-            value=[endpoint[0] for endpoint in endpoints],
-            options=endpoints,
-            inline=False,
-            required=True,
-        )
-        form.add_submit('submit', _('Submit'))
-        form.add_submit('cancel', _('Cancel'))
-
-        if form.get_widget('cancel').parse():
-            return redirect('.')
-        if not form.is_submitted() or form.has_errors():
-            get_response().breadcrumb.append(('anonymise', _('Anonymise')))
-            html_top('forms', title=_('Anonymise Forms'))
-            r = TemplateIO(html=True)
-            r += htmltext('<h2>%s</h2>') % _('Anonymise Forms')
-            r += htmltext('<p>%s</p>' % _("You are about to irrevocably anonymise forms."))
-            r += form.render()
-            return r.getvalue()
-        else:
-            return self.anonymise_submit(form)
-
-    def anonymise_submit(self, form):
-        class Anonymiser:
-            def __init__(self, formdef, status_ids, before_date):
-                self.formdef = formdef
-                self.status_ids = ["wf-%s" % id for id in status_ids]
-                self.before_date = before_date
-
-            def anonymise(self, job=None):
-                for formdata in self.formdef.data_class().select():
-                    if formdata.anonymised:
-                        continue
-                    if formdata.status not in self.status_ids:
-                        continue
-                    if (formdata.evolution and formdata.evolution[-1].time >= self.before_date) or (
-                        formdata.receipt_time >= self.before_date
-                    ):
-                        continue
-                    formdata.anonymise()
-
-        before_date = form.get_widget('before_request_date').parse()
-        before_date = time.strptime(before_date, misc.date_format())
-        status_ids = form.get_widget('endpoints').parse()
-        count = self.formdef.data_class().count()
-        anonymiser = Anonymiser(self.formdef, status_ids, before_date)
-        if count > 100:  # Arbitrary threshold
-            job = get_response().add_after_job(
-                _('Anonymising forms'),
-                anonymiser.anonymise,
-                done_action_url=self.formdef.get_admin_url(),
-                done_action_label=_('Back'),
-            )
-            job.store()
-            return redirect(job.get_processing_url())
-        else:
-            anonymiser.anonymise()
-
-        return redirect('.')
-
     def enable(self):
         self.formdef.disabled = False
         self.formdef.store(comment=_('Enable'))
-- 
2.32.0

