diff --git a/project/tests/test_view_clear_db.py b/project/tests/test_view_clear_db.py index 44ccbe60..1d50ecbd 100644 --- a/project/tests/test_view_clear_db.py +++ b/project/tests/test_view_clear_db.py @@ -20,3 +20,19 @@ def test_clear_all(self): response = self.client.post(silky_reverse("cleardb"), {"clear_all": "on"}) self.assertTrue(response.status_code == 200) self.assertEqual(models.Request.objects.count(), 0) + + +class TestViewClearDBAndDeleteProfiles(TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + SilkyConfig().SILKY_AUTHENTICATION = False + SilkyConfig().SILKY_AUTHORISATION = False + SilkyConfig().SILKY_DELETE_PROFILES = True + + def test_clear_all_and_delete_profiles(self): + RequestMinFactory.create() + self.assertEqual(models.Request.objects.count(), 1) + response = self.client.post(silky_reverse("cleardb"), {"clear_all": "on"}) + self.assertTrue(response.status_code == 200) + self.assertEqual(models.Request.objects.count(), 0) diff --git a/silk/config.py b/silk/config.py index fddbf9e2..8b237340 100644 --- a/silk/config.py +++ b/silk/config.py @@ -33,7 +33,8 @@ class SilkyConfig(metaclass=Singleton): 'SILKY_JSON_ENSURE_ASCII': True, 'SILKY_ANALYZE_QUERIES': False, 'SILKY_EXPLAIN_FLAGS': None, - 'SILKY_SENSITIVE_KEYS': {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'} + 'SILKY_SENSITIVE_KEYS': {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'}, + 'SILKY_DELETE_PROFILES': False } def _setup(self): diff --git a/silk/templates/silk/clear_db.html b/silk/templates/silk/clear_db.html index a073983b..297b3fd9 100644 --- a/silk/templates/silk/clear_db.html +++ b/silk/templates/silk/clear_db.html @@ -38,7 +38,7 @@

Silk Clear DB

-
{{ msg }}
+
{{ msg|linebreaks }}
diff --git a/silk/views/clear_db.py b/silk/views/clear_db.py index 141213b3..893ee29b 100644 --- a/silk/views/clear_db.py +++ b/silk/views/clear_db.py @@ -1,9 +1,13 @@ +import os +import shutil + from django.db import transaction from django.shortcuts import render from django.utils.decorators import method_decorator from django.views.generic import View from silk.auth import login_possibly_required, permissions_possibly_required +from silk.config import SilkyConfig from silk.models import Profile, Request, Response, SQLQuery from silk.utils.data_deletion import delete_model @@ -27,4 +31,15 @@ def post(self, request, *_, **kwargs): delete_model(Request) tables = ['Response', 'SQLQuery', 'Profile', 'Request'] context['msg'] = 'Cleared data for following silk tables: {}'.format(', '.join(tables)) + + if SilkyConfig().SILKY_DELETE_PROFILES: + dir = SilkyConfig().SILKY_PYTHON_PROFILER_RESULT_PATH + for files in os.listdir(dir): + path = os.path.join(dir, files) + try: + shutil.rmtree(path) + except OSError: + os.remove(path) + context['msg'] += '\nDeleted all profiles from the directory.' + return render(request, 'silk/clear_db.html', context=context)