From 74dadb3424c5ba68b777aa6d62fdfb3aadf49b70 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Sep 07 2016 10:50:18 +0000 Subject: Port the controllers remaining to rely on the variables in flask.g --- diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index 89afa1e..1999a20 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -29,8 +29,7 @@ import pagure.exceptions import pagure.lib import pagure.lib.git import pagure.forms -from pagure import (APP, SESSION, LOG, login_required, is_repo_admin, - __get_file_in_tree) +from pagure import APP, SESSION, LOG, login_required, __get_file_in_tree @@ -189,7 +188,6 @@ def request_pulls(repo, username=None): select='requests', repo=repo, username=username, - repo_obj=repo_obj, requests=requests, oth_requests=oth_requests, status=status, @@ -379,7 +377,7 @@ def request_pull_edit(repo, requestid, username=None): if request.status != 'Open': flask.abort(400, 'Pull-request is already closed') - if not is_repo_admin(repo) \ + if not flask.g.repo_admin \ and flask.g.fas_user.username != request.user.username: flask.abort(403, 'You are not allowed to edit this pull-request') @@ -537,7 +535,7 @@ def pull_request_drop_comment(repo, requestid, username=None): if (flask.g.fas_user.username != comment.user.username or comment.parent.status is False) \ - and not is_repo_admin(repo): + and not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to remove this comment from ' @@ -589,7 +587,7 @@ def pull_request_edit_comment(repo, requestid, commentid, username=None): if (flask.g.fas_user.username != comment.user.username or comment.parent.status != 'Open') \ - and not is_repo_admin(project): + and not flask.g.repo_admin: flask.abort(403, 'You are not allowed to edit the comment') form = pagure.forms.EditCommentForm() @@ -635,7 +633,6 @@ def pull_request_edit_comment(repo, requestid, commentid, username=None): form=form, comment=comment, is_js=is_js, - repo_admin=is_repo_admin(project), ) @@ -664,7 +661,7 @@ def merge_request_pull(repo, requestid, username=None): if not request: flask.abort(404, 'Pull-request not found') - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to merge pull-request for this project') @@ -778,7 +775,7 @@ def set_assignee_requests(repo, requestid, username=None): if request.status != 'Open': flask.abort(403, 'Pull-request closed') - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort(403, 'You are not allowed to assign this pull-request') form = pagure.forms.ConfirmationForm() @@ -895,7 +892,7 @@ def new_request_pull(repo, branch_to, branch_from, username=None): return flask.redirect(flask.url_for( 'view_repo', username=username, repo=repo.name)) - repo_admin = is_repo_admin(repo) + repo_admin = flask.g.repo_admin form = pagure.forms.RequestPullForm() if form.validate_on_submit() and repo_admin: @@ -952,7 +949,7 @@ def new_request_pull(repo, branch_to, branch_from, username=None): SESSION.rollback() flask.flash(str(err), 'error') - if not is_repo_admin(repo): + if not flask.g.repo_admin: form = None # if the pull request we are creating only has one commit, @@ -976,20 +973,19 @@ def new_request_pull(repo, branch_to, branch_from, username=None): contributing, _ = pagure.doc_utils.convert_readme( contributing.data, 'md') + flask.g.branches = sorted(orig_repo.listall_branches()) + return flask.render_template( 'pull_request.html', select='requests', repo=repo, username=username, - repo_obj=repo_obj, orig_repo=orig_repo, diff_commits=diff_commits, diff=diff, form=form, - branches=sorted(orig_repo.listall_branches()), branch_to=branch_to, branch_from=branch_from, - repo_admin=repo_admin, contributing=contributing, ) @@ -1014,7 +1010,7 @@ def new_remote_request_pull(repo, username=None): parentpath = pagure.get_repo_path(repo) orig_repo = pygit2.Repository(parentpath) - repo_admin = is_repo_admin(repo) + repo_admin = flask.g.repo_admin form = pagure.forms.RemoteRequestPullForm() if form.validate_on_submit(): diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index e5436fa..cde2e1d 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -31,7 +31,7 @@ import pagure.exceptions import pagure.lib import pagure.forms from pagure import (APP, SESSION, LOG, __get_file_in_tree, - login_required, is_repo_admin, authenticated) + login_required, authenticated) # URLs @@ -47,7 +47,7 @@ def update_issue(repo, issueid, username=None): ''' Add a comment to an issue. ''' is_js = flask.request.args.get('js', False) - repo = pagure.lib.get_project(SESSION, repo, user=username) + repo = flask.g.repo if flask.request.method == 'GET': if not is_js: @@ -55,9 +55,6 @@ def update_issue(repo, issueid, username=None): return flask.redirect(flask.url_for( 'view_issue', username=username, repo=repo.name, issueid=issueid)) - if repo is None: - flask.abort(404, 'Project not found') - if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -66,7 +63,7 @@ def update_issue(repo, issueid, username=None): if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') - if issue.private and not is_repo_admin(repo) \ + if issue.private and not flask.g.repo_admin \ and (not authenticated() or not issue.user.user == flask.g.fas_user.username): flask.abort( @@ -84,7 +81,7 @@ def update_issue(repo, issueid, username=None): status=status, priorities=repo.priorities) if form.validate_on_submit(): - repo_admin = is_repo_admin(repo) + repo_admin = flask.g.repo_admin if flask.request.form.get('drop_comment'): commentid = flask.request.form.get('drop_comment') @@ -96,7 +93,7 @@ def update_issue(repo, issueid, username=None): if (flask.g.fas_user.username != comment.user.username or comment.parent.status != 'Open') \ - and not is_repo_admin(repo): + and not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to remove this comment from ' @@ -269,12 +266,9 @@ def update_issue(repo, issueid, username=None): def edit_tag(repo, tag, username=None): """ Edit the specified tag associated with the issues of a project. """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to edit tags associated with the issues of \ @@ -325,12 +319,9 @@ def edit_tag(repo, tag, username=None): def remove_tag(repo, username=None): """ Remove the specified tag, associated with the issues, from the project. """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to remove tags associated with the issues \ @@ -379,10 +370,7 @@ def view_issues(repo, username=None): assignee = flask.request.args.get('assignee', None) author = flask.request.args.get('author', None) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -398,7 +386,7 @@ def view_issues(repo, username=None): if authenticated(): private = flask.g.fas_user.username # If user is repo admin, show all tickets included the private ones - if is_repo_admin(repo): + if flask.g.repo_admin: private = None oth_issues = None @@ -463,8 +451,6 @@ def view_issues(repo, username=None): assignee=assignee, author=author, priority=priority, - repo_admin=is_repo_admin(repo), - repo_obj=repo_obj, ) @@ -480,10 +466,7 @@ def view_roadmap(repo, username=None): status = None milestone = flask.request.args.getlist('milestone', None) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -494,7 +477,7 @@ def view_roadmap(repo, username=None): if authenticated(): private = flask.g.fas_user.username # If user is repo admin, show all tickets included the private ones - if is_repo_admin(repo): + if flask.g.repo_admin: private = None milestones = milestone or list(repo.milestones.keys()) @@ -556,8 +539,6 @@ def view_roadmap(repo, username=None): milestones=milestones_ordered, issues=milestone_issues, tags=milestone, - repo_admin=is_repo_admin(repo), - repo_obj=repo_obj, ) @@ -569,10 +550,7 @@ def view_roadmap(repo, username=None): def new_issue(repo, username=None): """ Create a new issue """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -665,7 +643,6 @@ def new_issue(repo, username=None): form=form, repo=repo, username=username, - repo_admin=is_repo_admin(repo), types=types, default=default, ) @@ -679,10 +656,7 @@ def view_issue(repo, issueid, username=None): """ List all issues associated to a repo """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -692,7 +666,7 @@ def view_issue(repo, issueid, username=None): if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') - if issue.private and not is_repo_admin(repo) \ + if issue.private and not flask.g.repo_admin \ and (not authenticated() or not issue.user.user == flask.g.fas_user.username): flask.abort( @@ -713,12 +687,10 @@ def view_issue(repo, issueid, username=None): select='issues', repo=repo, username=username, - repo_obj=repo_obj, tag_list=tag_list, issue=issue, issueid=issueid, form=form, - repo_admin=is_repo_admin(repo), ) @@ -729,10 +701,7 @@ def delete_issue(repo, issueid, username=None): """ Delete the specified issue """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -742,7 +711,7 @@ def delete_issue(repo, issueid, username=None): if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to remove tickets of this project') @@ -778,10 +747,7 @@ def delete_issue(repo, issueid, username=None): def edit_issue(repo, issueid, username=None): """ Edit the specified issue """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -791,7 +757,7 @@ def edit_issue(repo, issueid, username=None): if issue is None or issue.project != repo: flask.abort(404, 'Issue not found') - if not (is_repo_admin(repo) + if not (flask.g.repo_admin or flask.g.fas_user.username == issue.user.username): flask.abort( 403, 'You are not allowed to edit issues for this project') @@ -875,7 +841,6 @@ def edit_issue(repo, issueid, username=None): username=username, issue=issue, issueid=issueid, - repo_admin=is_repo_admin(repo), ) @@ -886,10 +851,7 @@ def edit_issue(repo, issueid, username=None): def upload_issue(repo, issueid, username=None): ''' Upload a file to a ticket. ''' - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if repo is None: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -939,10 +901,7 @@ def view_issue_raw_file(repo, filename=None, username=None): """ Displays the raw content of a file of a commit for the specified ticket repo. """ - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -1004,10 +963,7 @@ def edit_comment_issue(repo, issueid, commentid, username=None): """ is_js = flask.request.args.get('js', False) - project = pagure.lib.get_project(SESSION, repo, user=username) - - if not project: - flask.abort(404, 'Project not found') + project = flask.g.repo if not project.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') @@ -1025,7 +981,7 @@ def edit_comment_issue(repo, issueid, commentid, username=None): if (flask.g.fas_user.username != comment.user.username or comment.parent.status != 'Open') \ - and not is_repo_admin(project): + and not flask.g.repo_admin: flask.abort(403, 'You are not allowed to edit this comment') form = pagure.forms.EditCommentForm() @@ -1072,5 +1028,4 @@ def edit_comment_issue(repo, issueid, commentid, username=None): form=form, comment=comment, is_js=is_js, - repo_admin=is_repo_admin(project), ) diff --git a/pagure/ui/plugins.py b/pagure/ui/plugins.py index a8997ba..6cf552a 100644 --- a/pagure/ui/plugins.py +++ b/pagure/ui/plugins.py @@ -20,7 +20,7 @@ from pagure.hooks import BaseHook import pagure.exceptions import pagure.lib import pagure.forms -from pagure import APP, SESSION, login_required, is_repo_admin +from pagure import APP, SESSION, login_required from pagure.lib.model import BASE from pagure.exceptions import FileNotFoundException @@ -79,12 +79,9 @@ def get_plugin(plugin_name): def view_plugin(repo, plugin, username=None, full=True): """ Presents the settings of the project. """ - repo = pagure.lib.get_project(SESSION, repo, user=username) + repo = flask.g.repo - if not repo: - flask.abort(404, 'Project not found') - - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to change the settings for this project') diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 24cf80e..6dc3a25 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -48,7 +48,7 @@ import pagure.forms import pagure import pagure.ui.plugins from pagure import (APP, SESSION, LOG, __get_file_in_tree, login_required, - is_repo_admin, admin_session_timedout) + admin_session_timedout) @APP.route('/.git') @@ -145,7 +145,6 @@ def view_repo(repo, username=None): 'repo_info.html', select='overview', repo=repo, - repo_obj=repo_obj, username=username, head=head, readme=readme, @@ -344,7 +343,6 @@ def view_commits(repo, branchname=None, username=None): 'commits.html', select='commits', origin='view_commits', - repo_obj=repo_obj, repo=repo, username=username, head=head, @@ -781,7 +779,6 @@ def view_tree(repo, identifier=None, username=None): return flask.render_template( 'file.html', select='tree', - repo_obj=repo_obj, origin='view_tree', repo=repo, username=username, @@ -833,7 +830,6 @@ def view_tags(repo, username=None): username=username, repo=repo, tags=tags, - repo_obj=repo_obj, ) @@ -849,12 +845,9 @@ def new_release(repo, username=None): and not APP.config.get('UPLOAD_FOLDER'): flask.abort(404) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to change the settings for this project') @@ -957,7 +950,6 @@ def view_settings(repo, username=None): select='settings', username=username, repo=repo, - repo_obj=repo_obj, form=form, tag_form=tag_form, branches_form=branches_form, @@ -980,12 +972,9 @@ def update_project(repo, username=None): return flask.redirect( flask.url_for('auth_login', next=url)) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to change the settings for this project') @@ -1027,15 +1016,12 @@ def update_priorities(repo, username=None): return flask.redirect( flask.url_for('auth_login', next=url)) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to change the settings for this project') @@ -1118,15 +1104,12 @@ def update_milestones(repo, username=None): return flask.redirect( flask.url_for('auth_login', next=url)) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo if not repo.settings.get('issue_tracker', True): flask.abort(404, 'No issue tracker found for this project') - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort( 403, 'You are not allowed to change the settings for this project') @@ -1565,12 +1548,9 @@ def regenerate_git(repo, username=None): return flask.redirect( flask.url_for('auth_login', next=url)) - repo = pagure.lib.get_project(SESSION, repo, user=username) - - if not repo: - flask.abort(404, 'Project not found') + repo = flask.g.repo - if not is_repo_admin(repo): + if not flask.g.repo_admin: flask.abort(403, 'You are not allowed to regenerate the git repos') regenerate = flask.request.form.get('regenerate')