diff --git a/project/tests/test_config_meta.py b/project/tests/test_config_meta.py index d28c17d1..d3d4c2aa 100644 --- a/project/tests/test_config_meta.py +++ b/project/tests/test_config_meta.py @@ -23,7 +23,7 @@ def _execute_request(self): delete_all_models(Request) DataCollector().configure(Request.objects.create()) response = self._mock_response() - SilkyMiddleware()._process_response(response) + SilkyMiddleware()._process_response('', response) self.assertTrue(response.status_code == 200) objs = Request.objects.all() self.assertEqual(objs.count(), 1) diff --git a/silk/middleware.py b/silk/middleware.py index 130c7ef9..36fe0c09 100644 --- a/silk/middleware.py +++ b/silk/middleware.py @@ -28,6 +28,7 @@ def silky_reverse(name, *args, **kwargs): r = reverse(name, *args, **kwargs) return r + fpath = silky_reverse('summary') config = SilkyConfig() @@ -49,7 +50,6 @@ def _should_intercept(request): class TestMiddleware(object): - def process_response(self, request, response): return response @@ -94,10 +94,12 @@ def process_request(self, request): SQLCompiler._execute_sql = SQLCompiler.execute_sql SQLCompiler.execute_sql = execute_sql request_model = RequestModelFactory(request).construct_request_model() - DataCollector().configure(request_model) + DataCollector().configure(request_model) + else: + DataCollector().clear() @transaction.atomic() - def _process_response(self, response): + def _process_response(self, request, response): Logger.debug('Process response') with silk_meta_profiler(): collector = DataCollector() @@ -106,9 +108,12 @@ def _process_response(self, response): if silk_request: silk_response = ResponseModelFactory(response).construct_response_model() silk_response.save() + silk_request.end_time = timezone.now() + collector.finalise() else: - Logger.error( - 'No request model was available when processing response. Did something go wrong in process_request/process_view?') + Logger.error('No request model was available when processing response. ' + 'Did something go wrong in process_request/process_view?' + '\n' + str(request) + '\n\n' + str(response)) # Need to save the data outside the silk_meta_profiler # Otherwise the meta time collected in the context manager # is not taken in account @@ -118,5 +123,5 @@ def _process_response(self, response): def process_response(self, request, response): if getattr(request, 'silk_is_intercepted', False): - self._process_response(response) + self._process_response(request, response) return response diff --git a/silk/models.py b/silk/models.py index 92a98390..07a1b568 100644 --- a/silk/models.py +++ b/silk/models.py @@ -210,7 +210,7 @@ def save(self, *args, **kwargs): if not self.pk: if self.request: self.request.num_sql_queries += 1 - self.request.save() + self.request.save(update_fields=['num_sql_queries']) super(SQLQuery, self).save(*args, **kwargs) diff --git a/silk/views/requests.py b/silk/views/requests.py index 089480d0..26ee8146 100644 --- a/silk/views/requests.py +++ b/silk/views/requests.py @@ -1,4 +1,4 @@ -from django.core.context_processors import csrf +from django.template.context_processors import csrf from django.db.models import Sum from django.shortcuts import render_to_response diff --git a/silk/views/summary.py b/silk/views/summary.py index 0f8d54cd..78870c88 100644 --- a/silk/views/summary.py +++ b/silk/views/summary.py @@ -1,4 +1,4 @@ -from django.core.context_processors import csrf +from django.template.context_processors import csrf from django.db.models import Avg, Count, Sum, Max from django.shortcuts import render_to_response from django.utils.decorators import method_decorator