-
Notifications
You must be signed in to change notification settings - Fork 3
/
wishlists.py
78 lines (68 loc) · 2.44 KB
/
wishlists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from db_connector import get_db, BookSwapDatabase
import logging
log = logging.getLogger('app.sub')
class Wishlists:
"""
Wishlists contains the structure and methods for populating a user's
`my-wishlists` route.
"""
def __init__(self, user_num, bsdb):
"""
Class initializer. Opens database object for use, and set's
user's id number.
Accepts:
user_num (int): Users.id
bsdb (BookSwapDatabase instance): database access object
"""
self.user_num = user_num
self.bsdb = bsdb
def get_all_wishlist_books_for_user(self):
"""
Get_all_wishlist_books_for_user returns an array of dictionaries, each representing a Book entry.
Accepts:
None
Returns:
List of dicts. Each dict is a book in a user's wishlist.
"""
try:
wishlists = self._get_all_wishlists_for_user()
books = self._get_books_for_wishlists(wishlists)
log.info(f"Created books in wishlist for user {self.user_num}")
except Exception:
log.error(f"Error retrieving book details for wishlists for user {self.user_num}")
raise Exception
return books
def _get_all_wishlists_for_user(self):
"""
_Get_all_wishlists_for_user returns a list of wishlist ids belonging
to that user.
Accepts:
None
Returns:
List of ints
"""
try:
wishlist_rows = self.bsdb.get_wishlists_by_userid(self.user_num)
except Exception:
log.error(f"Error getting wishlists for user {self.user_num}")
raise Exception
wishlists = [row['id'] for row in wishlist_rows]
return wishlists
def _get_books_for_wishlists(self, wishlists):
"""
_Get_books_for_wishlists gives a list of dictionaries, each
representing a book in the user's wishlist.
Accepts:
wishlists (list of ints): List of Wishlists.id
Returns:
List of dictionaries
"""
books = []
for wishlist in wishlists:
try:
books += self.bsdb.get_book_details_for_wishlist(wishlist)
except Exception:
log.error(f"Error getting books for wishlist {wishlist}")
raise Exception
books = [dict(book) for book in books]
return books