Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merging "merge of rob's branch and some JSON view methods I added for…

… a query builder." from stj/master.
  • Loading branch information...
commit cbe44179c02c3a87e99249290bf45fe36fa5624c 2 parents b0bb312 + cd169f3
stj stj authored fitzgen committed
6 .gitignore
View
@@ -1,3 +1,9 @@
*.pyc
*.DS_Store
*~
+*.egg-info
+MANIFEST
+build
+dist
+docs/_build
+docs/_static
13 query_analyzer/decorators.py
View
@@ -0,0 +1,13 @@
+from django.http import HttpResponse
+from django.utils import simplejson
+from functools import wraps
+
+
+def json_view(func):
+ @wraps
+ def wrapper(*args, **kwargs):
+ content = func(*args, **kwargs)
+ response = HttpResponse(content_type="application/json")
+ simplejson.dump(content, response)
+ return response
+ return wrapper
2  query_analyzer/urls.py
View
@@ -2,5 +2,7 @@
urlpatterns = patterns('query_analyzer.views',
url(r'^analyze/$', 'analyze'),
+ url(r'^models/$', 'model_select'),
+ url(r'^model/(?P<app_label>\w+)/(?P<model_name>\w+)/$', 'model_details'),
)
25 query_analyzer/views.py
View
@@ -1,10 +1,14 @@
from django.conf import settings
from django.db import connection
-from django.http import HttpResponseBadRequest
+from django.db.models import get_model, get_models
+from django.db.models.query_utils import Q
+from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils import simplejson
from django.utils.hashcompat import sha_constructor
+from itertools import chain
+from query_analyzer.decorators import json_view
class InvalidSQLError(Exception):
def __init__(self, value):
@@ -59,3 +63,22 @@ def analyze(request):
return render_to_response('query_analyzer/analyze.html',
context,
context_instance=RequestContext(request))
+
+@json_view
+def model_select(request):
+ content = {
+ 'models': [(model._meta.app_label, model._meta.module_name) for model in get_models()],
+ }
+ return content
+
+
+@json_view
+def model_details(request, app_label, model_name):
+ model = get_model(app_label, model_name)
+ content = {
+ 'managers': [(id, name) for id, name, manager in chain(model._meta.concrete_managers,
+ model._meta.abstract_managers)],
+ 'fields': model._meta.get_all_field_names(),
+ 'related_fields': [rel.get_accessor_name() for rel in model._meta.get_all_related_objects()],
+ }
+ return content
Please sign in to comment.
Something went wrong with that request. Please try again.