class View(MethodView): decorators = [ gist_require(), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist, version='master'): jagare = get_jagare(gist.id, gist.parent) error, tree = jagare.ls_tree(gist.get_real_path(), version=version) if not error: tree, meta = tree['content'], tree['meta'] tree = render_tree(jagare, tree, gist, organization, version=version) watcher = get_gist_watcher(g.current_user.id, gist.id) return self.render_template( organization=organization, \ member=member, \ error=error, \ tree=tree, \ gist=gist, \ watcher=watcher, \ )
class Delete(MethodView): decorators = [ gist_require(owner=True), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist): delete_gist(g.current_user, gist, organization) return redirect(url_for('organization.view', git=organization.git))
class Unwatch(MethodView): decorators = [ gist_require(), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist): watcher = get_gist_watcher(g.current_user.id, gist.id) if watcher: delete_watcher(g.current_user, watcher, gist, organization) return redirect(gist.meta.view)
class Revisions(MethodView): decorators = [gist_require(owner=True), member_required(admin=False), login_required('account.login')] def get(self, organization, member, gist): page = request.args.get('p', 1) try: page = int(page) except ValueError: raise abort(403) jagare = get_jagare(gist.id, gist.parent) error, revisions = jagare.get_log(gist.get_real_path(), page=page, size=config.REVISIONS_PER_PAGE) if not revisions: raise abort(404) list_page = render_revisions_page(gist, page) revisions = self.render_revisions(organization, gist, revisions, list_page) return self.render_template( organization=organization, \ member=member, \ error=error, \ revisions=revisions, \ gist=gist, \ list_page=list_page, \ ) def render_revisions(self, organization, gist, revisions, list_page): for rev in revisions[:-1]: self.render_rev(rev, organization, gist) rev['type'] = 'update' yield rev rev = revisions[-1] self.render_rev(rev, organization, gist) rev['type'] = 'create' if not list_page.has_next else 'update' yield rev def render_rev(self, rev, organization, gist): rev['view'] = get_url(organization, gist, version=rev['sha']) rev['committer_time'] = format_time(rev['committer_time']) author = reqcache.get(rev['author_email']) if not author: author = get_user_from_alias(rev['author_email']) reqcache.set(rev['author_email'], author) if not author: author = Obj() author.email = rev['author_email'] author.name = None rev['author'] = author rev['diff'] = render_diff(rev['diff'])
class Raw(MethodView): decorators = [ gist_require(), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist, path): jagare = get_jagare(gist.id, gist.parent) error, res = jagare.cat_file(gist.get_real_path(), path) if error: raise abort(error) resp = Response(stream_with_context(res)) resp.headers['X-Accel-Buffering'] = 'no' resp.headers['Cache-Control'] = 'no-cache' resp.headers['Content-Type'] = res.headers.get( 'content-type', 'application/octet-stream') return resp
class Fork(MethodView): decorators = [ gist_require(), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist): private = create_token(20) if gist.private else None fork_gist, err = create_gist(organization, g.current_user, gist.summary, parent=gist, private=private, watchers=1) if err: return redirect(gist.meta.view) return redirect(get_url(organization, fork_gist))
class Watchers(MethodView): decorators = [ gist_require(), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist): watchers = get_gist_watchers(gist.id) return self.render_template( organization=organization, \ member=member, \ gist=gist, \ watchers=self.render_watchers(watchers) ) def render_watchers(self, watchers): for watcher in watchers: setattr(watcher, 'user', get_user(watcher.uid)) yield watcher
class Forks(MethodView): decorators = [ gist_require(), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist): forks = get_gist_forks(gist.id) return self.render_template( organization=organization, \ member=member, \ gist=gist, \ forks=self.render_forks(organization, forks) ) def render_forks(self, organization, forks): for fork in forks: view_url = get_url(organization, fork, 'gists.view') setattr(fork, 'view', view_url) setattr(fork, 'user', get_user(fork.uid)) yield fork
class Edit(MethodView): decorators = [ gist_require(owner=True), member_required(admin=False), login_required('account.login') ] def get(self, organization, member, gist): jagare = get_jagare(gist.id, gist.parent) error, tree = jagare.ls_tree(gist.get_real_path()) if not error: tree, meta = tree['content'], tree['meta'] tree = render_tree(jagare, tree, gist, organization, False) return self.render_template( organization=organization, \ member=member, \ error=error, \ tree=tree, \ gist=gist, \ ) def post(self, organization, member, gist): summary = request.form.get('summary') filenames = request.form.getlist('filename') codes = request.form.getlist('code') data = {} if len(filenames) != len(codes): raise abort(400) for filename, content in zip(filenames, codes): if not filename and not content: continue if not filename or not content: return self.render_template( organization=organization, \ member=member, \ error=code.GIST_WITHOUT_FILENAME if not filename else code.GIST_WITHOUT_CONTENT, \ tree=self.gen_tree(filenames, codes), \ gist=gist, ) if data.get(filename): return self.render_template( organization=organization, \ member=member, \ error=code.GIST_FILENAME_EXISTS, \ tree=self.gen_tree(filenames, codes), \ gist=gist, ) data[filename] = content jagare = get_jagare(gist.id, gist.parent) error, tree = jagare.ls_tree(gist.get_real_path()) if error: return self.render_template( organization=organization, \ member=member, \ error=code.REPOS_LS_TREE_FAILED, \ tree=self.gen_tree(filenames, codes), \ gist=gist, ) data = self.diff(tree, data) _, error = update_gist(g.current_user, gist, data, summary) if error: return self.render_template( organization=organization, \ member=member, \ error=code.GIST_UPDATE_FAILED, \ tree=self.gen_tree(filenames, codes), \ gist=gist, ) return redirect(gist.meta.view) def diff(self, tree, data): tree, meta = tree['content'], tree['meta'] for d in tree: name = d['name'] if data.get(name, None) is None: # set delete flag data[d['path']] = '' continue return data def gen_tree(self, filenames, codes): for filename, content in zip(filenames, codes): d = Obj() d.name = filename d.content = lambda: content yield d