Ejemplo 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 = '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')
Ejemplo n.º 2
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"])
Ejemplo n.º 3
0
Archivo: views.py Proyecto: 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"])
Ejemplo n.º 4
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
    })