Exemplo n.º 1
0
def handle_on_link_shared(channel_id, message_ts, links):
    for item in links:
        path = urlsplit(item['url'])[2]
        id_type, qid = urlsplit(item['url'])[3].split('=')

        try:
            if path == '/hue/editor' and id_type == 'editor':
                doc = Document2.objects.get(id=qid)
                doc_type = 'Editor'
            elif path == '/hue/gist' and id_type == 'uuid' and ENABLE_GIST_PREVIEW.get(
            ):
                doc = _get_gist_document(uuid=qid)
                doc_type = 'Gist'
            else:
                raise PopupException(_("Cannot unfurl link"))
        except Document2.DoesNotExist:
            msg = "Document with {key}={value} does not exist".format(
                key='uuid' if id_type == 'uuid' else 'id', value=qid)
            raise PopupException(_(msg))

        payload = _make_unfurl_payload(item['url'], id_type, doc, doc_type)
        response = slack_client.chat_unfurl(channel=channel_id,
                                            ts=message_ts,
                                            unfurls=payload)
        if not response['ok']:
            raise PopupException(_("Cannot unfurl link"),
                                 detail=response["error"])
Exemplo n.º 2
0
def create_notebook(request):
    response = {'status': -1}

    editor_type = request.POST.get('type', 'notebook')
    gist_id = request.POST.get('gist')
    directory_uuid = request.POST.get('directory_uuid')

    if gist_id:
        gist_doc = _get_gist_document(uuid=gist_id)
        statement = json.loads(gist_doc.data)['statement']

        editor = make_notebook(name='',
                               description='',
                               editor_type=editor_type,
                               statement=statement,
                               is_presentation_mode=True)
    else:
        editor = Notebook()

    data = editor.get_data()

    if editor_type != 'notebook':
        data['name'] = ''
        data[
            'type'] = 'query-%s' % editor_type  # TODO: Add handling for non-SQL types

    data['directoryUuid'] = directory_uuid
    editor.data = json.dumps(data)

    response['notebook'] = editor.get_data()
    response['status'] = 0

    return JsonResponse(response)
Exemplo n.º 3
0
def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
  for item in links:
    path = urlsplit(item['url'])[2]
    id_type, qid = urlsplit(item['url'])[3].split('=')
    query_id = {'id': qid} if qid.isdigit() else {'uuid': qid}

    try:
      if path == '/hue/editor' and id_type == 'editor':
        doc = Document2.objects.get(**query_id)
        doc_type = 'query'
      elif path == '/hue/gist' and id_type == 'uuid':
        doc = _get_gist_document(**query_id)
        doc_type = 'gist'
      else:
        raise PopupException(_("Cannot unfurl link"))
    except Document2.DoesNotExist:
      msg = "Document with {key} does not exist".format(key=query_id)
      raise PopupException(_(msg))

    # Permission check for Slack user to be Hue user
    slack_user = check_slack_user_permission(host_domain, user_id)
    user = get_user(channel_id, slack_user) if not slack_user['is_bot'] else doc.owner
    doc.can_read_or_exception(user)

    request = MockRequest(user=rewrite_user(user))

    payload = _make_unfurl_payload(request, item['url'], id_type, doc, doc_type)
    try:
      slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload'])
    except Exception as e:
      raise PopupException(_("Cannot unfurl link"), detail=e)
    
    # Generate and upload result xlsx file only if result available
    if payload['file_status']:
      send_result_file(request, channel_id, message_ts, doc, 'xls')
Exemplo n.º 4
0
def handle_on_link_shared(channel_id, message_ts, links):
  for item in links:
    path = urlsplit(item['url'])[2]
    id_type, qid = urlsplit(item['url'])[3].split('=')

    try:
      if path == '/hue/editor' and id_type == 'editor':
        doc = Document2.objects.get(id=qid)
        doc_type = 'Query'
      elif path == '/hue/gist' and id_type == 'uuid' and ENABLE_GIST_PREVIEW.get():
        doc = _get_gist_document(uuid=qid)
        doc_type = 'Gist'
      else:
        raise PopupException(_("Cannot unfurl link"))
    except Document2.DoesNotExist:
      msg = "Document with {key}={value} does not exist".format(key='uuid' if id_type == 'uuid' else 'id', value=qid)
      raise PopupException(_(msg))

    # Mock request for query execution and fetch result
    user = rewrite_user(User.objects.get(username=doc.owner.username))
    request = MockRequest(user=user)

    payload = _make_unfurl_payload(request, item['url'], id_type, doc, doc_type)
    try:
      slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload'])
    except Exception as e:
      raise PopupException(_("Cannot unfurl link"), detail=e)
    
    # Generate and upload result xlsx file only if result available
    if payload['file_status']:
      send_result_file(request, channel_id, message_ts, doc, 'xls')
Exemplo n.º 5
0
Arquivo: views.py Projeto: liuerge/hue
def handle_on_link_shared(channel_id, message_ts, links):
    for item in links:
        path = urlsplit(item['url'])[2]
        id_type, qid = urlsplit(item['url'])[3].split('=')

        try:
            if path == '/hue/editor' and id_type == 'editor':
                doc = Document2.objects.get(id=qid)
            elif path == '/hue/gist' and id_type == 'uuid' and ENABLE_GIST_PREVIEW.get(
            ):
                doc = _get_gist_document(uuid=qid)
            else:
                raise PopupException(_("Cannot unfurl link"))
        except Document2.DoesNotExist:
            msg = "Document with {key}={value} does not exist".format(
                key='uuid' if id_type == 'uuid' else 'id', value=qid)
            raise PopupException(_(msg))

        doc_data = json.loads(doc.data)
        statement = doc_data['snippets'][0][
            'statement_raw'] if id_type == 'editor' else doc_data[
                'statement_raw']
        dialect = doc_data['dialect'].capitalize(
        ) if id_type == 'editor' else doc.extra.capitalize()
        created_by = doc.owner.get_full_name() or doc.owner.username

        payload = _make_unfurl_payload(item['url'], statement, dialect,
                                       created_by)
        response = slack_client.chat_unfurl(channel=channel_id,
                                            ts=message_ts,
                                            unfurls=payload)
        if not response['ok']:
            raise PopupException(_("Cannot unfurl link"),
                                 detail=response["error"])
Exemplo n.º 6
0
def editor(request, is_mobile=False, is_embeddable=False):
  editor_id = request.GET.get('editor')
  editor_type = request.GET.get('type', 'hive')
  gist_id = request.GET.get('gist')

  if editor_type == 'notebook' or request.GET.get('notebook'):
    return notebook(request)

  if editor_type == 'gist':
    gist_doc = _get_gist_document(uuid=gist_id)
    editor_type = gist_doc.extra

  if editor_id and not gist_id:  # Open existing saved editor document
    editor_type = _get_editor_type(editor_id)

  template = 'editor.mako'
  if is_mobile:
    template = 'editor_m.mako'

  return render(template, request, {
      'editor_id': editor_id or None,
      'notebooks_json': '{}',
      'is_embeddable': request.GET.get('is_embeddable', False),
      'editor_type': editor_type,
      'options_json': json.dumps({
        'languages': get_ordered_interpreters(request.user),
        'mode': 'editor',
        'is_optimizer_enabled': has_optimizer(),
        'is_wa_enabled': has_workload_analytics(),
        'is_navigator_enabled': has_catalog(request.user),
        'editor_type': editor_type,
        'mobile': is_mobile
      })
  })
Exemplo n.º 7
0
def gist_get(request):
    gist_uuid = request.GET.get('uuid')

    gist_doc = _get_gist_document(uuid=gist_uuid)

    return redirect('/hue/editor?gist=%(uuid)s&type=%(type)s' % {
        'uuid': gist_doc.uuid,
        'type': gist_doc.extra.rsplit('-')[-1]
    })
Exemplo n.º 8
0
def gist_get(request):
  gist_uuid = request.GET.get('uuid')

  gist_doc = _get_gist_document(uuid=gist_uuid)

  if ENABLE_GIST_PREVIEW.get() and 'Slackbot-LinkExpanding' in request.META.get('HTTP_USER_AGENT', ''):
    statement = json.loads(gist_doc.data)['statement_raw']
    return render(
      'unfurl_link.mako',
      request, {
        'title': _('SQL gist from %s') % (gist_doc.owner.get_full_name() or gist_doc.owner.username),
        'description': statement if len(statement) < 30 else (statement[:70] + '...'),
        'image_link': None
      }
    )
  else:
    return redirect('/hue/editor?gist=%(uuid)s&type=%(type)s' % {
      'uuid': gist_doc.uuid,
      'type': gist_doc.extra
    })
Exemplo n.º 9
0
def create_notebook(request):
    response = {'status': -1}

    editor_type = request.POST.get('type', 'notebook')
    gist_id = request.POST.get('gist')
    directory_uuid = request.POST.get('directory_uuid')
    is_blank = request.POST.get('blank', 'false') == 'true'

    if gist_id:
        gist_doc = _get_gist_document(uuid=gist_id)
        statement = json.loads(gist_doc.data)['statement']

        editor = make_notebook(name='',
                               description='',
                               editor_type=editor_type,
                               statement=statement,
                               is_presentation_mode=True)
    else:
        editor = Notebook()

        if EXAMPLES.AUTO_OPEN.get() and not is_blank:
            document = _get_dialect_example(dialect=editor_type)
            if document:
                editor = Notebook(document=document)
                editor = upgrade_session_properties(request, editor)

    data = editor.get_data()

    if editor_type != 'notebook':
        data['name'] = ''
        data[
            'type'] = 'query-%s' % editor_type  # TODO: Add handling for non-SQL types

    data['directoryUuid'] = directory_uuid
    editor.data = json.dumps(data)

    response['notebook'] = editor.get_data()
    response['status'] = 0

    return JsonResponse(response)