mirror of https://github.com/logos-co/open-law.git
Edit book label
This commit is contained in:
parent
727b793e48
commit
0a6c5d3544
|
@ -1,9 +1,7 @@
|
|||
# flake8: noqa F401
|
||||
from .auth import LoginForm
|
||||
from .user import UserForm, NewUserForm
|
||||
from .book import (
|
||||
CreateBookForm,
|
||||
)
|
||||
from .book import CreateBookForm, EditBookForm
|
||||
from .contributor import (
|
||||
AddContributorForm,
|
||||
DeleteContributorForm,
|
||||
|
|
|
@ -3,6 +3,13 @@ from wtforms import StringField, SubmitField
|
|||
from wtforms.validators import DataRequired, Length
|
||||
|
||||
|
||||
class CreateBookForm(FlaskForm):
|
||||
class BaseBookForm(FlaskForm):
|
||||
label = StringField("Label", [DataRequired(), Length(6, 256)])
|
||||
|
||||
|
||||
class CreateBookForm(BaseBookForm):
|
||||
submit = SubmitField("Add new book")
|
||||
|
||||
|
||||
class EditBookForm(BaseBookForm):
|
||||
submit = SubmitField("Edit book")
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -7,6 +7,23 @@
|
|||
<!-- prettier-ignore -->
|
||||
{% block right_sidebar %} {% endblock %}
|
||||
|
||||
<div class="p-5">
|
||||
<div class="flex justify-between ml-4 mb-3">
|
||||
<h1 class="text-2xl font-extrabold dark:text-white">Settings</h1>
|
||||
</div>
|
||||
|
||||
<form action="{{ url_for('book.edit', book_id=book.id) }}" method="post" class="mb-0 flex flex-col space-y-2 w-1/2">
|
||||
{{ form_hidden_tag() }}
|
||||
<div>
|
||||
<label for="label" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Label</label>
|
||||
<input value="{{book.label}}" type="text" name="label" id="label" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 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" placeholder="My Book" required>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit" class="text-center 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-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="p-5">
|
||||
<div class="flex justify-between ml-4 mb-2">
|
||||
<h1 class="text-2xl font-extrabold dark:text-white">Contributors</h1>
|
||||
|
|
|
@ -76,6 +76,40 @@ def create():
|
|||
return redirect(url_for("book.my_books"))
|
||||
|
||||
|
||||
@bp.route("/<int:book_id>/edit", methods=["POST"])
|
||||
@login_required
|
||||
def edit(book_id: int):
|
||||
form = f.EditBookForm()
|
||||
if form.validate_on_submit():
|
||||
book: m.Book = db.session.get(m.Book, book_id)
|
||||
if not book or book.is_deleted:
|
||||
log(log.WARNING, "Book with id [%s] not found", book_id)
|
||||
flash("Book not found", "danger")
|
||||
return redirect(url_for("book.my_books"))
|
||||
label = form.label.data
|
||||
existing_book: m.Book = m.Book.query.filter_by(
|
||||
is_deleted=False,
|
||||
label=label,
|
||||
).first()
|
||||
if existing_book:
|
||||
log(log.WARNING, "Book with id [%s] not found", book_id)
|
||||
flash("Book label must be unique!", "danger")
|
||||
return redirect(url_for("book.my_books"))
|
||||
|
||||
book.label = label
|
||||
log(log.INFO, "Update Book: [%s]", book)
|
||||
book.save()
|
||||
flash("Success!", "success")
|
||||
return redirect(url_for("book.my_books"))
|
||||
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.collection_view", book_id=book_id))
|
||||
|
||||
|
||||
@bp.route("/<int:book_id>/collections", methods=["GET"])
|
||||
def collection_view(book_id: int):
|
||||
book = db.session.get(m.Book, book_id)
|
||||
|
|
|
@ -5,7 +5,7 @@ from app import models as m, db
|
|||
from tests.utils import login
|
||||
|
||||
|
||||
def test_create_book(client: FlaskClient):
|
||||
def test_create_edit_book(client: FlaskClient):
|
||||
login(client)
|
||||
|
||||
BOOK_NAME = "Test Book"
|
||||
|
@ -61,6 +61,41 @@ def test_create_book(client: FlaskClient):
|
|||
assert book.versions
|
||||
assert len(book.versions) == 1
|
||||
|
||||
response: Response = client.post(
|
||||
"/book/9999/edit",
|
||||
data=dict(
|
||||
label=BOOK_NAME,
|
||||
),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Book not found" in response.data
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/edit",
|
||||
data=dict(
|
||||
label=BOOK_NAME,
|
||||
),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Book label must be unique!" in response.data
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/edit",
|
||||
data=dict(
|
||||
label=BOOK_NAME + " EDITED",
|
||||
),
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Success!" in response.data
|
||||
book = db.session.get(m.Book, book.id)
|
||||
assert book.label != BOOK_NAME
|
||||
|
||||
|
||||
def test_add_contributor(client: FlaskClient):
|
||||
_, user = login(client)
|
||||
|
|
Loading…
Reference in New Issue