diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index 34954de..47a4ede 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -19,6 +19,7 @@ import logging import os import re from collections import defaultdict +from functools import wraps from math import ceil import flask @@ -41,7 +42,21 @@ from pagure import (APP, SESSION, __get_file_in_tree, _log = logging.getLogger(__name__) -# URLs + +def has_issue_tracker(function): + """ + Decorator that checks if the current pagure project has the + issue tracker active + If not active returns a 404 page + """ + @wraps(function) + def check_issue_tracker(*args, **kwargs): + repo = flask.g.repo + if not repo.settings.get('issue_tracker', True): + flask.abort(404, 'No issue tracker found for this project') + return function(*args, **kwargs) + + return check_issue_tracker @APP.route( @@ -69,6 +84,7 @@ _log = logging.getLogger(__name__) '/fork////issue//update', methods=['GET', 'POST']) @login_required +@has_issue_tracker def update_issue(repo, issueid, username=None, namespace=None): ''' Add a comment to an issue. ''' is_js = flask.request.args.get('js', False) @@ -82,9 +98,6 @@ def update_issue(repo, issueid, username=None, namespace=None): 'view_issue', username=username, repo=repo.name, namespace=repo.namespace, issueid=issueid)) - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - issue = pagure.lib.search_issues(SESSION, repo, issueid=issueid) if issue is None or issue.project != repo: @@ -379,6 +392,7 @@ def update_issue(repo, issueid, username=None, namespace=None): '/fork////tag//edit', methods=('GET', 'POST')) @login_required +@has_issue_tracker def edit_tag(repo, tag, username=None, namespace=None): """ Edit the specified tag associated with the issues of a project. """ @@ -390,9 +404,6 @@ def edit_tag(repo, tag, username=None, namespace=None): 'You are not allowed to edit tags associated with the issues of \ this project') - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - tags = pagure.lib.get_tags_of_project(SESSION, repo) if not tags: flask.abort(404, 'Project has no tags to edit') @@ -448,15 +459,13 @@ def edit_tag(repo, tag, username=None, namespace=None): @APP.route('//update/tags', methods=['POST']) @APP.route('///update/tags', methods=['POST']) @login_required +@has_issue_tracker def update_tags(repo, username=None, namespace=None): """ Update the tags of a project. """ repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - if not flask.g.repo_admin: flask.abort( 403, @@ -541,6 +550,7 @@ def update_tags(repo, username=None, namespace=None): @APP.route('/fork///droptag/', methods=['POST']) @APP.route('/fork////droptag/', methods=['POST']) @login_required +@has_issue_tracker def remove_tag(repo, username=None, namespace=None): """ Remove the specified tag, associated with the issues, from the project. """ @@ -552,9 +562,6 @@ def remove_tag(repo, username=None, namespace=None): 'You are not allowed to remove tags associated with the issues \ of this project') - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - form = pagure.forms.DeleteIssueTagForm() if form.validate_on_submit(): tags = form.tag.data @@ -590,6 +597,7 @@ def remove_tag(repo, username=None, namespace=None): @APP.route('/fork///issues') @APP.route('/fork////issues/') @APP.route('/fork////issues') +@has_issue_tracker def view_issues(repo, username=None, namespace=None): """ List all issues associated to a repo """ @@ -622,9 +630,6 @@ def view_issues(repo, username=None, namespace=None): repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - try: priority = int(priority) except (ValueError, TypeError): @@ -745,6 +750,7 @@ def view_issues(repo, username=None, namespace=None): @APP.route('/fork///roadmap') @APP.route('/fork////roadmap/') @APP.route('/fork////roadmap') +@has_issue_tracker def view_roadmap(repo, username=None, namespace=None): """ List all issues associated to a repo as roadmap """ @@ -756,9 +762,6 @@ def view_roadmap(repo, username=None, namespace=None): repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - # Hide private tickets private = False # If user is authenticated, show him/her his/her private tickets @@ -869,14 +872,12 @@ def view_roadmap(repo, username=None, namespace=None): '/fork////new_issue', methods=('GET', 'POST')) @login_required +@has_issue_tracker def new_issue(repo, username=None, namespace=None): """ Create a new issue """ repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - form = pagure.forms.IssueFormSimplied() if form.validate_on_submit(): title = form.title.data @@ -991,15 +992,13 @@ def new_issue(repo, username=None, namespace=None): @APP.route('/fork///issue/') @APP.route('/fork////issue//') @APP.route('/fork////issue/') +@has_issue_tracker def view_issue(repo, issueid, username=None, namespace=None): """ List all issues associated to a repo """ repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - issue = pagure.lib.search_issues(SESSION, repo, issueid=issueid) if issue is None or issue.project != repo: @@ -1055,15 +1054,13 @@ def view_issue(repo, issueid, username=None, namespace=None): methods=['POST']) @APP.route('/fork////issue//drop', methods=['POST']) +@has_issue_tracker def delete_issue(repo, issueid, username=None, namespace=None): """ Delete the specified issue """ repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - issue = pagure.lib.search_issues(SESSION, repo, issueid=issueid) if issue is None or issue.project != repo: @@ -1114,14 +1111,12 @@ def delete_issue(repo, issueid, username=None, namespace=None): @APP.route('/fork////issue//edit', methods=('GET', 'POST')) @login_required +@has_issue_tracker def edit_issue(repo, issueid, username=None, namespace=None): """ Edit the specified issue """ repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - issue = pagure.lib.search_issues(SESSION, repo, issueid=issueid) if issue is None or issue.project != repo: @@ -1233,14 +1228,12 @@ def edit_issue(repo, issueid, username=None, namespace=None): @APP.route('/fork////issue//upload', methods=['POST']) @login_required +@has_issue_tracker def upload_issue(repo, issueid, username=None, namespace=None): ''' Upload a file to a ticket. ''' repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - issue = pagure.lib.search_issues(SESSION, repo, issueid=issueid) if issue is None or issue.project != repo: @@ -1293,6 +1286,7 @@ def upload_issue(repo, issueid, username=None, namespace=None): @APP.route('///issue/raw/') @APP.route('/fork///issue/raw/') @APP.route('/fork////issue/raw/') +@has_issue_tracker def view_issue_raw_file( repo, filename=None, username=None, namespace=None): """ Displays the raw content of a file of a commit for the specified @@ -1303,9 +1297,6 @@ def view_issue_raw_file( repo = flask.g.repo - if not repo.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - mimetype, encoding = mimetypes.guess_type(filename) attachdir = os.path.join(APP.config['ATTACHMENTS_FOLDER'], repo.fullname) @@ -1397,6 +1388,7 @@ def view_issue_raw_file( @APP.route('/fork////issue//comment' '//edit', methods=('GET', 'POST')) @login_required +@has_issue_tracker def edit_comment_issue( repo, issueid, commentid, username=None, namespace=None): """Edit comment of an issue @@ -1405,9 +1397,6 @@ def edit_comment_issue( project = flask.g.repo - if not project.settings.get('issue_tracker', True): - flask.abort(404, 'No issue tracker found for this project') - issue = pagure.lib.search_issues(SESSION, project, issueid=issueid) if issue is None or issue.project != project: diff --git a/tests/test_pagure_flask_ui_issues.py b/tests/test_pagure_flask_ui_issues.py index 33ef494..90109db 100644 --- a/tests/test_pagure_flask_ui_issues.py +++ b/tests/test_pagure_flask_ui_issues.py @@ -1386,9 +1386,6 @@ class PagureFlaskIssuestests(tests.Modeltests): self.session.commit() with tests.user_set(pagure.APP, user): - output = self.app.get('/test/issue/1/update') - self.assertEqual(output.status_code, 302) - # Repo not set-up for issue tracker output = self.app.post('/test/issue/1/update', data=data) self.assertEqual(output.status_code, 404)