Skip to content

Commit

Permalink
Make Silk Great Again and Upgrade Dev Project (#122)
Browse files Browse the repository at this point in the history
* add migrations to def project app

* use new templates settings structure for newer django

* must past request as the first param to render

* update readme with devlopment instructions

* update header formatting

* typos

* use regular six instead of silks own six which was removed in commit 48b7416

* replace delete_all_models() with standard django query since .util no longer exists

* comment out unused import breaking the build

* bring mock_data_collector back

* Revert "replace delete_all_models() with standard django query since .util no longer exists"

This reverts commit 5a527df.

* bring back delete_all_models() for sqlite's sake

* pep8 shame
  • Loading branch information
hanleyhansen authored and avelis committed Jul 8, 2016
1 parent 5f407a9 commit 9118975
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 39 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Silk is a live profiling and inspection tool for the Django framework. Silk inte
* [Meta-Profiling](#meta-profiling)
* [Recording a fraction of requests](#recording-a-fraction-of-requests)
* [Clearing logged data](#clearing-logged-data)
* [Development](#development)

## Requirements

Expand Down Expand Up @@ -399,3 +400,32 @@ A management command will wipe out all logged data:
```bash
python manage.py silk_clear_request_log
```

## Development

Silk features a project named `project` that can be used for `silk` development. It has the `silk` code symlinked so
you can work on the sample `project` and on the `silk` package at the same time.

In order to setup local development you should first install all the dependencies for the test `project`. From the
root of the `project` directory:

```bash
pip install -r test-requirements.txt
```

You'll also need to install `silk`'s dependencies. From the root of the git repository:

```bash
pip install -r requirements.txt
```

At this point your virtual environment should have everything it needs to run both the sample `project` and
`silk` successfully.

Now from the root of the sample `project` directory start the django server

```bash
python manage.py runserver
```

Happy profiling!
28 changes: 28 additions & 0 deletions project/example_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-08 13:19
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Blind',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('photo', models.ImageField(upload_to=b'products')),
('name', models.TextField()),
('child_safe', models.BooleanField(default=False)),
],
options={
'abstract': False,
},
),
]
Empty file.
2 changes: 1 addition & 1 deletion project/example_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def do_something_long():

with silk_profile(name='Why do this take so long?'):
do_something_long()
return render('example_app/index.html', {'blinds': models.Blind.objects.all()})
return render(request, 'example_app/index.html', {'blinds': models.Blind.objects.all()})
39 changes: 16 additions & 23 deletions project/project/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import django

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Expand All @@ -8,8 +7,6 @@
DEBUG = True
DEBUG_PROPAGATE_EXCEPTIONS = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
Expand Down Expand Up @@ -96,30 +93,26 @@
if not os.path.exists(MEDIA_ROOT):
os.mkdir(MEDIA_ROOT)

TEMPLATE_DIRS = (
BASE_DIR,
)

# A tuple of template loader classes, specified as strings. Each Loader class
# knows how to import templates from a particular source.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
)

# A tuple of callables that are used to populate the context in RequestContext.
# These callables take a request object as their argument and return a dictionary of
# items to be merged into the context.
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages',
'django.contrib.auth.context_processors.auth'
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'

SILKY_META = True
SILKY_PYTHON_PROFILER = True
# SILKY_AUTHENTICATION = True
# SILKY_AUTHORISATION = True
# SILKY_AUTHORISATION = True
5 changes: 3 additions & 2 deletions project/tests/test_code_gen_curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import unittest
import subprocess

from .util import PORT, construct_echo_process

# .util has disappeared so let's comment out the import for
# now since nothing is using it and it's breaking the build
# from .util import PORT, construct_echo_process


# noinspection PyUnresolvedReferences
Expand Down
2 changes: 1 addition & 1 deletion project/tests/test_dynamic_profiling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase
from mock import patch
import silk.utils.six as six
import six

import silk
from silk.profiling.dynamic import _get_module, _get_parent_module, profile_function_or_method
Expand Down
23 changes: 23 additions & 0 deletions project/tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from mock import Mock
from silk.models import Request


def mock_data_collector():
mock = Mock()
mock.queries = []
mock.local = Mock()
r = Request()
mock.local.request = r
mock.request = r
return mock


def delete_all_models(model_class):
"""
A sqlite3-safe deletion function to avoid "django.db.utils.OperationalError: too many SQL variables"
:param model_class:
:return:
"""
while model_class.objects.count():
ids = model_class.objects.values_list('pk', flat=True)[:80]
model_class.objects.filter(pk__in=ids).delete()
2 changes: 1 addition & 1 deletion silk/views/profile_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def get(self, request, *_, **kwargs):
else:
raise e

return render('silk/profile_detail.html', context)
return render(request, 'silk/profile_detail.html', context)
4 changes: 2 additions & 2 deletions silk/views/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def _create_context(self, request, *args, **kwargs):
@method_decorator(permissions_possibly_required)

def get(self, request, *args, **kwargs):
return render('silk/profiling.html', self._create_context(request, *args, **kwargs))
return render(request, 'silk/profiling.html', self._create_context(request, *args, **kwargs))

@method_decorator(login_possibly_required)
@method_decorator(permissions_possibly_required)
def post(self, request):
filters = filters_from_request(request)
request.session[self.session_key_profile_filters] = {ident: f.as_dict() for ident, f in filters.items()}
return render('silk/profiling.html', self._create_context(request))
return render(request, 'silk/profiling.html', self._create_context(request))
2 changes: 1 addition & 1 deletion silk/views/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get(self, request, request_id):
elif typ == 'response':
Logger.debug(silk_request.response.raw_body_decoded)
body = silk_request.response.raw_body_decoded if subtyp == 'raw' else silk_request.response.body
return render('silk/raw.html', {
return render(request, 'silk/raw.html', {
'body': body
})
else:
Expand Down
2 changes: 1 addition & 1 deletion silk/views/request_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def get(self, request, request_id):
content_type=silk_request.content_type),
'request': request
}
return render('silk/request.html', context)
return render(request, 'silk/request.html', context)
5 changes: 2 additions & 3 deletions silk/views/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.generic import View
from silk.profiling.dynamic import _get_module

from silk.auth import login_possibly_required, permissions_possibly_required
from silk.models import Request
Expand Down Expand Up @@ -121,11 +120,11 @@ def _create_context(self, request):
@method_decorator(login_possibly_required)
@method_decorator(permissions_possibly_required)
def get(self, request):
return render('silk/requests.html', self._create_context(request))
return render(request, 'silk/requests.html', self._create_context(request))

@method_decorator(login_possibly_required)
@method_decorator(permissions_possibly_required)
def post(self, request):
filters = filters_from_request(request)
request.session[self.session_key_request_filters] = {ident: f.as_dict() for ident, f in filters.items()}
return render('silk/requests.html', self._create_context(request))
return render(request, 'silk/requests.html', self._create_context(request))
2 changes: 1 addition & 1 deletion silk/views/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def get(self, request, *_, **kwargs):
raise KeyError('no profile_id or request_id')
# noinspection PyUnboundLocalVariable
context['items'] = page
return render('silk/sql.html', context)
return render(request, 'silk/sql.html', context)
2 changes: 1 addition & 1 deletion silk/views/sql_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ def get(self, request, *_, **kwargs):
actual_line, code = _code(file_path, line_num)
context['code'] = code
context['actual_line'] = actual_line
return render('silk/sql_detail.html', context)
return render(request, 'silk/sql_detail.html', context)
4 changes: 2 additions & 2 deletions silk/views/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def _create_context(self, request):
@method_decorator(permissions_possibly_required)
def get(self, request):
c = self._create_context(request)
return render('silk/summary.html', c)
return render(request, 'silk/summary.html', c)

@method_decorator(login_possibly_required)
@method_decorator(permissions_possibly_required)
def post(self, request):
filters = filters_from_request(request)
request.session[self.filters_key] = {ident: f.as_dict() for ident, f in filters.items()}
return render('silk/summary.html', self._create_context(request))
return render(request, 'silk/summary.html', self._create_context(request))

0 comments on commit 9118975

Please sign in to comment.