Ejemplo n.º 1
0
 def get(self, id):
     prop = Proposal.get_by_id(int(id))
     if self.request.get('change'):
         self._handle_change(prop, self.request.get('change'))
         return
     changes_db = Change.all().ancestor(prop).order('created').fetch(100)
     changes = []
     lexer = pygments.lexers.get_lexer_by_name('diff')
     formatter = pygments.formatters.get_formatter_by_name('html', nobackground=True)
     for change_db in changes_db:
         change = {
             'db': change_db,
         }
         if change_db.class_name() == 'PatchChange':
             change['type'] = 'patch'
             change['highlighted'] = pygments.highlight(change_db.diff, lexer, formatter)
         elif change_db.class_name() == 'BinaryChange':
             mime_type, encoding = mimetypes.guess_type(change_db.path, False)
             if mime_type.startswith('image'):
                 change['type'] = 'image'
             else:
                 change['type'] = 'binary'
         changes.append(change)
     
     vote = prop.get_vote(user)
     add_stylesheet(self.request, '/pygments.css')
     self.response.out.write(self.env.get_template('proposal_view.html').render(locals()))
Ejemplo n.º 2
0
 def _get_file(self, path, path_segs, dir):
     if self.request.get('version'):
         file = dir.version(self.request.get('version'))
     else:
         file = dir.latest
     latest_file = dir.latest
     mime_type, encoding = mimetypes.guess_type(dir.path, False)
     if mime_type.startswith('text'):
         mode = 'code'
         lexer = pygments.lexers.guess_lexer_for_filename(dir.path, file.data)
         formatter = pygments.formatters.get_formatter_by_name('html', linenos='table', lineanchors='line', anchorlinenos=True, nobackground=True)
         highlighted = pygments.highlight(file.data, lexer, formatter)
         add_stylesheet(self.request, '/pygments.css')
     elif mime_type.startswith('image'):
         mode = 'image'
     else:
         mode = 'binary'
     
     if self.request.get('format') == 'raw':
         if mode == 'binary':
             self.response.headers['Content-Disposition'] = 'attachment'
         self.response.headers['Content-Type'] = mime_type
         self.response.out.write(file.data)
     else:
         self.response.out.write(self.env.get_template('browser_file.html').render(locals()))
Ejemplo n.º 3
0
 def _handle_preview(self):
     data = self.request.get('data').replace('\t', '    ').replace('\r\n', '\n')
     path = self.request.get('path')
     title = self.request.get('title')
     old_file = File.from_path(path)
     new_lines = [line+'\n' for line in data.splitlines()]
     old_lines = [line+'\n' for line in old_file.data.splitlines()]
     diff = difflib.unified_diff(old_lines, new_lines, 'a/'+path, 'b/'+path)
     diff_data = ''.join(diff)
     lexer = pygments.lexers.get_lexer_by_name('diff')
     formatter = pygments.formatters.get_formatter_by_name('html', nobackground=True)
     highlighted = pygments.highlight(diff_data, lexer, formatter)
     user_props = Proposal.all().filter('state', 'private').filter('user', user.user).fetch(100)
     add_stylesheet(self.request, '/pygments.css')
     self.response.out.write(self.env.get_template('proposal_preview.html').render(locals()))