Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

library_borrow: Fix proposition for is_available searchers and getters in Book and Exemplary #23

Open
wants to merge 2 commits into
base: 5.0/step10_completed
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions modules/library_borrow/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,38 +145,52 @@ class Book(metaclass=PoolMeta):

@classmethod
def getter_is_available(cls, books, name):
pool = Pool()
checkout = pool.get('library.user.checkout').__table__()
exemplary = pool.get('library.book.exemplary').__table__()
book = cls.__table__()
result = {x.id: False for x in books}
book = Pool().get('library.book').__table__()
exemplary = Pool().get('library.book.exemplary').__table__()
checkout = Pool().get('library.user.checkout').__table__()
books_ids = [x.id for x in books]
results = {x.id: False for x in books}

cursor = Transaction().connection.cursor()
sub_query = (checkout.select(checkout.exemplary,
where=(checkout.return_date == Null)))

cursor.execute(*book.join(exemplary,
condition=(exemplary.book == book.id)
).join(checkout, 'LEFT OUTER',
condition=(exemplary.id == checkout.exemplary)
).select(book.id,
where=(checkout.return_date != Null) | (checkout.id == Null)))
for book_id, in cursor.fetchall():
result[book_id] = True
return result
condition=(exemplary.book == book.id)
).join(sub_query ,'LEFT OUTER',
condition=(sub_query.exemplary == exemplary.id)
).select(book.id, exemplary.id, sub_query.exemplary,
where=book.id.in_(books_ids)))

for book_id, _, exemplary_borrowed in cursor.fetchall():
if not exemplary_borrowed:
results[book_id] = True
return results

@classmethod
def search_is_available(cls, name, clause):
_, operator, value = clause
if operator == '!=':
value = not value
pool = Pool()
checkout = pool.get('library.user.checkout').__table__()
exemplary = pool.get('library.book.exemplary').__table__()
book = cls.__table__()
query = book.join(exemplary,
condition=(exemplary.book == book.id)
).join(checkout, 'LEFT OUTER',
condition=(exemplary.id == checkout.exemplary)
).select(book.id,
where=(checkout.return_date != Null) | (checkout.id == Null))
return [('id', 'in' if value else 'not in', query)]
book = Pool().get('library.book').__table__()
exemplary = Pool().get('library.book.exemplary').__table__()
checkout = Pool().get('library.user.checkout').__table__()
results = set()

cursor = Transaction().connection.cursor()
sub_query = (checkout.select(checkout.exemplary,
where=(checkout.return_date == Null)))

cursor.execute(*book.join(exemplary,
condition=(exemplary.book == book.id)
).join(sub_query ,'LEFT OUTER',
condition=(sub_query.exemplary == exemplary.id)
).select(book.id, exemplary.id, sub_query.exemplary))

for book_id, _, exemplary_borrowed in cursor.fetchall():
if not exemplary_borrowed:
results.add(book_id)
return [('id', 'in' if value else 'not in', results)]


class Exemplary(metaclass=PoolMeta):
Expand Down Expand Up @@ -228,7 +242,7 @@ def search_is_available(cls, name, clause):
checkout = pool.get('library.user.checkout').__table__()
exemplary = cls.__table__()
query = exemplary.join(checkout, 'LEFT OUTER',
condition=(exemplary.id == checkout.exemplary)
condition=(checkout.exemplary == exemplary.id)
).select(exemplary.id,
where=(checkout.return_date != Null) | (checkout.id == Null))
return [('id', 'in' if value else 'not in', query)]
where=((checkout.return_date == Null) & (checkout.id != Null)))
return [('id', 'in' if not value else 'not in', query)]
2 changes: 1 addition & 1 deletion modules/library_borrow/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def default_select_checkouts(self, name):
Transaction().context.get('active_ids'))
if len({x.user for x in checkouts}) != 1:
self.raise_user_error('multiple_users')
if any(x.is_available for x in checkouts):
if any(x.exemplary.is_available for x in checkouts):
self.raise_user_error('available')
user = checkouts[0].user.id
return {
Expand Down