├── soet ├── __init__.py ├── models.py └── middleware.py ├── requirements.txt ├── MANIFEST.in ├── soet.png ├── LICENSE ├── README.rst ├── setup.py └── .gitignore /soet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.7.0 -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.rst -------------------------------------------------------------------------------- /soet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vitorfs/soet/HEAD/soet.png -------------------------------------------------------------------------------- /soet/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Vitor Freitas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | StackOverflow Exception Troubleshooting 2 | ======================================= 3 | 4 | A simple Django Middleware for Exception Troubleshooting. It is meant to be used in debug mode only. 5 | 6 | In a nutshell, the Middleware intercepts a exception thrown by a view and look up for the three most relevant questions 7 | on StackOverflow and print the result to the console. 8 | 9 | Quick Start 10 | ----------- 11 | 12 | **1. Install using pip:** 13 | 14 | .. code-block:: console 15 | 16 | pip install django-soet 17 | 18 | **2. Include "soet" to your INSTALLED_APPS:** 19 | 20 | .. code-block:: python 21 | 22 | INSTALLED_APPS = [ 23 | ... 24 | 'soet', 25 | ] 26 | 27 | **3. Include "StackOverflowMiddleware" to your MIDDLEWARE_CLASSES:** 28 | 29 | .. code-block:: python 30 | 31 | MIDDLEWARE_CLASSES = ( 32 | ... 33 | 'soet.middleware.StackOverflowMiddleware', 34 | ) 35 | 36 | **4. Make sure you are running your project with DEBUG=True.** 37 | 38 | **5. Start your development server and wait for the view exceptions (or not).** 39 | 40 | Preview 41 | ------- 42 | 43 | This is how it looks like in your Terminal: 44 | 45 | .. image:: https://github.com/vitorfs/soet/raw/master/soet.png 46 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import find_packages, setup 3 | 4 | with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme: 5 | README = readme.read() 6 | 7 | # allow setup.py to be run from any path 8 | os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) 9 | 10 | setup( 11 | name='django-soet', 12 | version='1.0', 13 | packages=find_packages(), 14 | install_requires=[ 15 | 'requests', 16 | ], 17 | include_package_data=True, 18 | license='MIT License', 19 | description='A simple Django Middleware for Exception Troubleshooting.', 20 | long_description=README, 21 | url='https://github.com/vitorfs/soet/', 22 | author='Vitor Freitas', 23 | author_email='vitor@freitas.com', 24 | classifiers=[ 25 | 'Environment :: Web Environment', 26 | 'Framework :: Django', 27 | 'Framework :: Django :: 1.9', 28 | 'Intended Audience :: Developers', 29 | 'License :: OSI Approved :: MIT License', 30 | 'Operating System :: OS Independent', 31 | 'Programming Language :: Python', 32 | 'Programming Language :: Python :: 2', 33 | 'Programming Language :: Python :: 2.7', 34 | 'Topic :: Internet :: WWW/HTTP', 35 | 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 36 | ], 37 | ) 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | 91 | .DS_Store 92 | -------------------------------------------------------------------------------- /soet/middleware.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import re 4 | import requests 5 | import json 6 | from HTMLParser import HTMLParser 7 | 8 | from django.conf import settings 9 | 10 | 11 | GRID_LINE = '+' + (78 * '-') + '+' 12 | 13 | 14 | def break_string(string, every=76): 15 | lines = [] 16 | for i in xrange(0, len(string), every): 17 | lines.append(string[i:i+every]) 18 | return lines 19 | 20 | 21 | def print_string(string): 22 | lines = break_string(string) 23 | for line in lines: 24 | print u'| {}'.format(line) + ((76 - len(line)) * ' ') + ' |' 25 | 26 | 27 | class StackOverflowMiddleware(object): 28 | 29 | def __init__(self): 30 | self.headers = { 31 | 'User-Agent': 'github.com/vitorfs/seot' 32 | } 33 | self.url = 'https://api.stackexchange.com/2.2/search' 34 | self.default_params = { 35 | 'order': 'desc', 36 | 'sort': 'votes', 37 | 'site': 'stackoverflow', 38 | 'pagesize': 3, 39 | 'filter': '!*1SgQGDOL9bUjMgbu_yYx4IC-MQSUH*aDX9WRdjjI' 40 | } 41 | 42 | def get_questions(self, intitle, tagged): 43 | query_params = { 'tagged': tagged, 'intitle': intitle } 44 | params = dict(self.default_params.items() + query_params.items()) 45 | r = requests.get(self.url, params=params, headers=self.headers) 46 | questions = r.json() 47 | return questions 48 | 49 | def process_exception(self, request, exception): 50 | if settings.DEBUG: 51 | intitle = u'{}: {}'.format(exception.__class__.__name__, exception.message) 52 | questions = self.get_questions(intitle, 'python;django') 53 | 54 | if len(questions['items']) == 0: 55 | message = exception.message.split("'")[0] 56 | intitle = u'{}: {}'.format(exception.__class__.__name__, message) 57 | questions = self.get_questions(intitle, 'python;django') 58 | 59 | if len(questions['items']) == 0: 60 | intitle = exception.__class__.__name__ 61 | questions = self.get_questions(intitle, 'django') 62 | 63 | count = 0 64 | 65 | for question in reversed(questions['items']): 66 | if 'answers' in question and len(question['answers']) > 0: 67 | print '\n' + GRID_LINE 68 | body = question['body_markdown'] 69 | lines = body.splitlines() 70 | print_string('Question: ') 71 | print_string(' ') 72 | for line in lines: 73 | text = HTMLParser().unescape(re.sub('\s+', ' ', line)) 74 | print_string(text) 75 | 76 | print GRID_LINE 77 | 78 | best_answer = None 79 | for answer in question['answers']: 80 | if best_answer == None or answer['score'] > best_answer['score']: 81 | best_answer = answer 82 | if best_answer != None: 83 | answer_body = best_answer['body_markdown'] 84 | answer_lines = answer_body.splitlines() 85 | print_string('Best Answer: ') 86 | print_string(' ') 87 | for line in answer_lines: 88 | text = HTMLParser().unescape(re.sub('\s+', ' ', line)) 89 | print_string(text) 90 | 91 | print GRID_LINE 92 | print_string('Score: {} / Views: {} / Answers: {}'.format( 93 | question['score'], 94 | question['view_count'], 95 | question['answer_count'] 96 | )) 97 | print_string('Tags: {}'.format(', '.join(question['tags']))) 98 | print GRID_LINE 99 | print_string(u'Title: {}'.format(HTMLParser().unescape(question['title']))) 100 | print GRID_LINE 101 | link = 'Link: http://stackoverflow.com/questions/{}'.format(question['question_id']) 102 | print_string(link) 103 | print GRID_LINE 104 | 105 | count += 1 106 | 107 | if count == 0: 108 | print '\n' + GRID_LINE 109 | print_string('No result found.') 110 | print GRID_LINE 111 | 112 | print '\n' + GRID_LINE 113 | print_string(u'Exception: {}'.format(exception.__class__.__name__)) 114 | print_string(u'Message: {}'.format(exception.message)) 115 | print GRID_LINE 116 | 117 | print '' 118 | 119 | return None 120 | --------------------------------------------------------------------------------