From 41b3de4fcef78bd77406196edfe888d2faaa975a Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Wed, 29 Jan 2020 16:28:04 +0500 Subject: [PATCH 1/5] Moved delete model function to utils --- silk/utils/data_deletion.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 silk/utils/data_deletion.py diff --git a/silk/utils/data_deletion.py b/silk/utils/data_deletion.py new file mode 100644 index 00000000..e29e5a5f --- /dev/null +++ b/silk/utils/data_deletion.py @@ -0,0 +1,28 @@ +from django.conf import settings +from django.db import connection + + +def delete_model(model): + engine = settings.DATABASES['default']['ENGINE'] + table = model._meta.db_table + if 'mysql' in engine or 'postgresql' in engine: + # Use "TRUNCATE" on the table + with connection.cursor() as cursor: + if 'mysql' in engine: + cursor.execute("SET FOREIGN_KEY_CHECKS=0;") + cursor.execute("TRUNCATE TABLE {0}".format(table)) + cursor.execute("SET FOREIGN_KEY_CHECKS=1;") + elif 'postgres' in engine: + cursor.execute("ALTER TABLE {0} DISABLE TRIGGER USER;".format(table)) + cursor.execute("TRUNCATE TABLE {0} CASCADE".format(table)) + cursor.execute("ALTER TABLE {0} ENABLE TRIGGER USER;".format(table)) + return + + # Manually delete rows because sqlite does not support TRUNCATE and + # oracle doesn't provide good support for disabling foreign key checks + while True: + items_to_delete = list( + model.objects.values_list('pk', flat=True).all()[:1000]) + if not items_to_delete: + break + model.objects.filter(pk__in=items_to_delete).delete() From ca90594ccbfad040d5a3f4c3fcccb5ffccf7867a Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Wed, 29 Jan 2020 16:29:19 +0500 Subject: [PATCH 2/5] Used function to delete data from utils --- .../commands/silk_clear_request_log.py | 36 ++++--------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/silk/management/commands/silk_clear_request_log.py b/silk/management/commands/silk_clear_request_log.py index 166cc57a..6e2ea7e6 100644 --- a/silk/management/commands/silk_clear_request_log.py +++ b/silk/management/commands/silk_clear_request_log.py @@ -2,42 +2,18 @@ from django.core.management.base import BaseCommand from django.db import connection +from silk.utils.data_deletion import delete_model + import silk.models class Command(BaseCommand): help = "Clears silk's log of requests." - @staticmethod - def delete_model(model): - engine = settings.DATABASES['default']['ENGINE'] - table = model._meta.db_table - if 'mysql' in engine or 'postgresql' in engine: - # Use "TRUNCATE" on the table - with connection.cursor() as cursor: - if 'mysql' in engine: - cursor.execute("SET FOREIGN_KEY_CHECKS=0;") - cursor.execute("TRUNCATE TABLE {0}".format(table)) - cursor.execute("SET FOREIGN_KEY_CHECKS=1;") - elif 'postgres' in engine: - cursor.execute("ALTER TABLE {0} DISABLE TRIGGER USER;".format(table)) - cursor.execute("TRUNCATE TABLE {0} CASCADE".format(table)) - cursor.execute("ALTER TABLE {0} ENABLE TRIGGER USER;".format(table)) - return - - # Manually delete rows because sqlite does not support TRUNCATE and - # oracle doesn't provide good support for disabling foreign key checks - while True: - items_to_delete = list( - model.objects.values_list('pk', flat=True).all()[:1000]) - if not items_to_delete: - break - model.objects.filter(pk__in=items_to_delete).delete() - def handle(self, *args, **options): # Django takes a long time to traverse foreign key relations, # so delete in the order that makes it easy. - Command.delete_model(silk.models.Profile) - Command.delete_model(silk.models.SQLQuery) - Command.delete_model(silk.models.Response) - Command.delete_model(silk.models.Request) + delete_model(silk.models.Profile) + delete_model(silk.models.SQLQuery) + delete_model(silk.models.Response) + delete_model(silk.models.Request) From 6205b263316b39b64e8e44882f5c73b1ed45646d Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Wed, 29 Jan 2020 16:31:53 +0500 Subject: [PATCH 3/5] Removed unused imports --- silk/management/commands/silk_clear_request_log.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/silk/management/commands/silk_clear_request_log.py b/silk/management/commands/silk_clear_request_log.py index 6e2ea7e6..f65ac7a0 100644 --- a/silk/management/commands/silk_clear_request_log.py +++ b/silk/management/commands/silk_clear_request_log.py @@ -1,10 +1,7 @@ -from django.conf import settings from django.core.management.base import BaseCommand -from django.db import connection - -from silk.utils.data_deletion import delete_model import silk.models +from silk.utils.data_deletion import delete_model class Command(BaseCommand): From fccaa0d55cd9b5caa242cf6806c85b998514540a Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Wed, 29 Jan 2020 16:33:58 +0500 Subject: [PATCH 4/5] Used function to delete data from utils in views --- silk/views/clear_db.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/silk/views/clear_db.py b/silk/views/clear_db.py index bc1803df..e19f2420 100644 --- a/silk/views/clear_db.py +++ b/silk/views/clear_db.py @@ -5,6 +5,7 @@ from silk.auth import login_possibly_required, permissions_possibly_required from silk.models import Request, Response, SQLQuery, Profile +from silk.utils.data_deletion import delete_model class ClearDBView(View): @@ -27,7 +28,10 @@ def get(self, request, *_, **kwargs): def post(self, request, *_, **kwargs): context = {} if 'clear_all' in request.POST: - self._truncate_tables([Response, SQLQuery, Profile, Request]) + delete_model(Profile) + delete_model(SQLQuery) + delete_model(Response) + delete_model(Request) tables = ['Response', 'SQLQuery', 'Profile', 'Request'] context['msg'] = 'Cleared data for following silk tables: {0}'.format(', '.join(tables)) return render(request, 'silk/clear_db.html', context=context) From b8021eff747f737e8d5c82f8a22d65596b17decb Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Wed, 29 Jan 2020 16:34:43 +0500 Subject: [PATCH 5/5] Removed unused code --- silk/views/clear_db.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/silk/views/clear_db.py b/silk/views/clear_db.py index e19f2420..d31e9db6 100644 --- a/silk/views/clear_db.py +++ b/silk/views/clear_db.py @@ -1,4 +1,3 @@ -from django.db import connection from django.shortcuts import render from django.utils.decorators import method_decorator from django.views.generic import View @@ -10,14 +9,6 @@ class ClearDBView(View): - def _truncate_tables(self, models): - raw_query = 'TRUNCATE TABLE {0};' - truncate_query = [raw_query.format(m._meta.db_table) for m in models] - truncate_query = ' '.join(truncate_query) - query = 'SET FOREIGN_KEY_CHECKS = 0; {0} SET FOREIGN_KEY_CHECKS = 1;'.format(truncate_query) - cursor = connection.cursor() - cursor.execute(query) - @method_decorator(login_possibly_required) @method_decorator(permissions_possibly_required) def get(self, request, *_, **kwargs):