handle adding contributor that already exists

This commit is contained in:
SvyatoslavArtymovych 2023-04-25 11:20:37 +03:00
parent d9ad00e537
commit a1fa634c9d
2 changed files with 18 additions and 3 deletions

View File

@ -1,4 +1,4 @@
from flask import Blueprint, render_template, flash, redirect, url_for
from flask import Blueprint, render_template, flash, redirect, url_for, request
from flask_login import login_required, current_user
from app import models as m, db, forms as f
@ -72,15 +72,22 @@ def add_contributor(book_id):
form = f.AddContributorForm()
if form.validate_on_submit():
book_contributor = m.BookContributor.query.filter_by(
user_id=form.user_id.data, book_id=book_id
).first()
if book_contributor:
flash("Already exists!", "danger")
return redirect(url_for("book.settings", book_id=book_id))
role = m.BookContributor.Roles(int(form.role.data))
m.BookContributor(user_id=form.user_id.data, book_id=book_id, role=role).save()
flash("Contributor was added!", "success")
return redirect(url_for("book.get_all"))
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.get_all"))
return redirect(url_for("book.settings", book_id=book_id))

View File

@ -88,6 +88,14 @@ def test_add_contributor(client: FlaskClient):
assert response.status_code == 200
assert b"Contributor was added!" in response.data
response: Response = client.post(
f"/book/{book.id}/add_contributor",
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Already exists!" in response.data
contributor: m.BookContributor = m.BookContributor.query.filter_by(
user=moderator, book=book