Test coverage report: online_docs.views

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