示例#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)
示例#2
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 old is None or new is None:
            raise NotFound()

        return Response(old.compare_to(new), mimetype='text/plain')
示例#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 = 'text'

        code = error = ''
        show_captcha = False
        private = local.application.pastes_private
        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:
                private = coalesce_private(private, 'private' in req.form)
                paste = Paste(code, language, parent, None, bool(private))
                db.session.add(paste)
                db.session.commit()
                return redirect(paste.url)

        else:
            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 = coalesce_private(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
        )
示例#4
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
     )
示例#5
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)
        )
示例#6
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()
示例#7
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 spacepaste.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')
示例#8
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,
        )
示例#9
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
示例#10
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)]