views.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import json
  18. import logging
  19. from django.core.urlresolvers import reverse
  20. from django.utils.translation import ugettext as _
  21. from desktop.lib.django_util import render, JsonResponse
  22. from desktop.lib.json_utils import JSONEncoderForHTML
  23. from desktop.models import Document2, Document
  24. from notebook.decorators import check_document_access_permission, check_document_modify_permission
  25. from notebook.connectors.base import Notebook, get_api
  26. from notebook.management.commands.notebook_setup import Command
  27. from notebook.connectors.spark_shell import SparkApi
  28. from notebook.conf import get_interpreters
  29. LOG = logging.getLogger(__name__)
  30. @check_document_access_permission()
  31. def notebook(request):
  32. notebook_id = request.GET.get('notebook')
  33. if notebook_id:
  34. notebook = Notebook(document=Document2.objects.get(id=notebook_id))
  35. else:
  36. notebook = Notebook()
  37. autocomplete_base_url = ''
  38. try:
  39. autocomplete_base_url = reverse('beeswax:api_autocomplete_databases', kwargs={})
  40. except:
  41. LOG.exception('failed to get autocomplete base url')
  42. is_yarn_mode = False
  43. try:
  44. from spark.conf import LIVY_SERVER_SESSION_KIND
  45. is_yarn_mode = LIVY_SERVER_SESSION_KIND.get()
  46. except:
  47. LOG.exception('Spark is not enabled')
  48. return render('notebook.mako', request, {
  49. 'notebooks_json': json.dumps([notebook.get_data()]),
  50. 'options_json': json.dumps({
  51. 'languages': get_interpreters(request.user),
  52. 'session_properties': SparkApi.PROPERTIES
  53. }),
  54. 'autocomplete_base_url': autocomplete_base_url,
  55. 'is_yarn_mode': is_yarn_mode
  56. })
  57. @check_document_access_permission()
  58. def editor(request):
  59. editor_id = request.GET.get('editor')
  60. if editor_id:
  61. editor = Notebook(document=Document2.objects.get(id=editor_id))
  62. else:
  63. editor = Notebook()
  64. data = editor.get_data()
  65. data['name'] = 'Hive Query'
  66. data['type'] = 'query'
  67. editor.data = json.dumps(data)
  68. autocomplete_base_url = ''
  69. try:
  70. autocomplete_base_url = reverse('beeswax:api_autocomplete_databases', kwargs={})
  71. except:
  72. LOG.exception('failed to get autocomplete base url')
  73. return render('editor.mako', request, {
  74. 'notebooks_json': json.dumps([editor.get_data()]),
  75. 'options_json': json.dumps({
  76. 'languages': [{"name": "Hive SQL", "type": "hive"}]
  77. }),
  78. 'autocomplete_base_url': autocomplete_base_url,
  79. })
  80. def editor_hive(request):
  81. return editor(request)
  82. def new(request):
  83. return notebook(request)
  84. def notebooks(request):
  85. notebooks = [d.content_object.to_dict() for d in Document.objects.get_docs(request.user, Document2, extra='notebook') if not d.content_object.is_history]
  86. return render('notebooks.mako', request, {
  87. 'notebooks_json': json.dumps(notebooks, cls=JSONEncoderForHTML)
  88. })
  89. @check_document_modify_permission()
  90. def delete(request):
  91. notebooks = json.loads(request.POST.get('notebooks', '[]'))
  92. for notebook in notebooks:
  93. doc2 = Document2.objects.get(uuid=notebook['uuid'])
  94. doc = doc2.doc.get()
  95. doc.can_write_or_exception(request.user)
  96. doc.delete()
  97. doc2.delete()
  98. return JsonResponse({})
  99. @check_document_access_permission()
  100. def copy(request):
  101. notebooks = json.loads(request.POST.get('notebooks', '[]'))
  102. for notebook in notebooks:
  103. doc2 = Document2.objects.get(uuid=notebook['uuid'])
  104. doc = doc2.doc.get()
  105. name = doc2.name + '-copy'
  106. doc2 = doc2.copy(name=name, owner=request.user)
  107. doc.copy(content_object=doc2, name=name, owner=request.user)
  108. return JsonResponse({})
  109. @check_document_access_permission()
  110. def download(request):
  111. notebook = json.loads(request.POST.get('notebook', '{}'))
  112. snippet = json.loads(request.POST.get('snippet', '{}'))
  113. file_format = request.POST.get('format', 'csv')
  114. return get_api(request.user, snippet, request.fs, request.jt).download(notebook, snippet, file_format)
  115. def install_examples(request):
  116. response = {'status': -1, 'message': ''}
  117. if request.method == 'POST':
  118. try:
  119. Command().handle(user=request.user)
  120. response['status'] = 0
  121. except Exception, err:
  122. LOG.exception(err)
  123. response['message'] = str(err)
  124. else:
  125. response['message'] = _('A POST request is required.')
  126. return JsonResponse(response)