Move sources routes to separate blueprint

This commit is contained in:
Drew DeVault 2020-04-01 13:30:57 -04:00
parent b6b673e4d7
commit 3b7b28a5cf
8 changed files with 172 additions and 161 deletions

View File

@ -13,11 +13,13 @@ class HubApp(SrhtFlask):
from hubsrht.blueprints.projects import projects
from hubsrht.blueprints.public import public
from hubsrht.blueprints.sources import sources
from hubsrht.blueprints.users import users
from hubsrht.blueprints.webhooks import webhooks
self.register_blueprint(projects)
self.register_blueprint(public)
self.register_blueprint(sources)
self.register_blueprint(users)
self.register_blueprint(webhooks)

View File

@ -67,157 +67,6 @@ def create_POST():
owner=current_user.canonical_name,
project_name=project.name))
@projects.route("/<owner>/<project_name>/sources")
@loginrequired
def sources_GET(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.read)
sources = (SourceRepo.query
.filter(SourceRepo.project_id == project.id)
.order_by(SourceRepo.updated.desc()))
terms = request.args.get("search")
search_error = None
try:
sources = search_by(sources, terms,
[SourceRepo.name, SourceRepo.description])
except ValueError as ex:
search_error = str(ex)
sources, pagination = paginate_query(sources)
return render_template("project-sources.html", view="sources",
owner=owner, project=project, sources=sources,
search=terms, search_error=search_error,
**pagination)
@projects.route("/<owner>/<project_name>/sources/new")
@loginrequired
def sources_new_GET(owner, project_name):
# TODO: Redirect appropriately if this instance only has git or hg support
owner, project = get_project(owner, project_name, ProjectAccess.write)
return render_template("project-sources-new.html", view="new-resource",
owner=owner, project=project)
@projects.route("/<owner>/<project_name>/sources/new", methods=["POST"])
@loginrequired
def sources_new_POST(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
valid = Validation(request)
if "git" in valid:
return redirect(url_for("projects.sources_git_new_GET",
owner=owner.canonical_name, project_name=project.name))
if "hg" in valid:
# TODO: Hg repos
return redirect(url_for("projects.sources_hg_new_GET",
owner=owner.canonical_name, project_name=project.name))
@projects.route("/<owner>/<project_name>/git/new")
@loginrequired
def sources_git_new_GET(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
# TODO: Pagination
repos = git.get_repos(owner)
repos = sorted(repos, key=lambda r: r["updated"], reverse=True)
return render_template("project-sources-select.html",
view="new-resource", vcs="git",
owner=owner, project=project, repos=repos,
existing=[]) # TODO: Fetch existing repos for this project
@projects.route("/<owner>/<project_name>/git/new", methods=["POST"])
@loginrequired
def sources_git_new_POST(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
valid = Validation(request)
if "create" in valid:
git_repo = git.create_repo(owner, valid)
if not valid.ok:
repos = git.get_repos(owner)
return render_template("project-sources-select.html",
view="new-resource", vcs="git",
owner=owner, project=project, repos=repos,
existing=[], **valid.kwargs)
else:
repo_name = None
for field in valid.source:
if field.startswith("existing-"):
repo_name = field[len("existing-"):]
break
if not repo_name:
search = valid.optional("search")
repos = git.get_repos(owner)
# TODO: Search properly
repos = filter(lambda r: search.lower() in r["name"].lower(), repos)
repos = sorted(repos, key=lambda r: r["updated"], reverse=True)
# TODO: Fetch existing repos for this project
return render_template("project-sources-select.html",
view="new-resource", vcs="git",
owner=owner, project=project, repos=repos,
existing=[], search=search)
git_repo = git.get_repo(owner, repo_name)
repo = SourceRepo()
repo.remote_id = git_repo["id"]
repo.project_id = project.id
repo.owner_id = owner.id
repo.name = git_repo["name"]
repo.description = git_repo["description"]
repo.repo_type = RepoType.git
db.session.add(repo)
db.session.flush()
event = Event()
event.event_type = EventType.source_repo_added
event.source_repo_id = repo.id
event.project_id = project.id
event.user_id = project.owner_id
db.session.add(event)
git.ensure_user_webhooks(owner)
git.ensure_repo_webhooks(owner, repo.name)
db.session.commit()
return redirect(url_for("projects.summary_GET",
owner=owner.canonical_name, project_name=project.name))
@projects.route("/<owner>/<project_name>/sources/manage")
@loginrequired
def sources_manage_GET(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
sources = (SourceRepo.query
.filter(SourceRepo.project_id == project.id)
.order_by(SourceRepo.updated.desc()))
terms = request.args.get("search")
search_error = None
try:
sources = search_by(sources, terms,
[SourceRepo.name, SourceRepo.description])
except ValueError as ex:
search_error = str(ex)
sources, pagination = paginate_query(sources)
return render_template("project-sources-manage.html", view="sources",
owner=owner, project=project, sources=sources,
search=terms, search_error=search_error,
**pagination)
@projects.route("/<owner>/<project_name>/sources/set-summary/<int:repo_id>",
methods=["POST"])
@loginrequired
def set_summary_repo(owner, project_name, repo_id):
owner, project = get_project(owner, project_name, ProjectAccess.write)
repo = (SourceRepo.query
.filter(SourceRepo.id == repo_id)
.filter(SourceRepo.project_id == project.id)).one_or_none()
if not repo:
abort(404)
project.summary_repo_id = repo.id
db.session.commit()
return redirect(url_for("projects.summary_GET",
owner=owner.canonical_name, project_name=project.name))
@projects.route("/<owner>/<project_name>/lists")
@loginrequired
def mailing_lists_GET(owner, project_name):

View File

@ -0,0 +1,148 @@
from flask import Blueprint, render_template, request, redirect, url_for
from hubsrht.projects import ProjectAccess, get_project
from hubsrht.services import git
from hubsrht.types import RepoType, SourceRepo
from srht.database import db
from srht.flask import paginate_query
from srht.oauth import loginrequired
from srht.search import search_by
sources = Blueprint("sources", __name__)
@sources.route("/<owner>/<project_name>/sources")
@loginrequired
def list_GET(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.read)
sources = (SourceRepo.query
.filter(SourceRepo.project_id == project.id)
.order_by(SourceRepo.updated.desc()))
terms = request.args.get("search")
search_error = None
try:
sources = search_by(sources, terms,
[SourceRepo.name, SourceRepo.description])
except ValueError as ex:
search_error = str(ex)
sources, pagination = paginate_query(sources)
return render_template("project-sources.html", view="sources",
owner=owner, project=project, sources=sources,
search=terms, search_error=search_error,
**pagination)
@sources.route("/<owner>/<project_name>/sources/new")
@loginrequired
def new_GET(owner, project_name):
# TODO: Redirect appropriately if this instance only has git or hg support
owner, project = get_project(owner, project_name, ProjectAccess.write)
return render_template("project-sources-new.html", view="new-resource",
owner=owner, project=project)
@sources.route("/<owner>/<project_name>/git/new")
@loginrequired
def git_new_GET(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
# TODO: Pagination
repos = git.get_repos(owner)
repos = sorted(repos, key=lambda r: r["updated"], reverse=True)
return render_template("project-sources-select.html",
view="new-resource", vcs="git",
owner=owner, project=project, repos=repos,
existing=[]) # TODO: Fetch existing repos for this project
@sources.route("/<owner>/<project_name>/git/new", methods=["POST"])
@loginrequired
def git_new_POST(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
valid = Validation(request)
if "create" in valid:
git_repo = git.create_repo(owner, valid)
if not valid.ok:
repos = git.get_repos(owner)
return render_template("project-sources-select.html",
view="new-resource", vcs="git",
owner=owner, project=project, repos=repos,
existing=[], **valid.kwargs)
else:
repo_name = None
for field in valid.source:
if field.startswith("existing-"):
repo_name = field[len("existing-"):]
break
if not repo_name:
search = valid.optional("search")
repos = git.get_repos(owner)
# TODO: Search properly
repos = filter(lambda r: search.lower() in r["name"].lower(), repos)
repos = sorted(repos, key=lambda r: r["updated"], reverse=True)
# TODO: Fetch existing repos for this project
return render_template("project-sources-select.html",
view="new-resource", vcs="git",
owner=owner, project=project, repos=repos,
existing=[], search=search)
git_repo = git.get_repo(owner, repo_name)
repo = SourceRepo()
repo.remote_id = git_repo["id"]
repo.project_id = project.id
repo.owner_id = owner.id
repo.name = git_repo["name"]
repo.description = git_repo["description"]
repo.repo_type = RepoType.git
db.session.add(repo)
db.session.flush()
event = Event()
event.event_type = EventType.source_repo_added
event.source_repo_id = repo.id
event.project_id = project.id
event.user_id = project.owner_id
db.session.add(event)
git.ensure_user_webhooks(owner)
git.ensure_repo_webhooks(owner, repo.name)
db.session.commit()
return redirect(url_for("projects.summary_GET",
owner=owner.canonical_name, project_name=project.name))
@sources.route("/<owner>/<project_name>/sources/manage")
@loginrequired
def manage_GET(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
sources = (SourceRepo.query
.filter(SourceRepo.project_id == project.id)
.order_by(SourceRepo.updated.desc()))
terms = request.args.get("search")
search_error = None
try:
sources = search_by(sources, terms,
[SourceRepo.name, SourceRepo.description])
except ValueError as ex:
search_error = str(ex)
sources, pagination = paginate_query(sources)
return render_template("project-sources-manage.html", view="sources",
owner=owner, project=project, sources=sources,
search=terms, search_error=search_error,
**pagination)
@sources.route("/<owner>/<project_name>/sources/summary/<int:repo_id>",
methods=["POST"])
@loginrequired
def summary_POST(owner, project_name, repo_id):
owner, project = get_project(owner, project_name, ProjectAccess.write)
repo = (SourceRepo.query
.filter(SourceRepo.id == repo_id)
.filter(SourceRepo.project_id == project.id)).one_or_none()
if not repo:
abort(404)
project.summary_repo_id = repo.id
db.session.commit()
return redirect(url_for("projects.summary_GET",
owner=owner.canonical_name, project_name=project.name))

View File

@ -14,7 +14,11 @@
{% endif %}
<form class="container">
<div class="row">
{% if current_user and current_user.id == owner.id %}
<div class="form-group col-md-12">
{% else %}
<div class="form-group col-md-10">
{% endif %}
<input
name="search"
type="text"

View File

@ -16,7 +16,7 @@
</li>
{% if any(project.source_repos) %}
<li class="nav-item">
{{link(url_for("projects.sources_GET",
{{link(url_for("sources.list_GET",
owner=owner.canonical_name,
project_name=project.name), "sources")}}
</li>

View File

@ -41,7 +41,7 @@
<form
class="d-inline"
method="POST"
action="{{url_for("projects.set_summary_repo",
action="{{url_for("sources.summary_POST",
owner=project.owner.canonical_name,
project_name=project.name,
repo_id=repo.id)}}"

View File

@ -14,7 +14,11 @@
{% endif %}
<form class="container">
<div class="row">
{% if current_user and current_user.id == owner.id %}
<div class="form-group col-md-12">
{% else %}
<div class="form-group col-md-10">
{% endif %}
<input
name="search"
type="text"
@ -46,12 +50,12 @@
{% if current_user and current_user.id == owner.id %}
<div class="col-md-2">
<a
href="{{url_for("projects.sources_new_GET",
href="{{url_for("sources.new_GET",
owner=owner.canonical_name, project_name=project.name)}}"
class="btn btn-primary btn-block"
>Add repository&nbsp;{{icon('caret-right')}}</a>
<a
href="{{url_for("projects.sources_manage_GET",
href="{{url_for("sources.manage_GET",
owner=owner.canonical_name, project_name=project.name)}}"
class="btn btn-default btn-block"
>Manage sources&nbsp;{{icon('caret-right')}}</a>

View File

@ -35,7 +35,7 @@
{% else %}
{{icon('plus-square', cls='text-info')}}
<a
href="{{url_for("projects.sources_new_GET",
href="{{url_for("sources.new_GET",
owner=owner.canonical_name, project_name=project.name)}}"
>Add source code repositories&nbsp;{{icon('arrow-right')}}</a>
<br />
@ -125,13 +125,17 @@
<div class="col-md-10">
{% if current_user and current_user.id == project.owner_id and not summary %}
<div class="alert alert-danger">
<strong>Head's up!</strong> This project is configured to use the
README.md file from
<strong>Head's up!</strong> This project is configured to use
"<code>README.md</code>" from
<a href="{{project.summary_repo.url()}}">
{{project.summary_repo.owner.canonical_name}}/{{project.summary_repo.name}}
</a>
as the project summary - but there is no such file. Push a README.md
file to update the project summary.
as the project summary, but there is no such file. Push one to update
the project summary, or
<a
href="{{url_for("sources.manage_GET",
owner=owner.canonical_name, project_name=project.name)}}"
>choose a different repository</a>.
</div>
{% else %}
{{summary | extended_md}}
@ -151,7 +155,7 @@
<form
class="event"
method="POST"
action="{{url_for("projects.set_summary_repo",
action="{{url_for("sources.summary_POST",
owner=project.owner.canonical_name,
project_name=project.name,
repo_id=repo.id)}}"