Prevent naming a package the same as a collection

This commit is contained in:
rubenwardy 2023-08-16 01:03:54 +01:00
parent ea2f1f4f6f
commit eb81674f06

@ -281,7 +281,7 @@ class PackageForm(FlaskForm):
def handle_create_edit(package: typing.Optional[Package], form: PackageForm, author: User):
wasNew = False
if package is None:
package = Package.query.filter_by(name=form["name"].data, author_id=author.id).first()
package = Package.query.filter_by(name=form.name.data, author_id=author.id).first()
if package is not None:
if package.state == PackageState.DELETED:
flash(
@ -293,6 +293,12 @@ def handle_create_edit(package: typing.Optional[Package], form: PackageForm, aut
gettext("Package already exists")), "danger")
return None
if Collection.query \
.filter(Collection.name == form.name.data, Collection.author == author) \
.count() > 0:
flash(gettext("A collection with a similar name already exists"), "danger")
return
package = Package()
db.session.add(package)
package.author = author