Esempio n. 1
0
def save(request):
    """homepage and save pad page"""
    try:
        data = unicode(request.params['data'])
    except:
        data = u''

    if data:
        if request.params['semail'] != '': return HTTPNotFound()

        pad = get_pad(request.params['pad_id'])

        if pad != None:
            # this is an edit of an existing pad.
            if request.remote_addr == pad.ip_addr:
                pad.data = data
        else:
            # this is an new or clone of an existing pad.
            syntax = guess_lexer_name(data)
            pad = Pad(str(uuid4()), data, syntax, request.remote_addr)

        DBSession.add(pad)
        DBSession.flush()

        return HTTPFound(location='/' + str(pad.id) + '/' + pad.uri)

    return {'title': 'Add a pad', 'pad_id': '', 'data': data}
Esempio n. 2
0
def clone(request):
    """clone the given pad"""
    # prettier varible
    pad_id = request.matchdict['id']
    # query for the pad object by id
    pad = get_pad(pad_id)

    if pad == None:  # redirect home if invalid id
        return HTTPFound(location='/')

    return {'title': 'Clone pad', 'pad_id': '', 'data': pad.data}
Esempio n. 3
0
def raw(request):
    """show the raw text pad"""
    # prettier varible
    pad_id = request.matchdict['id']
    # query for the pad object by id
    pad = get_pad(pad_id)

    if pad == None:  # redirect home if invalid id
        return HTTPFound(location='/')

    try:  # make sure 'uri' key in matchdict
        pad_id = request.matchdict['uri']
    except KeyError:
        return HTTPFound(location='/' + str(pad.id) + '/' + pad.uri + '/raw')

    return pad.data
Esempio n. 4
0
def show(request):
    """show the pad"""
    # prettier varible
    pad_id = request.matchdict['id']
    # query for the pad object by id
    pad = get_pad(pad_id)

    if pad == None:  # redirect home if invalid id
        return HTTPFound(location='/')

    try:  # make sure 'uri' key in matchdict
        pad_uri = request.matchdict['uri']
    except KeyError:
        return HTTPFound(location='/' + str(pad.id) + '/' + pad.uri)

    try:
        lexer = get_lexer_by_name(pad.syntax)
    except ClassNotFound:
        lexer = get_lexer_by_name(guess_lexer_name(pad.data))

    formatter = HtmlFormatter(linenos=True, style='native')
    pygdata = highlight(pad.data, lexer, formatter)

    return {'pad': pad, 'pygdata': pygdata}
Esempio n. 5
0
def alter(request):
    """alter the pad"""
    from slugify import slugify

    # prettier varible
    pad_id = request.matchdict['id']
    # query for the pad object by id
    pad = get_pad(pad_id)

    if pad == None:  # redirect home if invalid id
        return HTTPFound(location='/')

    try:  # make sure 'uri' key in matchdict
        pad_uri = request.matchdict['uri']
    except KeyError:
        return HTTPFound(location='/' + str(pad.id) + '/' + pad.uri)

    if 'save' in request.POST:

        try:
            pad.uri = slugify(request.params['newuri'])
        except:
            pass

        try:
            pad.syntax = request.params['newsyntax']
        except:
            pass

        if 'wordwrap' in request.params: pad.wordwrap = True
        else: pad.wordwrap = False

        DBSession.add(pad)
        DBSession.flush()

    return HTTPFound(location='/' + str(pad.id) + '/' + pad.uri)