views.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 logging
  18. import json
  19. from django.utils.translation import ugettext as _
  20. from desktop.lib.django_util import JsonResponse, render
  21. from desktop.lib.exceptions_renderable import PopupException
  22. from desktop.models import get_cluster_config
  23. from indexer.solr_client import SolrClient
  24. from indexer.fields import FIELD_TYPES, Field
  25. from indexer.file_format import get_file_indexable_format_types
  26. from indexer.management.commands import indexer_setup
  27. from indexer.indexers.morphline_operations import OPERATORS
  28. LOG = logging.getLogger(__name__)
  29. HIVE_PRIMITIVE_TYPES = (
  30. "string", "tinyint", "smallint", "int", "bigint", "boolean", "float", "double", "decimal", "timestamp", "date", "char", "varchar"
  31. )
  32. HIVE_TYPES = HIVE_PRIMITIVE_TYPES + ("array", "map", "struct")
  33. def collections(request, is_redirect=False):
  34. if not request.user.has_hue_permission(action="access", app='indexer'):
  35. raise PopupException(_('Missing permission.'), error_code=403)
  36. return render('collections.mako', request, {
  37. 'is_embeddable': request.GET.get('is_embeddable', False),
  38. })
  39. def indexes(request, index=''):
  40. if not request.user.has_hue_permission(action="access", app='indexer'):
  41. raise PopupException(_('Missing permission.'), error_code=403)
  42. return render('indexes.mako', request, {
  43. 'is_embeddable': request.GET.get('is_embeddable', False),
  44. 'index': index
  45. })
  46. def topics(request, index=''):
  47. if not request.user.has_hue_permission(action="access", app='search'):
  48. raise PopupException(_('Missing permission.'), error_code=403)
  49. return render('topics.mako', request, {
  50. 'is_embeddable': request.GET.get('is_embeddable', False),
  51. 'index': index
  52. })
  53. def indexer(request):
  54. if not request.user.has_hue_permission(action="access", app='indexer'):
  55. raise PopupException(_('Missing permission.'), error_code=403)
  56. searcher = SolrClient(request.user)
  57. indexes = searcher.get_indexes()
  58. for index in indexes:
  59. index['isSelected'] = False
  60. return render('indexer.mako', request, {
  61. 'is_embeddable': request.GET.get('is_embeddable', False),
  62. 'indexes_json': json.dumps(indexes),
  63. 'fields_json': json.dumps([field.name for field in FIELD_TYPES]),
  64. 'operators_json': json.dumps([operator.to_dict() for operator in OPERATORS]),
  65. 'file_types_json': json.dumps([format_.format_info() for format_ in get_file_indexable_format_types()]),
  66. 'default_field_type': json.dumps(Field().to_dict())
  67. })
  68. def importer(request):
  69. prefill = {
  70. 'source_type': '',
  71. 'target_type': '',
  72. 'target_path': ''
  73. }
  74. return _importer(request, prefill)
  75. def importer_prefill(request, source_type, target_type, target_path=None):
  76. prefill = {
  77. 'source_type': source_type,
  78. 'target_type': target_type,
  79. 'target_path': target_path or ''
  80. }
  81. return _importer(request, prefill)
  82. def _importer(request, prefill):
  83. cluster_config = get_cluster_config(request.user)
  84. source_type = request.GET.get('sourceType') or cluster_config['default_sql_interpreter']
  85. return render('importer.mako', request, {
  86. 'is_embeddable': request.GET.get('is_embeddable', False),
  87. 'fields_json': json.dumps({'solr': [field.name for field in FIELD_TYPES], 'hive': HIVE_TYPES, 'hivePrimitive': HIVE_PRIMITIVE_TYPES}),
  88. 'operators_json': json.dumps([operator.to_dict() for operator in OPERATORS]),
  89. 'file_types_json': json.dumps([format_.format_info() for format_ in get_file_indexable_format_types()]),
  90. 'default_field_type': json.dumps(Field().to_dict()),
  91. 'prefill': json.dumps(prefill),
  92. 'source_type': source_type
  93. })
  94. def install_examples(request, is_redirect=False):
  95. result = {'status': -1, 'message': ''}
  96. if request.method != 'POST':
  97. result['message'] = _('A POST request is required.')
  98. else:
  99. try:
  100. data = request.POST.get('data')
  101. indexer_setup.Command().handle(data=data)
  102. result['status'] = 0
  103. except Exception as e:
  104. LOG.exception(e)
  105. result['message'] = str(e)
  106. return JsonResponse(result)