Beispiel #1
0
def edit():
    form = EditForm()
    if form.validate_on_submit():
        text = form.text.data
        extension = form.extension.data
        id = paste.insert_paste(text)
        return redirect(
            url_for('view.view', id=id, extension=extension, _external=True))

    clone = request.args.get('clone')
    if clone == 'about':
        doc = {'text': about_text}
    else:
        doc = paste.get_paste(clone)

    if doc is not None and form.text.data is None:
        form.text.data = doc['text']

    return render_template(
        'edit/edit.html',
        hide_new=True,
        languages=LANGUAGES,
        lang=request.args.get('lang'),
        form=form,
        body_class='edit-height-fix',
    )
Beispiel #2
0
    def dispatch_request(self, id, extension=None):
        doc = paste.get_paste(id)
        abort_if(doc is None, 404)

        return (doc['text'], 200, {
            'Content-type': 'text/plain; charset=utf-8',
            'X-Content-Type-Options': 'nosniff'
        })
    def dispatch_request(self, id, extension='txt'):
        doc = paste.get_paste(id)
        abort_if(doc is None, 404)

        res = make_response(doc['text'])
        res.headers['Content-Disposition'] = f'attachment; filename={id}.{extension}'
        res.headers['Content-type'] = 'text/plain; charset=utf-8'
        return res
Beispiel #4
0
    def get(self, form):
        lang = request.args.get('lang')
        edit = request.args.get('edit')
        if edit and edit not in session.get('created_ids', []):
            flash(f'You don\'t have permission to edit this paste [{edit}].',
                  category='error')
            return redirect(url_for('paste.show', id=edit, extension=lang))

        clone = request.args.get('clone')

        if edit:
            doc = paste.get_paste(edit)
            abort_if(doc is None, 404)
        elif clone:
            doc = paste.get_paste(clone)
            abort_if(doc is None, 404)
        else:
            doc = None

        text = ''
        delete_after = '7'
        if doc is not None and form.text.data is None:
            form.text.data = doc['text']
            if doc['delete_at']:
                form.delete_after.data = str(
                    math.ceil(
                        (doc['delete_at'] - datetime.utcnow()).total_seconds()
                        / 86400))
            else:
                form.delete_after.data = delete_after
            form.extension.data = lang
            form.id.data = edit if edit else None
            text = highlight(doc['text'], request.args.get('lang'))
        else:
            form.text.data = text
            form.delete_after.data = delete_after

        return render_template(
            'paste_edit.html',
            hide_new=True,
            languages=LANGUAGES,
            form=form,
            text=text,
            body_class='edit-height-fix',
        )
Beispiel #5
0
def view_raw(id, extension=None):
    if id == 'about':
        doc = {'text': about_text}
    else:
        doc = paste.get_paste(id)

    if doc is None:
        abort(404)

    return (doc['text'], 200, {
        'Content-type': 'text/plain; charset=utf-8',
        'X-Content-Type-Options': 'nosniff'
    })
Beispiel #6
0
def view_download(id, extension='txt'):
    if id == 'about':
        doc = {'text': about_text}
    else:
        doc = paste.get_paste(id)

    if doc is None:
        abort(404)

    res = make_response(doc['text'])
    res.headers['Content-Disposition'] = \
        f'attachment; filename={id}.{extension}'
    res.headers['Content-type'] = 'text/plain; charset=utf-8'
    return res
Beispiel #7
0
    def post(self, form):
        id = form.id.data
        text = form.text.data
        delete_after_days = int(form.delete_after.data)
        extension = form.extension.data or None

        if id:
            doc = paste.get_paste(id)
            abort_if(doc is None, 404)
            doc['text'] = text
            doc['delete_at'] = doc['created_at'] + timedelta(
                days=delete_after_days)
        else:
            doc = {
                'text': text,
                'delete_at':
                datetime.utcnow() + timedelta(days=delete_after_days)
            }

        upserted_id = paste.upsert_paste(doc)
        created_ids = session.get('created_ids', [])

        if upserted_id not in created_ids:
            created_ids.append(upserted_id)

        session['created_ids'] = created_ids

        accept = request.headers.get('Accept')
        if accept == 'text/plain':
            return url_for('paste.show',
                           id=upserted_id,
                           extension=extension,
                           _external=True), 200, {
                               'Content-Type': 'text/plain'
                           }
        return redirect(
            url_for('paste.show',
                    id=upserted_id,
                    extension=extension,
                    _external=True))
Beispiel #8
0
def view(id, extension=None):
    if id == 'about':
        doc = {'text': about_text}
    else:
        doc = paste.get_paste(id)

    if doc is None:
        abort(404)

    highlighted = request.args.get('h')
    if highlighted:
        highlighted = highlighted.split(' ')

    try:
        lexer = get_lexer_for_filename('foo.{}'.format(extension))
    except ClassNotFound:
        lexer = guess_lexer(doc['text'])

    if id == 'about':
        title = '/about.md'
        days_left = None
    elif doc['delete_at'] is None:
        title = f'{request.path}{urlencode(request.args)}'
        days_left = None
    else:
        title = f'{request.path}{urlencode(request.args)}'
        days_left = (doc['delete_at'] - doc['created_at']).days

    return dict(
        id=id,
        text=highlight(doc['text'], lexer, HtmlFormatter()),
        text_raw=doc['text'],
        days_left=days_left,
        extension=extension,
        lines=doc['text'].count('\n') + 1,
        highlighted=highlighted,
        title=title,
        title_link=request.url
    )
Beispiel #9
0
 def dispatch_request(self, id, extension=None):
     doc = paste.get_paste(id)
     abort_if(doc is None, 404)
     return self.generate_social_image(doc, extension), {
         'Content-Type': 'image/jpeg'
     }
Beispiel #10
0
def test_should_return_data_when_in_db(app, monkeypatch):
    with app.app_context():
        doc = paste.get_paste('abcd')
        assert doc is not None
        assert doc['_id'] == 'abcd'
Beispiel #11
0
def test_should_return_none_when_not_in_db(app, monkeypatch):
    with app.app_context():
        doc = paste.get_paste('1234')
        assert doc is None