mirror of https://git.sr.ht/~sircmpwn/hub.sr.ht
Implement creating new mailing lists
This commit is contained in:
parent
c47f6ff49a
commit
88af4176a7
|
@ -215,18 +215,33 @@ def mailing_lists_new_GET(owner, project_name):
|
|||
def mailing_lists_new_POST(owner, project_name):
|
||||
owner, project = get_project(owner, project_name, ProjectAccess.write)
|
||||
valid = Validation(request)
|
||||
if "create" in valid:
|
||||
assert False # TODO: Create list
|
||||
if "from-template" in valid:
|
||||
assert False # TODO: Create lists from template
|
||||
elif "create" in valid:
|
||||
mailing_list = lists.create_list(owner, valid)
|
||||
if not valid.ok:
|
||||
mls = lists.get_lists(owner)
|
||||
mls = sorted(mls, key=lambda r: r["updated"], reverse=True)
|
||||
return render_template("project-lists-new.html",
|
||||
view="new-resource", owner=owner, project=project,
|
||||
lists=mls, **valid.kwargs)
|
||||
else:
|
||||
list_name = None
|
||||
for field in valid.source:
|
||||
if field.startswith("existing-"):
|
||||
list_name = field[len("existing-"):]
|
||||
break
|
||||
if not list_name:
|
||||
search = valid.optional("searc")
|
||||
mls = lists.get_list(owner)
|
||||
# TODO: Search properly
|
||||
mls = filter(lambda r: search.lower() in r["name"].lower(), mls)
|
||||
mls = sorted(mls, key=lambda r: r["updated"], reverse=True)
|
||||
return render_template("project-lists-new.html",
|
||||
view="new-resource", owner=owner, project=project,
|
||||
lists=mls)
|
||||
mailing_list = lists.get_list(owner, list_name)
|
||||
|
||||
list_name = None
|
||||
for field in valid.source:
|
||||
if field.startswith("existing-"):
|
||||
list_name = field[len("existing-"):]
|
||||
break
|
||||
|
||||
mailing_list = lists.get_list(owner, list_name)
|
||||
ml = MailingList()
|
||||
ml.remote_id = mailing_list["id"]
|
||||
ml.project_id = project.id
|
||||
|
@ -244,7 +259,7 @@ def mailing_lists_new_POST(owner, project_name):
|
|||
db.session.add(event)
|
||||
|
||||
# TODO: lists webhooks for rigging up new mail events
|
||||
lists.ensure_mailing_list_webhooks(owner, list_name, {
|
||||
lists.ensure_mailing_list_webhooks(owner, ml.name, {
|
||||
origin + url_for("webhooks.mailing_list_update"):
|
||||
["list:update", "list:delete"],
|
||||
})
|
||||
|
|
|
@ -1,16 +1,36 @@
|
|||
import requests
|
||||
from abc import ABC
|
||||
from srht.api import ensure_webhooks, get_authorization, get_results
|
||||
from srht.config import get_origin
|
||||
|
||||
_gitsrht = get_origin("git.sr.ht", external=True, default=None)
|
||||
_listsrht = get_origin("lists.sr.ht", external=True, default=None)
|
||||
|
||||
class GitService:
|
||||
class SrhtService(ABC):
|
||||
def __init__(self):
|
||||
self.session = requests.Session()
|
||||
|
||||
def post(self, user, valid, url, payload):
|
||||
r = self.session.post(url,
|
||||
headers=get_authorization(user),
|
||||
json=payload)
|
||||
if r.status_code == 400:
|
||||
for error in r.json()["errors"]:
|
||||
valid.error(error["reason"], field=error.get("field"))
|
||||
return None
|
||||
elif r.status_code != 201:
|
||||
raise Exception(r.text)
|
||||
return r.json()
|
||||
|
||||
class GitService(SrhtService):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def get_repos(self, user):
|
||||
return get_results(f"{_gitsrht}/api/repos", user)
|
||||
|
||||
def get_repo(self, user, repo_name):
|
||||
r = requests.get(f"{_gitsrht}/api/repos/{repo_name}",
|
||||
r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code != 200:
|
||||
raise Exception(r.text)
|
||||
|
@ -19,7 +39,7 @@ class GitService:
|
|||
def get_readme(self, user, repo_name):
|
||||
# TODO: Cache?
|
||||
# TODO: Use default branch
|
||||
r = requests.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/README.md",
|
||||
r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/README.md",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code == 404:
|
||||
return ""
|
||||
|
@ -32,30 +52,21 @@ class GitService:
|
|||
description = valid.require("description")
|
||||
if not valid.ok:
|
||||
return None
|
||||
r = requests.post(f"{_gitsrht}/api/repos",
|
||||
headers=get_authorization(user),
|
||||
json={
|
||||
"name": name,
|
||||
"description": description,
|
||||
"visibility": "public", # TODO: Should this be different?
|
||||
})
|
||||
if r.status_code == 400:
|
||||
for error in r.json()["errors"]:
|
||||
valid.error(error["reason"], field=error.get("field"))
|
||||
return None
|
||||
elif r.status_code != 201:
|
||||
raise Exception(r.text)
|
||||
return r.json()
|
||||
return self.post(user, valid, f"{_gitsrht}/api/repos", {
|
||||
"name": name,
|
||||
"description": description,
|
||||
"visibility": "public", # TODO: Should this be different?
|
||||
})
|
||||
|
||||
def ensure_user_webhooks(self, user, config):
|
||||
ensure_webhooks(user, f"{_gitsrht}/api/user/webhooks", config)
|
||||
|
||||
class ListService():
|
||||
class ListService(SrhtService):
|
||||
def get_lists(self, user):
|
||||
return get_results(f"{_listsrht}/api/lists", user)
|
||||
|
||||
def get_list(self, user, list_name):
|
||||
r = requests.get(f"{_listsrht}/api/lists/{list_name}",
|
||||
r = self.session.get(f"{_listsrht}/api/lists/{list_name}",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code != 200:
|
||||
raise Exception(r.json())
|
||||
|
@ -65,5 +76,16 @@ class ListService():
|
|||
url = f"{_listsrht}/api/user/{user.canonical_name}/lists/{list_name}/webhooks"
|
||||
ensure_webhooks(user, url, config)
|
||||
|
||||
def create_list(self, user, valid):
|
||||
name = valid.require("name")
|
||||
description = valid.require("description")
|
||||
print(name, description)
|
||||
if not valid.ok:
|
||||
return None
|
||||
return self.post(user, valid, f"{_listsrht}/api/lists", {
|
||||
"name": name,
|
||||
"description": description,
|
||||
})
|
||||
|
||||
git = GitService()
|
||||
lists = ListService()
|
||||
|
|
|
@ -78,27 +78,27 @@
|
|||
<label for="{{ typename }}">Name</label>
|
||||
<input
|
||||
type="text"
|
||||
name="list_name"
|
||||
name="name"
|
||||
id="name"
|
||||
class="form-control {{ valid.cls("list_name") }}"
|
||||
value="{{ list_name or project.name }}" />
|
||||
{{ valid.summary("list_name") }}
|
||||
class="form-control {{ valid.cls("name") }}"
|
||||
value="{{ name or project.name }}" />
|
||||
{{ valid.summary("name") }}
|
||||
<div class="form-group">
|
||||
<label for="list_description">Description</label>
|
||||
<input
|
||||
type="text"
|
||||
name="list_description"
|
||||
id="list_description"
|
||||
class="form-control {{valid.cls("list_description")}}"
|
||||
value="{{ list_description or project.description }}" />
|
||||
{{valid.summary("list_description")}}
|
||||
name="description"
|
||||
id="description"
|
||||
class="form-control {{valid.cls("description")}}"
|
||||
value="{{ description or project.description }}" />
|
||||
{{valid.summary("description")}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex-grow-1 d-flex flex-row justify-content-end">
|
||||
<button
|
||||
type="submit"
|
||||
class="btn btn-primary align-self-end"
|
||||
name="new"
|
||||
name="create"
|
||||
>Create new mailing list {{icon("caret-right")}}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue