Skip to content

Commit

Permalink
Allow option to delete profiles (#652)
Browse files Browse the repository at this point in the history
* Update clear_db.html

Updated msg to be displayed with line breaks so new message can be shown in new line.

* Update clear_db.py

Added option to check for `SILKY_DELETE_PROFILES` set by user in settings file which if set true, all profiles will be deleted along with database clean function.

* Update config.py

Added new configuration setting SILKY_DELETE_PROFILES.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update test_view_clear_db.py

Added new test that will test delete all profile functionality.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
viralj and pre-commit-ci[bot] authored Dec 30, 2023
1 parent 667e756 commit f24f055
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
16 changes: 16 additions & 0 deletions project/tests/test_view_clear_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion silk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion silk/templates/silk/clear_db.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h2>Silk Clear DB</h2>
</div>
<button class="btn">Clear</button>
</form>
<div class="msg">{{ msg }}</div>
<div class="msg">{{ msg|linebreaks }}</div>
</div>
</div>

Expand Down
15 changes: 15 additions & 0 deletions silk/views/clear_db.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

0 comments on commit f24f055

Please sign in to comment.