コード例 #1
0
ファイル: pastes.py プロジェクト: sergiik/lodgeit
    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')
            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_id, req.user_hash,
                              'private' in req.form)
                session.add(paste)
                session.commit()
                local.request.session['language'] = language
                if paste.private:
                    identifier = paste.private_id
                else:
                    identifier = paste.paste_id
                return redirect(url_for('pastes/show_paste',
                                        identifier=identifier))
        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 = 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
                                  )
コード例 #2
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')
            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_id, req.user_hash,
                              'private' in req.form)
                session.add(paste)
                session.commit()
                local.request.session['language'] = language
                if paste.private:
                    identifier = paste.private_id
                else:
                    identifier = paste.paste_id
                return redirect(url_for('pastes/show_paste',
                                        identifier=identifier))
        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 = 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
                                  )
コード例 #3
0
 def set_language(self, lang='en'):
     """Minimal view that sets a different language. Redirects
     back to the page the user is coming from."""
     for key, value in i18n_list_languages():
         if key == lang:
             local.request.set_language(lang)
             break
     return redirect(local.request.headers.get('referer') or
                     url_for('pastes/new_paste'))
コード例 #4
0
ファイル: pastes.py プロジェクト: sergiik/lodgeit
 def set_language(self, lang='en'):
     """Minimal view that sets a different language. Redirects
     back to the page the user is coming from."""
     for key, value in i18n_list_languages():
         if key == lang:
             local.request.set_language(lang)
             break
     return redirect(local.request.headers.get('referer') or
                     url_for('pastes/new_paste'))
コード例 #5
0
 def set_colorscheme(self):
     """Minimal view that updates the style session cookie. Redirects
     back to the page the user is coming from.
     """
     style_name = local.request.form.get('style')
     resp = redirect(local.request.headers.get('referer') or
                     url_for('pastes/new_paste'))
     # TODO(dc): use some sort of form element validation instead
     if style_name in STYLES:
         resp.set_cookie('style', style_name)
     return resp
コード例 #6
0
ファイル: pastes.py プロジェクト: sergiik/lodgeit
 def set_colorscheme(self):
     """Minimal view that updates the style session cookie. Redirects
     back to the page the user is coming from.
     """
     style_name = local.request.form.get('style')
     resp = redirect(local.request.headers.get('referer') or
                     url_for('pastes/new_paste'))
     # TODO(dc): use some sort of form element validation instead
     if style_name in STYLES:
         resp.set_cookie('style', style_name)
     return resp
コード例 #7
0
ファイル: pastes.py プロジェクト: dpursehouse/lodgeit
    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(
                url_for('pastes/compare_paste', old_id=old_id, new_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))
コード例 #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(url_for('pastes/compare_paste',
                                    old_id=old_id, new_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)
                                  )