コード例 #1
0
ファイル: share11.py プロジェクト: rhymes/share11
  def post(self):
    # get the fields' values
    content = self.request.get('content')
    if not content: # content is required
      self.redirect('/')
      return

    user = users.get_current_user()
    syntax = self.request.get('syntax', default_value='text')

    # save and redirect
    pasting = Pasting(content=content, author=user, syntax=syntax)
    pasting.put()

    self.redirect('/%s' % pasting.key().id())
コード例 #2
0
ファイル: share11.py プロジェクト: rhymes/share11
  def get(self, pasting_id, out_format=''):
    user = users.get_current_user()
    if user:
      logout_url = users.create_logout_url(self.request.uri)
    else:
      login_url = users.create_login_url(self.request.uri)

    try:
      pasting = Pasting.get_by_id(int(pasting_id))
      if not pasting:
        self.error(404)
        return
    except ValueError:
      self.error(404)
      return

    # pygmentize
    try:
      formatter = formatters.get_formatter_by_name(
        out_format or 'html', linenos=not out_format)
      lexer = get_lexer_by_name(pasting.syntax, stripall=True)
    except ValueError:
      self.error(404)
      return

    pasting_body = highlight(pasting.content, lexer, formatter)

    if not out_format:
      formats = get_formatters()
      syntax_description = lexer.name
      self.render("pasting.html", locals())
    elif out_format and formatter:
      self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
      disposition = "inline; filename='%s.%s'" % (pasting_id, out_format)
      self.response.headers['Content-Disposition'] = disposition
      self.response.write(pasting_body)