Esempio n. 1
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
        )
Esempio n. 2
0
def _get_pygments_lexers(add_empty=True):
    r = []
    if add_empty:
        r.append(('', ''),)
    for lexer in get_all_lexers():
        r.append((lexer[1][0], _(lexer[0])),)
    return r
Esempio n. 3
0
#: we use a hardcoded list here because we want to keep the interface
#: simple
def _get_pygments_lexers(add_empty=True):
    r = []
    if add_empty:
        r.append(('', ''),)
    for lexer in get_all_lexers():
        r.append((lexer[1][0], _(lexer[0])),)
    return r

LANGUAGES = _get_pygments_lexers()
#: Add spacepaste's special language lexers
#: Or at least ensure those exist.
LANGUAGES.extend([
    ('csv',              _('CSV')),
    ('javac-messages',   _('javac Messages')),
    ('diff',             _('Unified Diff')),
    ('gcc-messages',     _('GCC Messages')),
    ('creole',           _('Creole Wiki')),
    ('multi',            _('Multi-File')),
])
LANGUAGES = dict(sorted(LANGUAGES, key=itemgetter(1)))


STYLES = dict((x, x.title()) for x in get_all_styles())

DEFAULT_STYLE = 'friendly'

_section_marker_re = re.compile(r'^(?<!\\)###\s*(.*?)(?:\[(.+?)\])?\s*$(?m)')
_escaped_marker = re.compile(r'^\\(?=###)(?m)')
Esempio n. 4
0
"""
    spacepaste.libs.filterable
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    Small library that adds filterable support to some parts of spacepaste.

    :copyright: 2008 by Christopher Grebs.
    :license: BSD.
"""
from spacepaste.i18n import _
from spacepaste.utils import render_template


ACTIONS = {
    'str': {
        'is':           _(u'is'),
        'contains':     _(u'contains'),
        'startswith':   _('startswith'),
    },
    'int': {
        'is':           _(u'is'),
        'greater':      _(u'greater'),
        'lower':        _(u'lower'),
    },
    'date': {
        'is':           _(u'same date'),
        'greater':      _(u'later'),
        'lower':        _(u'earlier'),
    }
}
ACTIONS_MAP = {
Esempio n. 5
0
def generate_pagination(page, per_page, total, link_builder=None,
                        normal='<a href="%(url)s">%(page)d</a>',
                        active='<strong>%(page)d</strong>',
                        commata=',\n', ellipsis=' ...\n', threshold=3,
                        prev_link=True, next_link=True,
                        gray_prev_link=True, gray_next_link=True):
    """Generates a pagination.

    :param page:            current page number
    :param per_page:        items per page
    :param total:           total number of items
    :param link_builder:    a function which is called with a page number
                            and has to return the link to a page. Per
                            default it links to ``?page=$PAGE``
    :param normal:          template for normal (not active) link
    :param active:          template for active link
    :param commata:         inserted into the output to separate two links
    :param ellipsis:        inserted into the output to display an ellipsis
    :param threshold:       number of links next to each node (left end,
                            right end and current page)
    :param prev_link:       display back link
    :param next_link:       dipslay next link
    :param gray_prev_link:  the back link is displayed as span class disabled
                            if no backlink exists. otherwise it's not
                            displayed at all
    :param gray_next_link:  like `gray_prev_link` just for the next page link
    """
    page = int(page or 1)
    if link_builder is None:
        link_builder = lambda page: '?page=%d' % page

    was_ellipsis = False
    result = []
    pages = int(math.ceil(total / float(per_page)))
    prev = None
    next = None
    for num in xrange(1, pages + 1):
        if num == page:
            was_ellipsis = False
        if num - 1 == page:
            next = num
        if num + 1 == page:
            prev = num
        if num <= threshold or num > pages - threshold or \
           abs(page - num) < math.ceil(threshold / 2.0):
            if result and result[-1] != ellipsis:
                result.append(commata)
            link = link_builder(num)
            template = num == page and active or normal
            result.append(template % {
                'url':      link,
                'page':     num
            })
        elif not was_ellipsis:
            was_ellipsis = True
            result.append(ellipsis)

    if next_link:
        if next is not None:
            result.append(_(u' <a href="%s">Next &raquo;</a>') %
                          link_builder(next))
        elif gray_next_link:
            result.append(_(u' <span class="disabled">Next &raquo;</span>'))
    if prev_link:
        if prev is not None:
            result.insert(0, _(u'<a href="%s">&laquo; Prev</a> ') %
                          link_builder(prev))
        elif gray_prev_link:
            result.insert(0, _(u'<span class="disabled">&laquo; Prev</span> '))

    return u''.join(result)