예제 #1
0
def snippet_api(request):
    content = request.POST.get('content', '').strip()
    lexer = request.REQUEST.get('lexer', LEXER_DEFAULT).strip()
    filename = request.REQUEST.get('filename', '').strip()
    expires = request.REQUEST.get('expires', '').strip()
    format = request.REQUEST.get('format', 'default').strip()

    if not content:
        return HttpResponseBadRequest('No content given')

    # We need at least a lexer or a filename
    if not lexer and not filename:
        return HttpResponseBadRequest(
            'No lexer or filename given. Unable to '
            'determine a highlight. Valid lexers are: %s' %
            ', '.join(LEXER_KEYS))

    # A lexer is given, check if its valid at all
    if lexer and lexer not in LEXER_KEYS:
        return HttpResponseBadRequest(
            'Invalid lexer "%s" given. Valid lexers are: %s' %
            (lexer, ', '.join(LEXER_KEYS)))

    # No lexer is given, but we have a filename, try to get the lexer out of it.
    # In case Pygments cannot determine the lexer of the filename, we fallback
    # to 'plain' code.
    if not lexer and filename:
        try:
            lexer_cls = get_lexer_for_filename(filename)
            lexer = lexer_cls.aliases[0]
        except (ClassNotFound, IndexError):
            lexer = PLAIN_CODE

    if expires:
        expire_options = [str(i) for i in dict(EXPIRE_CHOICES).keys()]
        if not expires in expire_options:
            return HttpResponseBadRequest('Invalid expire choice "{}" given. '
                                          'Valid values are: {}'.format(
                                              expires,
                                              ', '.join(expire_options)))
        expires, expire_type = get_expire_values(expires)
    else:
        expires = datetime.datetime.now() + datetime.timedelta(seconds=60 *
                                                               60 * 24 * 30)
        expire_type = Snippet.EXPIRE_TIME

    s = Snippet.objects.create(
        content=content,
        lexer=lexer,
        expires=expires,
        expire_type=expire_type,
    )
    s.save()

    if not format in FORMAT_MAPPING:
        response = _format_default(s)
    else:
        response = FORMAT_MAPPING[format](s)

    return HttpResponse(response)
예제 #2
0
파일: views.py 프로젝트: mozfreddyb/dpaste
    def post(self, request, *args, **kwargs):
        content = request.POST.get('content', '')
        lexer = request.POST.get('lexer', highlight.LEXER_DEFAULT).strip()
        filename = request.POST.get('filename', '').strip()
        expires = request.POST.get('expires', '').strip()
        response_format = request.POST.get('format', 'default').strip()

        if not content.strip():
            return HttpResponseBadRequest('No content given')

        # We need at least a lexer or a filename
        if not lexer and not filename:
            return HttpResponseBadRequest(
                'No lexer or filename given. Unable to '
                'determine a highlight. Valid lexers are: %s' %
                ', '.join(highlight.LEXER_KEYS))

        # A lexer is given, check if its valid at all
        if lexer and lexer not in highlight.LEXER_KEYS:
            return HttpResponseBadRequest(
                'Invalid lexer "%s" given. Valid lexers are: %s' %
                (lexer, ', '.join(highlight.LEXER_KEYS)))

        # No lexer is given, but we have a filename, try to get the lexer
        #  out of it. In case Pygments cannot determine the lexer of the
        # filename, we fallback to 'plain' code.
        if not lexer and filename:
            try:
                lexer_cls = get_lexer_for_filename(filename)
                lexer = lexer_cls.aliases[0]
            except (ClassNotFound, IndexError):
                lexer = config.PLAIN_CODE_SYMBOL

        if expires:
            expire_options = [str(i) for i in dict(config.EXPIRE_CHOICES)]
            if expires not in expire_options:
                return HttpResponseBadRequest(
                    'Invalid expire choice "{}" given. Valid values are: {}'.
                    format(expires, ', '.join(expire_options)))
            expires, expire_type = get_expire_values(expires)
        else:
            expires = datetime.datetime.now() + datetime.timedelta(seconds=60 *
                                                                   60 * 24)
            expire_type = Snippet.EXPIRE_TIME

        snippet = Snippet.objects.create(
            content=content,
            lexer=lexer,
            expires=expires,
            expire_type=expire_type,
        )

        # Custom formatter for the API response
        formatter = getattr(self, '_format_{0}'.format(response_format), None)
        if callable(formatter):
            return HttpResponse(formatter(snippet))

        # Otherwise use the default one.
        return HttpResponse(self._format_default(snippet))
예제 #3
0
파일: views.py 프로젝트: apaisp/dpaste
    def post(self, request, *args, **kwargs):
        content = request.POST.get('content', '')
        lexer = request.POST.get('lexer', highlight.LEXER_DEFAULT).strip()
        filename = request.POST.get('filename', '').strip()
        expires = request.POST.get('expires', '').strip()
        response_format = request.POST.get('format', 'default').strip()

        if not content.strip():
            return HttpResponseBadRequest('No content given')

        # We need at least a lexer or a filename
        if not lexer and not filename:
            return HttpResponseBadRequest(
                'No lexer or filename given. Unable to '
                'determine a highlight. Valid lexers are: %s'
                % ', '.join(highlight.LEXER_KEYS)
            )

        # A lexer is given, check if its valid at all
        if lexer and lexer not in highlight.LEXER_KEYS:
            return HttpResponseBadRequest(
                'Invalid lexer "%s" given. Valid lexers are: %s'
                % (lexer, ', '.join(highlight.LEXER_KEYS))
            )

        # No lexer is given, but we have a filename, try to get the lexer
        #  out of it. In case Pygments cannot determine the lexer of the
        # filename, we fallback to 'plain' code.
        if not lexer and filename:
            try:
                lexer_cls = get_lexer_for_filename(filename)
                lexer = lexer_cls.aliases[0]
            except (ClassNotFound, IndexError):
                lexer = config.PLAIN_CODE_SYMBOL

        if expires:
            expire_options = [str(i) for i in dict(config.EXPIRE_CHOICES).keys()]
            if expires not in expire_options:
                return HttpResponseBadRequest(
                    'Invalid expire choice "{}" given. Valid values are: {}'.format(
                        expires, ', '.join(expire_options)
                    )
                )
            expires, expire_type = get_expire_values(expires)
        else:
            expires = datetime.datetime.now() + datetime.timedelta(seconds=60 * 60 * 24)
            expire_type = Snippet.EXPIRE_TIME

        snippet = Snippet.objects.create(
            content=content, lexer=lexer, expires=expires, expire_type=expire_type
        )

        # Custom formatter for the API response
        formatter = getattr(self, '_format_{0}'.format(response_format), None)
        if callable(formatter):
            return HttpResponse(formatter(snippet))

        # Otherwise use the default one.
        return HttpResponse(self._format_default(snippet))
예제 #4
0
파일: views.py 프로젝트: MrTomato8/dpaste
def snippet_api(request):
    content = request.POST.get('content', '').strip()
    lexer = request.REQUEST.get('lexer', LEXER_DEFAULT).strip()
    filename = request.REQUEST.get('filename', '').strip()
    expires = request.REQUEST.get('expires', '').strip()
    format = request.REQUEST.get('format', 'default').strip()

    if not content:
        return HttpResponseBadRequest('No content given')

    # We need at least a lexer or a filename
    if not lexer and not filename:
        return HttpResponseBadRequest('No lexer or filename given. Unable to '
            'determine a highlight. Valid lexers are: %s' % ', '.join(LEXER_KEYS))

    # A lexer is given, check if its valid at all
    if lexer and lexer not in LEXER_KEYS:
        return HttpResponseBadRequest('Invalid lexer "%s" given. Valid lexers are: %s' % (
            lexer, ', '.join(LEXER_KEYS)))

    # No lexer is given, but we have a filename, try to get the lexer out of it.
    # In case Pygments cannot determine the lexer of the filename, we fallback
    # to 'plain' code.
    if not lexer and filename:
        try:
            lexer_cls = get_lexer_for_filename(filename)
            lexer = lexer_cls.aliases[0]
        except (ClassNotFound, IndexError):
            lexer = PLAIN_CODE

    if expires:
        expire_options = [str(i) for i in dict(EXPIRE_CHOICES).keys()]
        if not expires in expire_options:
            return HttpResponseBadRequest('Invalid expire choice "{}" given. '
                'Valid values are: {}'.format(expires, ', '.join(expire_options)))
        expires, expire_type = get_expire_values(expires)
    else:
        expires = datetime.datetime.now() + datetime.timedelta(seconds=60 * 60 * 24 * 30)
        expire_type = Snippet.EXPIRE_TIME

    s = Snippet.objects.create(
        content=content,
        lexer=lexer,
        expires=expires,
        expire_type=expire_type,
    )
    s.save()

    if not format in FORMAT_MAPPING:
        response = _format_default(s)
    else:
        response = FORMAT_MAPPING[format](s)

    return HttpResponse(response)