Exemple #1
0
    def post(self, **kwargs):
        self.form = PasteForm(request.form)

        if self.form.validate():
            if self.current_user:
                user_key = str(self.current_user.key())
            else:
                user_key = None

            language_code = request.form.get('language')
            code_raw = request.form.get('code', u'')
            code = highlight(code_raw, language_code)

            values = {
                'area_key': str(self.area.key()),
                'user_key': user_key,
                'code_raw': code_raw,
                'code':     code,
                'language': language_code,
            }
            paste = Paste(**values)
            paste.put()
            self.set_message('success', _('The paste was saved.'), flash=True)

            return redirect_to('paste/view', paste_id=paste.id,
                area_name=self.area.name)
        else:
            self.set_form_error(_('Ooops, code is empty! Please post '
                'some lines.'))
            return self.get()
Exemple #2
0
class PasteNewHandler(PasteBaseHandler):
    """Displays a paste form and saves a new paste."""
    form = None

    def get(self, **kwargs):
        context = {
            'form': self.form or PasteForm(language=kwargs.pop('language',
                'python')),
        }
        return self.render_response('paste/new.html', **context)

    def post(self, **kwargs):
        self.form = PasteForm(request.form)

        if self.form.validate():
            if self.current_user:
                user_key = str(self.current_user.key())
            else:
                user_key = None

            language_code = request.form.get('language')
            code_raw = request.form.get('code', u'')
            code = highlight(code_raw, language_code)

            values = {
                'area_key': str(self.area.key()),
                'user_key': user_key,
                'code_raw': code_raw,
                'code':     code,
                'language': language_code,
            }
            paste = Paste(**values)
            paste.put()
            self.set_message('success', _('The paste was saved.'), flash=True)

            return redirect_to('paste/view', paste_id=paste.id,
                area_name=self.area.name)
        else:
            self.set_form_error(_('Ooops, code is empty! Please post '
                'some lines.'))
            return self.get()