online_docs.views: 35 total statements, 93.3% covered

Generated: Sat 2012-06-23 23:31 SGT

Source file: /Users/martin/Repos/django-online-docs/online_docs/views.py

Stats: 28 executed, 2 missed, 5 excluded, 16 ignored

  1. """Views for the ``online_docs`` app."""
  2. import os
  3. from django.conf import settings
  4. from django.core.urlresolvers import resolve
  5. from django.http import Http404
  6. from django.views.generic import TemplateView
  7. class OnlineDocsView(TemplateView):
  8. """View that displays the docs for the current URL path."""
  9. def dispatch(self, request, *args, **kwargs):
  10. self.path = request.GET.get('path')
  11. if not self.path:
  12. raise Http404
  13. return super(OnlineDocsView, self).dispatch(request, *args, **kwargs)
  14. def get_context_data(self, **kwargs):
  15. ctx = super(OnlineDocsView, self).get_context_data(**kwargs)
  16. document_name = self.get_document_name()
  17. file_path = self.get_document_file_path(document_name)
  18. document_content = self.get_document_content(file_path)
  19. ctx.update({
  20. 'docs': document_content,
  21. 'document_name': document_name,
  22. 'DEBUG': settings.DEBUG,
  23. })
  24. return ctx
  25. def get_document_name(self):
  26. url = resolve(self.path)
  27. document_name = '%s.md' % url.view_name.replace(':', '_')
  28. return document_name
  29. def get_document_file_path(self, document_name):
  30. file_path = os.path.join(settings.STATIC_ROOT, 'online_docs',
  31. document_name)
  32. return file_path
  33. def get_document_content(self, file_path):
  34. try:
  35. f = open(file_path, 'r')
  36. return f.read()
  37. except IOError:
  38. return None
  39. def get_template_names(self):
  40. if self.request.is_ajax():
  41. return ['online_docs/partials/online_docs.html', ]
  42. else:
  43. return ['online_docs/online_docs.html', ]