From 2b846b1e6b90b45679e302eefc245363a26c862d Mon Sep 17 00:00:00 2001 From: Jan Broer Date: Tue, 13 Oct 2015 19:12:18 +0200 Subject: [PATCH] Make shelf names required unique --- cps/web.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cps/web.py b/cps/web.py index 8fb9f9d5..fe8fd8cb 100755 --- a/cps/web.py +++ b/cps/web.py @@ -477,12 +477,16 @@ def create_shelf(): shelf.is_public = 1 shelf.name = to_save["title"] shelf.user_id = int(current_user.id) - try: - ub.session.add(shelf) - ub.session.commit() - flash("Shelf %s created" % to_save["title"], category="success") - except: - flash("there was an error", category="error") + existing_shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.name == shelf.name).first() + if existing_shelf: + flash("A shelf with the name '%s' already exists." % to_save["title"], category="error") + else: + try: + ub.session.add(shelf) + ub.session.commit() + flash("Shelf %s created" % to_save["title"], category="success") + except: + flash("There was an error", category="error") return render_template('shelf_edit.html', title="create a shelf") else: return render_template('shelf_edit.html', title="create a shelf")