views.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import json
  2. from django.conf import settings
  3. from django.http import Http404
  4. from django.views.generic import TemplateView
  5. from schema_graph.schema import get_schema
  6. class Schema(TemplateView):
  7. template_name = "schema_graph/schema.html"
  8. def access_permitted(self):
  9. """
  10. When this returns True, the schema graph page is accessible.
  11. We look for the setting `SCHEMA_GRAPH_VISIBLE`, and fall back to `DEBUG`.
  12. To control this on a per-request basis, override this function in a subclass.
  13. The request will be accessible using `self.request`.
  14. """
  15. return getattr(settings, "SCHEMA_GRAPH_VISIBLE", settings.DEBUG)
  16. def dispatch(self, request):
  17. if not self.access_permitted():
  18. raise Http404()
  19. return super().dispatch(request)
  20. def get_context_data(self, **kwargs):
  21. schema = get_schema()
  22. kwargs.update(
  23. {
  24. "abstract_models": json.dumps(schema.abstract_models),
  25. "models": json.dumps(schema.models),
  26. "foreign_keys": json.dumps(schema.foreign_keys),
  27. "many_to_manys": json.dumps(schema.many_to_manys),
  28. "one_to_ones": json.dumps(schema.one_to_ones),
  29. "inheritance": json.dumps(schema.inheritance),
  30. "proxies": json.dumps(schema.proxies),
  31. }
  32. )
  33. return super(Schema, self).get_context_data(**kwargs)