mirror of
				https://github.com/janeczku/calibre-web
				synced 2025-10-31 15:23:02 +00:00 
			
		
		
		
	Fixes for handling shelf requests without title
This commit is contained in:
		
							
								
								
									
										36
									
								
								cps/shelf.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								cps/shelf.py
									
									
									
									
									
								
							| @@ -72,7 +72,7 @@ def add_to_shelf(shelf_id, book_id): | |||||||
|  |  | ||||||
|     if not check_shelf_edit_permissions(shelf): |     if not check_shelf_edit_permissions(shelf): | ||||||
|         if not xhr: |         if not xhr: | ||||||
|             flash(_(u"Sorry you are not allowed to add a book to the the shelf"), category="error") |             flash(_(u"Sorry you are not allowed to add a book to that shelf"), category="error") | ||||||
|             return redirect(url_for('web.index')) |             return redirect(url_for('web.index')) | ||||||
|         return "Sorry you are not allowed to add a book to the that shelf", 403 |         return "Sorry you are not allowed to add a book to the that shelf", 403 | ||||||
|  |  | ||||||
| @@ -227,7 +227,7 @@ def remove_from_shelf(shelf_id, book_id): | |||||||
| @login_required | @login_required | ||||||
| def create_shelf(): | def create_shelf(): | ||||||
|     shelf = ub.Shelf() |     shelf = ub.Shelf() | ||||||
|     return create_edit_shelf(shelf, title=_(u"Create a Shelf"), page="shelfcreate") |     return create_edit_shelf(shelf, page_title=_(u"Create a Shelf"), page="shelfcreate") | ||||||
|  |  | ||||||
|  |  | ||||||
| @shelf.route("/shelf/edit/<int:shelf_id>", methods=["GET", "POST"]) | @shelf.route("/shelf/edit/<int:shelf_id>", methods=["GET", "POST"]) | ||||||
| @@ -237,11 +237,11 @@ def edit_shelf(shelf_id): | |||||||
|     if not check_shelf_edit_permissions(shelf): |     if not check_shelf_edit_permissions(shelf): | ||||||
|         flash(_(u"Sorry you are not allowed to edit this shelf"), category="error") |         flash(_(u"Sorry you are not allowed to edit this shelf"), category="error") | ||||||
|         return redirect(url_for('web.index')) |         return redirect(url_for('web.index')) | ||||||
|     return create_edit_shelf(shelf, title=_(u"Edit a shelf"), page="shelfedit", shelf_id=shelf_id) |     return create_edit_shelf(shelf, page_title=_(u"Edit a shelf"), page="shelfedit", shelf_id=shelf_id) | ||||||
|  |  | ||||||
|  |  | ||||||
| # if shelf ID is set, we are editing a shelf | # if shelf ID is set, we are editing a shelf | ||||||
| def create_edit_shelf(shelf, title, page, shelf_id=False): | def create_edit_shelf(shelf, page_title, page, shelf_id=False): | ||||||
|     sync_only_selected_shelves = current_user.kobo_only_shelves_sync |     sync_only_selected_shelves = current_user.kobo_only_shelves_sync | ||||||
|     # calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id).filter(ub.Shelf.kobo_sync).count() |     # calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id).filter(ub.Shelf.kobo_sync).count() | ||||||
|     if request.method == "POST": |     if request.method == "POST": | ||||||
| @@ -249,20 +249,20 @@ def create_edit_shelf(shelf, title, page, shelf_id=False): | |||||||
|         shelf.is_public = 1 if to_save.get("is_public") else 0 |         shelf.is_public = 1 if to_save.get("is_public") else 0 | ||||||
|         if config.config_kobo_sync: |         if config.config_kobo_sync: | ||||||
|             shelf.kobo_sync = True if to_save.get("kobo_sync") else False |             shelf.kobo_sync = True if to_save.get("kobo_sync") else False | ||||||
|  |         shelf_title = to_save.get("title", "") | ||||||
|         if check_shelf_is_unique(shelf, to_save, shelf_id): |         if check_shelf_is_unique(shelf, shelf_title, shelf_id): | ||||||
|             shelf.name = to_save.get("title", "") |             shelf.name = shelf_title | ||||||
|             if not shelf_id: |             if not shelf_id: | ||||||
|                 shelf.user_id = int(current_user.id) |                 shelf.user_id = int(current_user.id) | ||||||
|                 ub.session.add(shelf) |                 ub.session.add(shelf) | ||||||
|                 shelf_action = "created" |                 shelf_action = "created" | ||||||
|                 flash_text = _(u"Shelf %(title)s created", title=to_save.get("title", "")) |                 flash_text = _(u"Shelf %(title)s created", title=shelf_title) | ||||||
|             else: |             else: | ||||||
|                 shelf_action = "changed" |                 shelf_action = "changed" | ||||||
|                 flash_text = _(u"Shelf %(title)s changed", title=to_save.get("title", "")) |                 flash_text = _(u"Shelf %(title)s changed", title=shelf_title) | ||||||
|             try: |             try: | ||||||
|                 ub.session.commit() |                 ub.session.commit() | ||||||
|                 log.info(u"Shelf {} {}".format(to_save.get("title", ""), shelf_action)) |                 log.info(u"Shelf {} {}".format(shelf_title, shelf_action)) | ||||||
|                 flash(flash_text, category="success") |                 flash(flash_text, category="success") | ||||||
|                 return redirect(url_for('shelf.show_shelf', shelf_id=shelf.id)) |                 return redirect(url_for('shelf.show_shelf', shelf_id=shelf.id)) | ||||||
|             except (OperationalError, InvalidRequestError) as ex: |             except (OperationalError, InvalidRequestError) as ex: | ||||||
| @@ -276,37 +276,37 @@ def create_edit_shelf(shelf, title, page, shelf_id=False): | |||||||
|                 flash(_(u"There was an error"), category="error") |                 flash(_(u"There was an error"), category="error") | ||||||
|     return render_title_template('shelf_edit.html', |     return render_title_template('shelf_edit.html', | ||||||
|                                  shelf=shelf, |                                  shelf=shelf, | ||||||
|                                  title=title, |                                  title=page_title, | ||||||
|                                  page=page, |                                  page=page, | ||||||
|                                  kobo_sync_enabled=config.config_kobo_sync, |                                  kobo_sync_enabled=config.config_kobo_sync, | ||||||
|                                  sync_only_selected_shelves=sync_only_selected_shelves) |                                  sync_only_selected_shelves=sync_only_selected_shelves) | ||||||
|  |  | ||||||
|  |  | ||||||
| def check_shelf_is_unique(shelf, to_save, shelf_id=False): | def check_shelf_is_unique(shelf, title, shelf_id=False): | ||||||
|     if shelf_id: |     if shelf_id: | ||||||
|         ident = ub.Shelf.id != shelf_id |         ident = ub.Shelf.id != shelf_id | ||||||
|     else: |     else: | ||||||
|         ident = true() |         ident = true() | ||||||
|     if shelf.is_public == 1: |     if shelf.is_public == 1: | ||||||
|         is_shelf_name_unique = ub.session.query(ub.Shelf) \ |         is_shelf_name_unique = ub.session.query(ub.Shelf) \ | ||||||
|                                    .filter((ub.Shelf.name == to_save["title"]) & (ub.Shelf.is_public == 1)) \ |                                    .filter((ub.Shelf.name == title) & (ub.Shelf.is_public == 1)) \ | ||||||
|                                    .filter(ident) \ |                                    .filter(ident) \ | ||||||
|                                    .first() is None |                                    .first() is None | ||||||
|  |  | ||||||
|         if not is_shelf_name_unique: |         if not is_shelf_name_unique: | ||||||
|             log.error("A public shelf with the name '{}' already exists.".format(to_save["title"])) |             log.error("A public shelf with the name '{}' already exists.".format(title)) | ||||||
|             flash(_(u"A public shelf with the name '%(title)s' already exists.", title=to_save["title"]), |             flash(_(u"A public shelf with the name '%(title)s' already exists.", title=title), | ||||||
|                   category="error") |                   category="error") | ||||||
|     else: |     else: | ||||||
|         is_shelf_name_unique = ub.session.query(ub.Shelf) \ |         is_shelf_name_unique = ub.session.query(ub.Shelf) \ | ||||||
|                                    .filter((ub.Shelf.name == to_save.get("title", "")) & (ub.Shelf.is_public == 0) & |                                    .filter((ub.Shelf.name == title) & (ub.Shelf.is_public == 0) & | ||||||
|                                            (ub.Shelf.user_id == int(current_user.id))) \ |                                            (ub.Shelf.user_id == int(current_user.id))) \ | ||||||
|                                    .filter(ident) \ |                                    .filter(ident) \ | ||||||
|                                    .first() is None |                                    .first() is None | ||||||
|  |  | ||||||
|         if not is_shelf_name_unique: |         if not is_shelf_name_unique: | ||||||
|             log.error("A private shelf with the name '{}' already exists.".format(to_save["title"])) |             log.error("A private shelf with the name '{}' already exists.".format(title)) | ||||||
|             flash(_(u"A private shelf with the name '%(title)s' already exists.", title=to_save["title"]), |             flash(_(u"A private shelf with the name '%(title)s' already exists.", title=title), | ||||||
|                   category="error") |                   category="error") | ||||||
|     return is_shelf_name_unique |     return is_shelf_name_unique | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Ozzie Isaacs
					Ozzie Isaacs