Collection CRUD backend

This commit is contained in:
SvyatoslavArtymovych 2023-04-26 16:14:57 +03:00
parent e432fe1133
commit b3cf9a2f8a
6 changed files with 290 additions and 35 deletions

View File

@ -3,7 +3,10 @@ from .auth import LoginForm
from .user import UserForm, NewUserForm
from .book import (
CreateBookForm,
)
from .contributor import (
AddContributorForm,
DeleteContributorForm,
EditContributorRoleForm,
)
from .collection import CreateCollectionForm, EditCollectionForm

View File

@ -1,42 +1,8 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length
from app import models as m
class CreateBookForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(6, 1024)])
submit = SubmitField("Add new book")
class AddContributorForm(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("Add Contributor")
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")

17
app/forms/collection.py Normal file
View File

@ -0,0 +1,17 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length
class CreateCollectionForm(FlaskForm):
label = StringField("Label", [DataRequired(), Length(6, 1024)])
about = StringField("About")
submit = SubmitField("Create")
class EditCollectionForm(FlaskForm):
label = StringField("Label", [Length(6, 1024)])
about = StringField("About")
submit = SubmitField("Edit")

37
app/forms/contributor.py Normal file
View File

@ -0,0 +1,37 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms.validators import DataRequired
from app import models as m
class AddContributorForm(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("Add Contributor")
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")

View File

@ -330,3 +330,139 @@ def edit_contributor_role(book_id: int):
for error in errors:
flash(error.replace("Field", field_label), "danger")
return redirect(url_for("book.settings", book_id=book_id))
#################
# Collection CRUD
#################
@bp.route("/<int:book_id>/create_collection", methods=["POST"])
@login_required
def collection_create(book_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user or book.is_deleted:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
form = f.CreateCollectionForm()
if form.validate_on_submit():
label = form.label.data
collection: m.Collection = m.Collection.query.filter_by(
is_deleted=False, label=label
).first()
if collection:
log(
log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
book.id,
collection.id,
label,
)
flash("Collection label must be unique!", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
collection: m.Collection = m.Collection(
label=label, about=form.about.data, version_id=book.versions[-1].id
)
log(log.INFO, "Create collection [%s]. Book: [%s]", collection, book.id)
collection.save()
flash("Success!", "success")
return redirect(url_for("book.collection_view", 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))
@bp.route("/<int:book_id>/<int:collection_id>/edit", methods=["POST"])
@login_required
def collection_edit(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user or book.is_deleted:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
form = f.EditCollectionForm()
redirect_url = url_for(
"book.sub_collection_view",
book_id=book_id,
collection_id=collection_id,
)
if form.validate_on_submit():
label = form.label.data
if m.Collection.query.filter_by(label=label).first():
log(
log.INFO,
"Collection with similar label already exists. Book: [%s], Collection: [%s], Label: [%s]",
book.id,
collection.id,
label,
)
flash("Collection label must be unique!", "danger")
return redirect(redirect_url)
if label:
collection.label = label
about = form.about.data
if about:
collection.about = about
log(log.INFO, "Edit collection [%s]", collection.id)
collection.save()
flash("Success!", "success")
return redirect(redirect_url)
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(redirect_url)
@bp.route("/<int:book_id>/<int:collection_id>/delete", methods=["POST"])
@login_required
def collection_delete(book_id: int, collection_id: int):
book: m.Book = db.session.get(m.Book, book_id)
if not book or book.owner != current_user:
log(log.INFO, "User: [%s] is not owner of book: [%s]", current_user, book)
flash("You are not owner of this book!", "danger")
return redirect(url_for("book.my_books"))
collection: m.Collection = db.session.get(m.Collection, collection_id)
if not collection or collection.is_deleted:
log(log.WARNING, "Collection with id [%s] not found", collection_id)
flash("Collection not found", "danger")
return redirect(url_for("book.collection_view", book_id=book_id))
collection.is_deleted = True
log(log.INFO, "Delete collection [%s]", collection.id)
collection.save()
flash("Success!", "success")
return redirect(
url_for(
"book.collection_view",
book_id=book_id,
)
)

View File

@ -184,3 +184,99 @@ def test_edit_contributor_role(client: FlaskClient, runner: FlaskCliRunner):
assert response.status_code == 200
assert b"Success!" in response.data
def test_crud_collection(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()
response: Response = client.post(
f"/book/{book.id}/create_collection",
data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
response: Response = client.post(
f"/book/{book.id}/create_collection",
data=dict(label="Test Collection #1 Label", about="Test Collection #1 About"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
collection: m.Collection = m.Collection.query.filter_by(
label="Test Collection #1 Label"
).first()
response: Response = client.post(
f"/book/{book.id}/{collection.id}/edit",
data=dict(
label="Test Collection #1 Label",
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection label must be unique!" in response.data
new_label = "Test Collection #1 Label(edited)"
new_about = "Test Collection #1 About(edited)"
response: Response = client.post(
f"/book/{book.id}/{collection.id}/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
edited_collection: m.Collection = m.Collection.query.filter_by(
label=new_label, about=new_about
).first()
assert edited_collection
response: Response = client.post(
f"/book/{book.id}/0/edit",
data=dict(
label=new_label,
about=new_about,
),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data
response: Response = client.post(
f"/book/{book.id}/{collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Success!" in response.data
deleted_collection: m.Collection = db.session.get(m.Collection, collection.id)
assert deleted_collection.is_deleted
response: Response = client.post(
f"/book/{book.id}/{collection.id}/delete",
follow_redirects=True,
)
assert response.status_code == 200
assert b"Collection not found" in response.data