Delete contributor feat

This commit is contained in:
SvyatoslavArtymovych 2023-04-25 11:56:51 +03:00
parent 65db9dd1a8
commit 723fc1f862
6 changed files with 87 additions and 8 deletions

View File

@ -1,4 +1,4 @@
# flake8: noqa F401
from .auth import LoginForm
from .user import UserForm, NewUserForm
from .book import CreateBookForm, AddContributorForm
from .book import CreateBookForm, AddContributorForm, DeleteContributorForm

View File

@ -21,3 +21,9 @@ class AddContributorForm(FlaskForm):
)
submit = SubmitField("Add Contributor")
class DeleteContributorForm(FlaskForm):
user_id = StringField("User ID", [DataRequired()])
submit = SubmitField("Delete Contributor")

File diff suppressed because one or more lines are too long

View File

@ -34,7 +34,14 @@
<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 }}</td>
<td class="px-6 py-4"></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">
<input type="hidden" name="user_id" id="user_id" value="{{ contributor.user_id }}" />
<button type="submit" class="text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-sm rounded-lg text-sm px-5 py-1.5 dark:bg-red-600 dark:hover:bg-red-700 focus:outline-none dark:focus:ring-red-800">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>

View File

@ -1,4 +1,4 @@
from flask import Blueprint, render_template, flash, redirect, url_for, request
from flask import Blueprint, render_template, flash, redirect, url_for
from flask_login import login_required, current_user
from app import models as m, db, forms as f
@ -63,7 +63,6 @@ def settings(book_id):
@bp.route("/<int:book_id>/add_contributor", methods=["POST"])
@login_required
def add_contributor(book_id):
# TODO replace redirects to book.edit/settings
book: m.Book = db.session.get(m.Book, book_id)
if book.owner != current_user:
flash("You are not owner of this book!", "danger")
@ -91,3 +90,35 @@ def add_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>/delete_contributor", methods=["POST"])
@login_required
def delete_contributor(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.DeleteContributorForm()
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))
db.session.delete(book_contributor)
db.session.commit()
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))

View File

@ -1,7 +1,7 @@
from flask import current_app as Response
from flask.testing import FlaskClient
from flask.testing import FlaskClient, FlaskCliRunner
from app import models as m
from app import models as m, db
from tests.utils import login
@ -118,3 +118,38 @@ def test_add_contributor(client: FlaskClient):
).first()
assert contributor.role == m.BookContributor.Roles.EDITOR
assert len(book.contributors) == 2
def test_delete_contributor(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_to_delete = book.contributors[0]
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"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