move parent[] to parent as the array was never needed, removed some dead code, clarified another example of new BUG-46

This commit is contained in:
2023-07-08 15:10:38 +10:00
parent ff87aca2f5
commit 4e8bda4134
6 changed files with 27 additions and 44 deletions

1
BUGs
View File

@@ -7,3 +7,4 @@ BUG-45: create a new book, fail (say for now year), and it loses the series
BUG-46: search (and really all books.html usage), don't deal with sub books and/or series ordering. Probably should at least do subs ordering BUG-46: search (and really all books.html usage), don't deal with sub books and/or series ordering. Probably should at least do subs ordering
(so the code I wrote for author/<id>/books) and use that for search for example (so the code I wrote for author/<id>/books) and use that for search for example
- test with search for 'dune' - test with search for 'dune'
- also test with authors book list for frank herbert

2
TODO
View File

@@ -1,6 +1,4 @@
TODO: TODO:
* book.parent is an array, not a dict - should change it over
* search on more than title, e.g. author, series, etc. * search on more than title, e.g. author, series, etc.
* if we add a new parent book with sub-books into a series, with parent * book as 'null' sub_book_num, all the sub_books should be added somehow into series - but cant input numbering on parent book, so * if we add a new parent book with sub-books into a series, with parent * book as 'null' sub_book_num, all the sub_books should be added somehow into series - but cant input numbering on parent book, so

View File

@@ -122,13 +122,13 @@ def authors_books(author_id):
ordered_books.append( tmp ) ordered_books.append( tmp )
processed.append( bb.sub_book_id ) processed.append( bb.sub_book_id )
elif b.IsChild(): elif b.IsChild():
ordered_books.append(b.parent[0]) ordered_books.append(b.parent)
processed.append( b.parent[0].id ) processed.append( b.parent.id )
cnt=1 cnt=1
for bb in b.parent[0].child_ref: for bb in b.parent.child_ref:
tmp=Book.query.get(bb.sub_book_id) tmp=Book.query.get(bb.sub_book_id)
# need to set parent_id and sub_book_num for indenting to work in html # need to set parent_id and sub_book_num for indenting to work in html
tmp.parent_id=b.parent[0].id tmp.parent_id=b.parent.id
tmp.sub_book_num=cnt tmp.sub_book_num=cnt
ordered_books.append( tmp ) ordered_books.append( tmp )
processed.append( bb.sub_book_id ) processed.append( bb.sub_book_id )

36
main.py
View File

@@ -152,7 +152,7 @@ class Book(db.Model):
modified = db.Column(db.DateTime) modified = db.Column(db.DateTime)
# take actual parent book as there is no real associated sub_book_num data and can just use it # take actual parent book as there is no real associated sub_book_num data and can just use it
parent = db.relationship('Book', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id" ) parent = db.relationship('Book', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.book_id", uselist=False )
# but use child_ref as sub_book_num is per book, and I can't connect an empty array of sub_book_nums to a child book array in "child" # but use child_ref as sub_book_num is per book, and I can't connect an empty array of sub_book_nums to a child book array in "child"
child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", order_by="Book_Sub_Book_Link.sub_book_num", overlaps="parent" ) child_ref = db.relationship('Book_Sub_Book_Link', secondary=Book_Sub_Book_Link.__table__, primaryjoin="Book.id==Book_Sub_Book_Link.book_id", secondaryjoin="Book.id==Book_Sub_Book_Link.sub_book_id", order_by="Book_Sub_Book_Link.sub_book_num", overlaps="parent" )
@@ -166,7 +166,7 @@ class Book(db.Model):
return False return False
def IsChild(self): def IsChild(self):
if len(self.parent): if self.parent:
return True return True
else: else:
return False return False
@@ -444,7 +444,7 @@ def CalcMoveForBookInSeries( id, bid, dir ):
# if book2 is a sub book, then we need its parent as book2 instead, as we have to move the whole book as one # if book2 is a sub book, then we need its parent as book2 instead, as we have to move the whole book as one
if book2.IsChild(): if book2.IsChild():
book2 = Book.query.get( book2.parent[0].id ) book2 = Book.query.get( book2.parent.id )
# By here: book1 is parent or normal book we are swapping with book2 which is parent or normal book # By here: book1 is parent or normal book we are swapping with book2 which is parent or normal book
b1_numsb = book1.NumSubBooks() b1_numsb = book1.NumSubBooks()
@@ -598,7 +598,7 @@ def RemoveDuplicateSeriesInForm( book, request ):
# add the contains (null for book_num) bsl for the parent book # add the contains (null for book_num) bsl for the parent book
if book.IsChild(): if book.IsChild():
parent_bsl=Book_Series_Link( book_id=book.parent[0].id, series_id=request.form[f'bsl-series_id-{cnt}'] ) parent_bsl=Book_Series_Link( book_id=book.parent.id, series_id=request.form[f'bsl-series_id-{cnt}'] )
db.session.add(parent_bsl) db.session.add(parent_bsl)
db.session.add(newbsl) db.session.add(newbsl)
@@ -735,7 +735,7 @@ def DeleteBook(id):
st.SetMessage("Deleted {}".format(book.title) ) st.SetMessage("Deleted {}".format(book.title) )
pid = 0 pid = 0
if book.IsChild(): if book.IsChild():
pid = book.parent[0].id pid = book.parent.id
try: try:
ClearAuthorsForBook(book.id) ClearAuthorsForBook(book.id)
db.session.delete(book) db.session.delete(book)
@@ -828,30 +828,14 @@ def book(id):
CheckSeriesChange={'type':'parent', 'pid': book.id, 'bid': book.id, 'removing_series': removing_series } CheckSeriesChange={'type':'parent', 'pid': book.id, 'bid': book.id, 'removing_series': removing_series }
# saving a child / sub_book, consider series # saving a child / sub_book, consider series
if book.IsChild() and len(removing_series) > 0: if book.IsChild() and len(removing_series) > 0:
CheckSeriesChange={'type':'child', 'pid': book.parent[0].id, 'bid': book.id, 'removing_series': removing_series } CheckSeriesChange={'type':'child', 'pid': book.parent.id, 'bid': book.id, 'removing_series': removing_series }
else: else:
# either we are a normal book (no parent/child), OR not removing a series, might be adding though, so easiest is to # either we are a normal book (no parent/child), OR not removing a series, might be adding though, so easiest is to
# delete all bsls and then add them back based on the request.form # delete all bsls and then add them back based on the request.form
Book_Series_Link.query.filter(Book_Series_Link.book_id == book.id ).delete() Book_Series_Link.query.filter(Book_Series_Link.book_id == book.id ).delete()
if book.IsChild(): if book.IsChild():
Book_Series_Link.query.filter(Book_Series_Link.book_id == book.parent[0].id ).delete() Book_Series_Link.query.filter(Book_Series_Link.book_id == book.parent.id ).delete()
# cnt=1
# for field in request.form:
# if 'bsl-book_id-' in field and field != 'bsl-book_id-NUM':
# cnt=int(re.findall( '\d+', field )[0])
# if book.IsParent():
# newbsl=Book_Series_Link( book_id=request.form[f'bsl-book_id-{cnt}'],
# series_id=request.form[f'bsl-series_id-{cnt}'] )
# else:
# newbsl=Book_Series_Link( book_id=request.form[f'bsl-book_id-{cnt}'],
# series_id=request.form[f'bsl-series_id-{cnt}'],
# book_num=request.form[f'bsl-book_num-{cnt}'] )
# # add the contains (null for book_num) bsl for the parent book
# if book.IsChild():
# parent_bsl=Book_Series_Link( book_id=book.parent[0].id, series_id=request.form[f'bsl-series_id-{cnt}'] )
# db.session.add(parent_bsl)
# db.session.add(newbsl)
message=RemoveDuplicateSeriesInForm( book, request ) message=RemoveDuplicateSeriesInForm( book, request )
db.session.commit() db.session.commit()
@@ -985,10 +969,10 @@ def OrderBooks(books):
if tmp_b.IsChild(): if tmp_b.IsChild():
# this child book wont be in books, but its parent will -> use it instead # this child book wont be in books, but its parent will -> use it instead
# mark parent from books so we dont process it twice # mark parent from books so we dont process it twice
if tmp_b.parent[0].id not in processed: if tmp_b.parent.id not in processed:
processed.append(tmp_b.id) processed.append(tmp_b.id)
processed.append(tmp_b.parent[0].id) processed.append(tmp_b.parent.id)
ordered_books.append(tmp_b.parent[0]) ordered_books.append(tmp_b.parent)
else: else:
if tmp_b.id not in processed: if tmp_b.id not in processed:
ordered_books.append(tmp_b) ordered_books.append(tmp_b)

View File

@@ -221,17 +221,17 @@ function AddAuthorToBook(num) {
<form role="form" class="form" action="" method="POST"> <form role="form" class="form" action="" method="POST">
{{ book_form.id }} {{ book_form.id }}
{{ book_form.csrf_token }} {{ book_form.csrf_token }}
{% if b.parent|length %} {% if b.parent %}
<div class="form-row"> <div class="form-row">
<div class="input-group"> <div class="input-group">
<label class="input-group-text col-2 col-form-label bg-secondary text-white"><i>Parent Book:</i></label> <label class="input-group-text col-2 col-form-label bg-secondary text-white"><i>Parent Book:</i></label>
<div class="col"> <div class="col">
<span class="col-12 d-flex h-100 border rounded-end border-primary"> <span class="col-12 d-flex h-100 border rounded-end border-primary">
<i class="col-12 d-flex h-100 justify-content-center"><a href="/book/{{b.parent[0].id}}">{{b.parent[0].title}}</a></i> <i class="col-12 d-flex h-100 justify-content-center"><a href="/book/{{b.parent.id}}">{{b.parent.title}}</a></i>
</span> </span>
</div> </div>
<input type="hidden" name="parent_id" value="{{b.parent[0].id}}"> <input type="hidden" name="parent_id" value="{{b.parent.id}}">
<input type="hidden" name="parent_title" value="{{b.parent[0].title}}"> <input type="hidden" name="parent_title" value="{{b.parent.title}}">
</div> </div>
</div> </div>
{% endif %} {% endif %}
@@ -303,7 +303,7 @@ function AddAuthorToBook(num) {
{% if key == "notes" %} {% if key == "notes" %}
{% set rows=2 %} {% set rows=2 %}
{% endif %} {% endif %}
{% if b.parent|length and {% if b.parent and
(key == 'publisher' or key == 'owned' or key == 'covertype' (key == 'publisher' or key == 'owned' or key == 'covertype'
or key == 'condition' or key == 'blurb' ) %} or key == 'condition' or key == 'blurb' ) %}
{{book_form[key](class="form-control", rows=rows, disabled="disabled" )}} {{book_form[key](class="form-control", rows=rows, disabled="disabled" )}}
@@ -426,7 +426,7 @@ function AddAuthorToBook(num) {
{% else %} {% else %}
<div class="form-row"> <div class="form-row">
{% endif %} {% endif %}
{% if b.parent|length == 0 %} {% if b.parent %}
<form role="form" class="form" action="{{url_for('new_book')}}" method="POST"> <form role="form" class="form" action="{{url_for('new_book')}}" method="POST">
<input type="hidden" name="add_sub_parent_id" value="{{books.id}}"> <input type="hidden" name="add_sub_parent_id" value="{{books.id}}">
{{ book_form.add_sub( class="btn btn-outline-success offset-2 col-2" )}} {{ book_form.add_sub( class="btn btn-outline-success offset-2 col-2" )}}
@@ -434,7 +434,7 @@ function AddAuthorToBook(num) {
{% else %} {% else %}
<form role="form" class="form" action="{{url_for('remove_sub_book')}}" method="POST"> <form role="form" class="form" action="{{url_for('remove_sub_book')}}" method="POST">
<input type="hidden" name="rem_sub_sub_book_id" value="{{books.id}}"> <input type="hidden" name="rem_sub_sub_book_id" value="{{books.id}}">
<input type="hidden" name="rem_sub_parent_id" value="{{b.parent[0].id}}"> <input type="hidden" name="rem_sub_parent_id" value="{{b.parent.id}}">
{{ book_form.rem_sub( class="btn btn-outline-danger offset-2 col-3" )}} {{ book_form.rem_sub( class="btn btn-outline-danger offset-2 col-3" )}}
</form> </form>
{% endif %} {% endif %}

View File

@@ -6,7 +6,7 @@
{% for book in books %} {% for book in books %}
<tr> <tr>
<td id="{{book.id}}"> <td id="{{book.id}}">
{% if book.parent|length %} {% if book.parent %}
{% set style="style=visibility:hidden" %} {% set style="style=visibility:hidden" %}
{% endif %} {% endif %}
<button name="move_button" value="up-{{book.id}}" id="up-{{book.id}}" class="btn btn-outline-primary btn-small" {{style}} <button name="move_button" value="up-{{book.id}}" id="up-{{book.id}}" class="btn btn-outline-primary btn-small" {{style}}
@@ -51,8 +51,8 @@
{% endif %} {% endif %}
{% if SeriesBookNum( s.id, book.id ) == 1 %} {% if SeriesBookNum( s.id, book.id ) == 1 %}
<script> <script>
{% if book.parent|length %} {% if book.parent %}
$('#up-{{book.parent[0].id}}').prop("disabled", true) $('#up-{{book.parent.id}}').prop("disabled", true)
{% else %} {% else %}
$('#up-{{book.id}}').prop("disabled", true) $('#up-{{book.id}}').prop("disabled", true)
{% endif %} {% endif %}
@@ -60,8 +60,8 @@
{% endif %} {% endif %}
{% if SeriesBookNum( s.id, book.id ) == s.num_books %} {% if SeriesBookNum( s.id, book.id ) == s.num_books %}
<script> <script>
{% if book.parent|length %} {% if book.parent %}
$('#down-{{book.parent[0].id}}').prop("disabled", true) $('#down-{{book.parent.id}}').prop("disabled", true)
{% else %} {% else %}
$('#down-{{book.id}}').prop("disabled", true) $('#down-{{book.id}}').prop("disabled", true)
{% endif %} {% endif %}