mirror of https://github.com/logos-co/open-law.git
edit contributor feature
This commit is contained in:
parent
45b03ae311
commit
30c3788ea8
|
@ -1,4 +1,9 @@
|
|||
# flake8: noqa F401
|
||||
from .auth import LoginForm
|
||||
from .user import UserForm, NewUserForm
|
||||
from .book import CreateBookForm, AddContributorForm, DeleteContributorForm
|
||||
from .book import (
|
||||
CreateBookForm,
|
||||
AddContributorForm,
|
||||
DeleteContributorForm,
|
||||
EditContributorRoleForm,
|
||||
)
|
||||
|
|
|
@ -27,3 +27,16 @@ class DeleteContributorForm(FlaskForm):
|
|||
user_id = StringField("User ID", [DataRequired()])
|
||||
|
||||
submit = SubmitField("Delete Contributor")
|
||||
|
||||
|
||||
class EditContributorRoleForm(FlaskForm):
|
||||
user_id = StringField("User ID", [DataRequired()])
|
||||
role = SelectField(
|
||||
"Role",
|
||||
choices=[
|
||||
(member.value, name.capitalize())
|
||||
for name, member in m.BookContributor.Roles.__members__.items()
|
||||
],
|
||||
)
|
||||
|
||||
submit = SubmitField("Edit Contributor")
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -33,7 +33,27 @@
|
|||
{% for contributor in book.contributors %}
|
||||
<tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
|
||||
<td class="px-6 py-4">{{ contributor.user.username }}</td>
|
||||
<td class="px-6 py-4">{{ contributor.role.name.title() }}</td>
|
||||
<td class="px-6 py-4">
|
||||
<form action="{{ url_for('book.edit_contributor_role', book_id=book.id) }}" method="post" class="mb-0 shadow flex space-x-2">
|
||||
<input type="hidden" name="user_id" id="user_id" value="{{ contributor.user_id }}" />
|
||||
<select
|
||||
id="role"
|
||||
name="role"
|
||||
data-user-id="{{ contributor.user.id }}"
|
||||
class="contributor-role-select block w-1/2 py-1.5 px-2 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
|
||||
>
|
||||
|
||||
{% for role in roles if role.value %}
|
||||
<option
|
||||
{% if contributor.role == role %} selected {% endif %}
|
||||
value="{{ role.value }}">
|
||||
{{ role.name.title() }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-1 text-center inline-flex items-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Save</button>
|
||||
</form>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<!-- prettier-ignore -->
|
||||
<form action="{{ url_for('book.delete_contributor', book_id=book.id) }}" method="post" class="mb-0 shadow">
|
||||
|
|
|
@ -122,3 +122,36 @@ def delete_contributor(book_id):
|
|||
for error in errors:
|
||||
flash(error.replace("Field", field_label), "danger")
|
||||
return redirect(url_for("book.settings", book_id=book_id))
|
||||
|
||||
|
||||
@bp.route("/<int:book_id>/edit_contributor_role", methods=["POST"])
|
||||
@login_required
|
||||
def edit_contributor_role(book_id):
|
||||
book: m.Book = db.session.get(m.Book, book_id)
|
||||
if book.owner != current_user:
|
||||
flash("You are not owner of this book!", "danger")
|
||||
return redirect(url_for("book.get_all"))
|
||||
|
||||
form = f.EditContributorRoleForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
book_contributor = m.BookContributor.query.filter_by(
|
||||
user_id=int(form.user_id.data), book_id=book.id
|
||||
).first()
|
||||
if not book_contributor:
|
||||
flash("Does not exists!", "success")
|
||||
return redirect(url_for("book.settings", book_id=book_id))
|
||||
|
||||
role = m.BookContributor.Roles(int(form.role.data))
|
||||
book_contributor.role = role
|
||||
book_contributor.save()
|
||||
|
||||
flash("Success!", "success")
|
||||
return redirect(url_for("book.settings", book_id=book_id))
|
||||
else:
|
||||
log(log.ERROR, "Book create errors: [%s]", form.errors)
|
||||
for field, errors in form.errors.items():
|
||||
field_label = form._fields[field].label.text
|
||||
for error in errors:
|
||||
flash(error.replace("Field", field_label), "danger")
|
||||
return redirect(url_for("book.settings", book_id=book_id))
|
||||
|
|
|
@ -153,3 +153,43 @@ def test_delete_contributor(client: FlaskClient, runner: FlaskCliRunner):
|
|||
|
||||
assert response.status_code == 200
|
||||
assert b"Does not exists!" in response.data
|
||||
|
||||
|
||||
def test_edit_contributor_role(client: FlaskClient, runner: FlaskCliRunner):
|
||||
_, user = login(client)
|
||||
user: m.User
|
||||
|
||||
# add dummmy data
|
||||
runner.invoke(args=["db-populate"])
|
||||
|
||||
book = db.session.get(m.Book, 1)
|
||||
book.user_id = user.id
|
||||
book.save()
|
||||
|
||||
contributors_len = len(book.contributors)
|
||||
assert contributors_len
|
||||
|
||||
contributor_edit = book.contributors[0]
|
||||
|
||||
assert contributor_edit.role == m.BookContributor.Roles.MODERATOR
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/edit_contributor_role",
|
||||
data=dict(
|
||||
user_id=contributor_edit.user_id,
|
||||
role=m.BookContributor.Roles.MODERATOR.value,
|
||||
),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Success!" in response.data
|
||||
|
||||
# response: Response = client.post(
|
||||
# f"/book/{book.id}/delete_contributor",
|
||||
# data=dict(user_id=contributor_to_delete.user_id),
|
||||
# follow_redirects=True,
|
||||
# )
|
||||
|
||||
# assert response.status_code == 200
|
||||
# assert b"Does not exists!" in response.data
|
||||
|
|
Loading…
Reference in New Issue