Beispiel #1
0
def pastes_get_diff(old_id, new_id):
    """Compare the two pastes and return an unified diff."""
    old = Paste.get(old_id)
    new = Paste.get(new_id)
    if old is None or new is None:
        raise ValueError('argument error, paste not found')
    return old.compare_to(new)
Beispiel #2
0
def pastes_get_diff(old_id, new_id):
    """Compare the two pastes and return an unified diff."""
    old = Paste.get(old_id)
    new = Paste.get(new_id)
    if old is None or new is None:
        raise ValueError('argument error, paste not found')
    return old.compare_to(new)
Beispiel #3
0
    def new_paste(self, language=None):
        """The 'create a new paste' view."""
        language = local.request.args.get('language', language)
        if language is None:
            language = local.request.session.get('language', 'text')

        code = error = ''
        show_captcha = private = False
        parent = None
        req = local.request
        getform = req.form.get

        if local.request.method == 'POST':
            code = getform('code', u'')
            language = getform('language')

            parent_id = getform('parent')
            if parent_id is not None:
                parent = Paste.get(parent_id)

            spam = getform('webpage') or antispam.is_spam(code)
            if spam:
                error = _('your paste contains spam')
                captcha = getform('captcha')
                if captcha:
                    if check_hashed_solution(captcha):
                        error = None
                    else:
                        error = _('your paste contains spam and the '
                                  'CAPTCHA solution was incorrect')
                show_captcha = True
            if code and language and not error:
                paste = Paste(code, language, parent, req.user_hash,
                              'private' in req.form)
                db.session.add(paste)
                db.session.commit()
                local.request.session['language'] = language
                return redirect(paste.url)

        else:
            if local.request.args.get('private', '0') != '0':
                private = True
            parent_id = req.values.get('reply_to')
            if parent_id is not None:
                parent = Paste.get(parent_id)
                if parent is not None:
                    code = parent.code
                    language = parent.language
                    private = parent.private

        return render_to_response('new_paste.html',
            languages=list_languages(),
            parent=parent,
            code=code,
            language=language,
            error=error,
            show_captcha=show_captcha,
            private=private
        )
Beispiel #4
0
    def new_paste(self, language=None):
        """The 'create a new paste' view."""
        language = local.request.args.get('language', language)
        if language is None:
            language = local.request.session.get('language', 'text')

        code = error = ''
        show_captcha = private = False
        parent = None
        req = local.request
        getform = req.form.get

        if local.request.method == 'POST':
            code = getform('code', u'')
            language = getform('language')

            parent_id = getform('parent')
            if parent_id is not None:
                parent = Paste.get(parent_id)

            spam = getform('webpage') or antispam.is_spam(code)
            if spam:
                error = _('your paste contains spam')
                captcha = getform('captcha')
                if captcha:
                    if check_hashed_solution(captcha):
                        error = None
                    else:
                        error = _('your paste contains spam and the '
                                  'CAPTCHA solution was incorrect')
                show_captcha = True
            if code and language and not error:
                paste = Paste(code, language, parent, req.user_hash, 'private'
                              in req.form)
                db.session.add(paste)
                db.session.commit()
                local.request.session['language'] = language
                return redirect(paste.url)

        else:
            if local.request.args.get('private', '0') != '0':
                private = True
            parent_id = req.values.get('reply_to')
            if parent_id is not None:
                parent = Paste.get(parent_id)
                if parent is not None:
                    code = parent.code
                    language = parent.language
                    private = parent.private

        return render_to_response('new_paste.html',
                                  languages=list_languages(),
                                  parent=parent,
                                  code=code,
                                  language=language,
                                  error=error,
                                  show_captcha=show_captcha,
                                  private=private)
Beispiel #5
0
    def unidiff_paste(self, new_id=None, old_id=None):
        """Render an udiff for the two pastes."""
        old = Paste.get(old_id)
        new = Paste.get(new_id)

        if not (old or new):
            raise NotFound()

        return Response(old.compare_to(new), mimetype='text/plain')
Beispiel #6
0
    def unidiff_paste(self, new_id=None, old_id=None):
        """Render an udiff for the two pastes."""
        old = Paste.get(old_id)
        new = Paste.get(new_id)

        if not (old or new):
            raise NotFound()

        return Response(old.compare_to(new), mimetype='text/plain')
Beispiel #7
0
 def show_tree(self, identifier):
     """Display the tree of some related pastes."""
     paste = Paste.resolve_root(identifier)
     if paste is None:
         raise NotFound()
     return render_to_response('paste_tree.html',
                               paste=paste,
                               current=identifier)
Beispiel #8
0
    def compare_paste(self, new_id=None, old_id=None):
        """Render a diff view for two pastes."""
        getform = local.request.form.get
        # redirect for the compare form box
        if old_id is None:
            old_id = getform('old', '-1').lstrip('#')
            new_id = getform('new', '-1').lstrip('#')
            return redirect('/compare/%s/%s' % (old_id, new_id))

        old = Paste.get(old_id)
        new = Paste.get(new_id)
        if old is None or new is None:
            raise NotFound()

        return render_to_response('compare_paste.html',
                                  old=old,
                                  new=new,
                                  diff=old.compare_to(new, template=True))
Beispiel #9
0
 def show_tree(self, identifier):
     """Display the tree of some related pastes."""
     paste = Paste.resolve_root(identifier)
     if paste is None:
         raise NotFound()
     return render_to_response('paste_tree.html',
                               paste=paste,
                               current=identifier
                               )
Beispiel #10
0
    def show_paste(self, identifier, raw=False):
        """Show an existing paste."""
        linenos = local.request.args.get('linenos') != 'no'
        paste = Paste.get(identifier)
        if paste is None:
            raise NotFound()
        if raw:
            return Response(paste.code, mimetype='text/plain; charset=utf-8')

        style, css = get_style(local.request)
        return render_to_response('show_paste.html',
                                  paste=paste,
                                  style=style,
                                  css=css,
                                  styles=STYLES,
                                  linenos=linenos,
                                  new_replies=Paste.fetch_replies(),
                                  )
Beispiel #11
0
def pastes_get_paste(paste_id):
    """Get all known information about a paste by a given paste id.

    Return a dictionary with these keys:
    `paste_id`, `code`, `parsed_code`, `pub_date`, `language`,
    `parent_id`, `url`.
    """
    paste = Paste.get(paste_id)
    if paste is not None:
        return paste.to_xmlrpc_dict()
Beispiel #12
0
    def compare_paste(self, new_id=None, old_id=None):
        """Render a diff view for two pastes."""
        getform = local.request.form.get
        # redirect for the compare form box
        if old_id is None:
            old_id = getform('old', '-1').lstrip('#')
            new_id = getform('new', '-1').lstrip('#')
            return redirect('/compare/%s/%s' % (old_id, new_id))

        old = Paste.get(old_id)
        new = Paste.get(new_id)
        if old is None or new is None:
            raise NotFound()

        return render_to_response('compare_paste.html',
            old=old,
            new=new,
            diff=old.compare_to(new, template=True)
        )
Beispiel #13
0
    def show_paste(self, identifier, raw=False):
        """Show an existing paste."""
        linenos = local.request.args.get('linenos') != 'no'
        paste = Paste.get(identifier)

        if (paste is None) or (paste.private and identifier.isdigit()):
            raise NotFound()
        if raw:
            return Response(paste.code, mimetype='text/plain; charset=utf-8')

        style, css = get_style(local.request)
        return render_to_response('show_paste.html',
                                  paste=paste,
                                  style=style,
                                  css=css,
                                  styles=STYLES,
                                  linenos=linenos,
                                  new_replies=Paste.fetch_replies(),
                                  )
Beispiel #14
0
def pastes_get_paste(paste_id):
    """Get all known information about a paste by a given paste id.

    Return a dictionary with these keys:
    `paste_id`, `code`, `parsed_code`, `pub_date`, `language`,
    `parent_id`, `url`.
    """
    paste = Paste.get(paste_id)
    if paste is not None:
        return paste.to_xmlrpc_dict()
Beispiel #15
0
def render_to_response(template_name, **context):
    """Render a template to a response. This automatically fetches
    the list of new replies for the layout template. It also
    adds the current request to the context. This is used for the
    welcome message.
    """
    from lodgeit.models import Paste

    request = local.request
    if request.method == "GET":
        context["new_replies"] = Paste.fetch_replies()
    return Response(render_template(template_name, **context), mimetype="text/html")
Beispiel #16
0
def render_to_response(template_name, **context):
    """Render a template to a response. This automatically fetches
    the list of new replies for the layout template. It also
    adds the current request to the context. This is used for the
    welcome message.
    """
    from lodgeit.models import Paste
    request = local.request
    if request.method == 'GET':
        context['new_replies'] = Paste.fetch_replies()
    return Response(render_template(template_name, **context),
                    mimetype='text/html')
Beispiel #17
0
def pastes_new_paste(language,
                     code,
                     parent_id=None,
                     filename='',
                     mimetype='',
                     private=False):
    """Create a new paste. Return the new ID.

    `language` can be None, in which case the language will be
    guessed from `filename` and/or `mimetype`.
    """
    if not language:
        language = get_language_for(filename or '', mimetype or '')
    parent = None
    if parent_id:
        parent = Paste.get(parent_id)
        if parent is None:
            raise ValueError('parent paste not found')

    paste = Paste(code, language, parent, private=private)
    db.session.add(paste)
    db.session.commit()
    return paste.identifier
Beispiel #18
0
def pastes_new_paste(language, code, parent_id=None,
                     filename='', mimetype='', private=False):
    """Create a new paste. Return the new ID.

    `language` can be None, in which case the language will be
    guessed from `filename` and/or `mimetype`.
    """
    if not language:
        language = get_language_for(filename or '', mimetype or '')
    parent = None
    if parent_id:
        parent = Paste.get(parent_id)
        if parent is None:
            raise ValueError('parent paste not found')

    paste = Paste(code, language, parent, private=private)
    db.session.add(paste)
    db.session.commit()
    return paste.identifier
Beispiel #19
0
    def show_all(self, page=1):
        """Paginated list of pages."""
        def link(page):
            if page == 1:
                return '/all/'
            return '/all/%d' % page

        form_args = local.request.args
        query = Paste.find_all()

        pastes = query.limit(10).offset(10 * (page - 1)).all()
        if not pastes and page != 1:
            raise NotFound()

        return render_to_response('show_all.html',
                                  pastes=pastes,
                                  pagination=generate_pagination(
                                      page, 10, query.count(), link),
                                  css=get_style(local.request)[1],
                                  show_personal='show_personal' in form_args)
Beispiel #20
0
    def show_all(self, page=1):
        """Paginated list of pages."""
        def link(page):
            if page == 1:
                return '/all/'
            return '/all/%d' % page

        form_args = local.request.args
        query = Paste.find_all()

        pastes = query.limit(10).offset(10 * (page - 1)).all()
        if not pastes and page != 1:
            raise NotFound()

        return render_to_response('show_all.html',
            pastes=pastes,
            pagination=generate_pagination(page, 10, query.count(), link),
            css=get_style(local.request)[1],
            show_personal='show_personal' in form_args
        )
Beispiel #21
0
 def rss(self):
     query = Paste.find_all()
     items = query.all()
     return render_to_response('rss.html',
                               items=items,
                               mimetype='application/rss+xml')
Beispiel #22
0
def pastes_get_recent(amount=5):
    """Return information dict (see `getPaste`) about the last
    `amount` pastes.
    """
    amount = min(amount, 20)
    return [x.to_xmlrpc_dict() for x in Paste.find_all().limit(amount)]
Beispiel #23
0
def pastes_get_recent(amount=5):
    """Return information dict (see `getPaste`) about the last
    `amount` pastes.
    """
    amount = min(amount, 20)
    return [x.to_xmlrpc_dict() for x in Paste.find_all().limit(amount)]
Beispiel #24
0
 def rss(self):
     query = Paste.find_all()
     items = query.all()
     return render_to_response('rss.html', items=items,
                               mimetype='application/rss+xml')