def search_for_book(value, attribute=None): # This is broken because search_books_by_attribute now returns a list of dicts books = Item.search_by_attribute("book",value,attribute) if len(books) == 0: return jsonify({"Message":"No books found"}) else: return jsonify(JsonIterable.dict_of_dict(books))
def confirm(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() itemcopy.update_due_date(self.due_date) itemcopy.put() self.cleanup() return "%s has been extended to %s" %(item.title,str(self.due_date))
def request_to_borrow(lenderID, itemCopyID): emailText = [] emailText.append("You have received the following message from " + current_user().name + ", a Sharing Commons user.\n----\n\n") emailText.append(request.data) emailText.append("\n\n----\nReply to this message to send an email to " + current_user().name + " and set up the exchange. Once you've lent the item, visit beta.sharingcommons.com to confirm lending the item. " + current_user().name + " will receive an email when the item is due") emailBody = ''.join(emailText) # Request item try: borrower = current_user() lender = UserAccount.getuser(int(lenderID)) itemCopy = ItemCopy.get_by_id(int(itemCopyID)) rtb1 = RequestToBorrow() rtb1.useraccount = lender.key rtb1.connection = borrower.key rtb1.item = itemCopy.key rtb1.put() wtb1 = WaitingToBorrow() wtb1.useraccount = borrower.key wtb1.connection = lender.key wtb1.item = itemCopy.key wtb1.put() except: return jsonify({"result":"error"}) # Send email mail.send_mail(sender="Sharing Commons <*****@*****.**>", to=lender.name + " <" + lender.email + ">", reply_to=borrower.name + " <" + borrower.email + ">", subject='Sharing Commons: Request to Borrow "' + Item.query(Item.key == itemCopy.item).get().title + '"', body=emailBody) return jsonify({"result":"success"})
def library_requests(item_subtype, item_key): # Infer item_type if item_subtype in ('book', 'ebook', 'audiobook'): item_type = 'book' elif item_subtype in ('dvd', 'bluray'): item_type = 'movie' else: item_type = '' cur_user = current_user() if not cur_user: logging.info("there is not a user logged in") #return "<a href='%s' >Login</a>" %users.create_login_url(dest_url=url_for('library_requests',ISBN=ISBN)) if request.method == 'GET': #check the database to see if the item is in the user's library item = Item.get_by_key(item_type,item_key) if item: if cur_user.get_book(item_subtype,item): return item.title else: return "You do not have this item in your library" else: return "This item was not found" elif request.method == 'POST': #add the item to the user's library #If not found, add it to the cache, then to the user's library item = Item.get_by_key(item_type,item_key) if not item: return "Item " + item_key + " was not found" else: if cur_user.get_item(item_subtype,item): return "This item is already in your library" cur_user.add_item(item_subtype, item) return "Item " + item_key + " was added to your library" elif request.method == 'DELETE': #remove the item from the user's library item = Item.get_by_key(item_type,item_key) if not item: return "Item not found" else: if cur_user.get_item(item_subtype,item): cur_user.remove_item(item_subtype,item) return "Successfully deleted " + item_key + " from your library" else: #this should never be reached return "Error: http request was invalid"
def confirm(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() itemcopy.return_item() itemcopy.put() self.cleanup() return "%s has been returned to your library" %(item.title)
def reject(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() self.cleanup() otherAction = RequestToBorrow.query(RequestToBorrow.item == itemcopy.key and RequestToBorrow.useraccount == other.key).get() otherAction.cleanup() return "Request cancelled"
def reject(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() self.cleanup() otherAction = WaitingToBorrow.query(WaitingToBorrow.item == itemcopy.key and WaitingToBorrow.useraccount == other.key).get() otherAction.cleanup() return "You have denied %s permission to borrow %s" %(other.name,item.title)
def discover(): # Start by creating a list of items (as dicts) within the user's library # This is necessary prep to be able to show that the item is in the user's library librarylist = {} useraccount = current_user() for copy in useraccount.get_library(): item = Item.query(Item.key == copy.item).get().to_dict() item["item_subtype"] = copy.item_subtype item["escapedtitle"] = re.escape(item["title"]) librarylist[(item["item_key"],item["item_subtype"])] = item # Create a list of all items (as dicts) in the user's network user = current_user() itemlist = [] for connection in user.get_connections(): u = UserAccount.getuser(connection.id()) for copy in u.get_library(): item = Item.query(Item.key == copy.item).get().to_dict() item["item_subtype"] = copy.item_subtype item["escapedtitle"] = re.escape(item["title"]) if copy.borrower is None: item["available"] = True else: item["available"] = False # Check to see if book is in the user's library item["inLibrary"] = [] for item_subtype in ['book', 'ebook', 'audiobook']: if (item["item_key"],item_subtype) in librarylist: item["inLibrary"].append(item_subtype) itemlist.append(item) # Sort itemlist alphabetically, with title as the primary sort key, # author as secondary, and item_subtype as tertiary itemlist.sort(key=lambda item: item["item_subtype"]) itemlist.sort(key=lambda item: item["title"].lower()) #Remove duplicate books (dictionaries) from itemlist (list) dedupeditemlist = [] for item in itemlist: if item not in dedupeditemlist: dedupeditemlist.append(item) return render_response('discover.html',itemlist=dedupeditemlist)
def get_my_item_list(): cur_user = current_user() if not cur_user: logging.info("there is not a user logged in") return "<a href='%s' >Login</a>" %users.create_login_url(dest_url=url_for('manage_library')) items = {} counter = 0 for copy in cur_user.get_library(): item = Item.query(Item.key == copy.item).get() items[counter] = item.to_dict() counter += 1 return jsonify(JsonIterable.dict_of_dict(items))
def movie_info(RTKey): itemMovie = Item.get_by_key("movie",RTKey) movie = itemMovie.to_dict() # Check if user owns book and pass itemCopy object if current_user().is_authenticated(): itemCopy = ItemCopy.query(ItemCopy.item==itemMovie.key,ItemCopy.owner==current_user().key).fetch() if itemCopy: movieCopy = itemCopy[0] else: movieCopy = None else: movieCopy = None return render_response('itemdetail.html', item=movie, itemCopy=movieCopy)
def item_due_reminders(): count = 0 """find all the items due tomorrow and send reminder emails""" items = ItemCopy.query(ItemCopy.due_date==date.today() + timedelta(days=1)).fetch() for item in items: count += 1 owner = UserAccount.query(UserAccount.key==item.owner).get() mail.send_mail(sender=owner.email, to=UserAccount.query(UserAccount.key==item.borrower).get().email, subject="Item Due Soon", body="""The following item is due to be returned tomorrow: '%s'. Please return it to %s"""%(Item.query(Item.key==item.item).get().title,owner.name)) return "%s reminders were sent out" %count
def confirm(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() due_date = None cur_user = UserAccount.query(UserAccount.key==self.useraccount).get() cur_user.lend_item(int(itemcopy.key.id()), int(other.key.id()), due_date) self.cleanup() otherAction = WaitingToBorrow.query(WaitingToBorrow.item == itemcopy.key and WaitingToBorrow.useraccount == other.key).get() otherAction.cleanup() return "You have agreed to lend %s to %s" %(item.title,other.name)
def get_borrowed_items(): cur_user = current_user() borrowedItems = [] for itemcopy in cur_user.get_borrowed_books(): if not itemcopy.manual_borrower_name: #Don't include items the user is manually lending (they would come up because the borrower is set to the user) item = Item.get_by_id(itemcopy.item.id()) owner = UserAccount.get_by_id(itemcopy.owner.id()) itemInfo = dict() itemInfo["title"] = item.title itemInfo["author"] = item.author itemInfo["copyID"] = itemcopy.key.id() itemInfo["ownerId"] = itemcopy.owner.id() itemInfo["owner"] = owner.name itemInfo["due_date"] = str(itemcopy.due_date) borrowedItems.append(itemInfo) return jsonify({"borrowedItems":borrowedItems})
def star_rating(item_subtype, item_key, star_rating): # Get Item # Infer item_type if item_subtype in ('book', 'ebook', 'audiobook'): item_type = 'book' elif item_subtype in ('dvd', 'bluray'): item_type = 'movie' else: item_type = '' item = Item.get_by_key(item_type,item_key) # Get ItemCopy itemCopy = ItemCopy.query(ItemCopy.item==item.key,ItemCopy.owner==current_user().key,ItemCopy.item_subtype==item_subtype).get() if itemCopy.update_star_rating(star_rating): return jsonify({"result":"success"}) else: return jsonify({"result":"error"})
def search_network(item_key): user = current_user() networkuserlist = {} for connection in user.get_connections(): u = UserAccount.getuser(connection.id()) for copy in u.get_library(): if Item.query(Item.key == copy.item).get().item_key == item_key: user = {} user["username"] = u.name user["itemCopyID"] = copy.key.id() if copy.borrower == None: user["available"] = "True" else: user["available"] = "False" networkuserlist[u.get_id()] = user return jsonify(networkuserlist)
def get_lent_items(): cur_user = current_user() lentItems = [] for itemcopy in cur_user.get_lent_books(): item = Item.get_by_id(itemcopy.item.id()) borrower = UserAccount.get_by_id(itemcopy.borrower.id()) itemInfo = dict() itemInfo["title"] = item.title itemInfo["author"] = item.author itemInfo["copyID"] = itemcopy.key.id() itemInfo["borrowerId"] = itemcopy.borrower.id() itemInfo["borrower"] = borrower.name itemInfo["due_date"] = str(itemcopy.due_date) if itemcopy.manual_borrower_name: itemInfo["manual_borrower_name"] = itemcopy.manual_borrower_name itemInfo["manual_borrower_email"] = itemcopy.manual_borrower_email lentItems.append(itemInfo) return jsonify({"lentItems":lentItems})
def book_info(OLKey): # Pass book object to template itemBook = Item.get_by_key("book",OLKey) book = itemBook.to_dict() # Determine if the user owns this book # Find all connections who own this book and get the status for each # Find out any pending actions regarding this book # Find any current loans or borrow of this book # Check if user owns book and pass itemCopy object if current_user().is_authenticated(): itemCopy = ItemCopy.query(ItemCopy.item==itemBook.key,ItemCopy.owner==current_user().key).fetch() if itemCopy: bookCopy = itemCopy[0] else: bookCopy = None else: bookCopy = None return render_response('itemdetail.html',item=book,itemCopy=bookCopy)
def library(): # Create a list of items (as dicts) within the user's library itemlist = [] useraccount = current_user() for copy in useraccount.get_library(): item = Item.query(Item.key == copy.item).get().to_dict() item["item_subtype"] = copy.item_subtype if copy.star_rating: item["star_rating"] = copy.star_rating else: item["star_rating"] = 0 item["escapedtitle"] = re.escape(item["title"]) if copy.borrower is None: item["available"] = True else: item["available"] = False itemlist.append(item) # Sort itemlist alphabetically, with title as the primary sort key, # author as secondary, and item_subtype as tertiary itemlist.sort(key=lambda item: item["item_subtype"]) itemlist.sort(key=lambda item: item["title"].lower()) return render_response('managelibrary.html', itemlist=itemlist)
def profile(userID): try: int(userID) profile_user = UserAccount.get_by_id(int(userID)) # Check if profile user has a custom url and forward if so if profile_user.custom_url: try: long(profile_user.custom_url) # Custom URLs MUST include at least one letter, so this will always fail with a custom URL except: return redirect('/user/' + profile_user.custom_url) except: # Query custom URLs custom_url_user = UserAccount.query(UserAccount.custom_url==userID).get() if custom_url_user: profile_user = custom_url_user else: return redirect(url_for("invalid_profile")) user = current_user() if user.is_authenticated(): inNetwork = user.is_connected(profile_user) else: inNetwork = False if inNetwork or profile_user.profile_privacy == 1: if user == profile_user: inNetwork = True booklist = [] for copy in profile_user.get_library(): item = Item.query(Item.key == copy.item).get().to_dict() if item["item_type"] == "book": item["item_subtype"] = copy.item_subtype item["star_rating"] = copy.star_rating item["escapedtitle"] = re.escape(item["title"]) if copy.borrower is None: item["available"] = True else: item["available"] = False item["copyID"] = copy.key.id() booklist.append(item) # Sort library alphabetically, with title as the primary sort key, # author as secondary, and item_subtype as tertiary booklist.sort(key=lambda item: item["item_subtype"]) booklist.sort(key=lambda item: item["title"].lower()) movielist = [] for copy in profile_user.get_library(): item = Item.query(Item.key == copy.item).get().to_dict() if item["item_type"] == "movie": item["item_subtype"] = copy.item_subtype item["star_rating"] = copy.star_rating item["escapedtitle"] = re.escape(item["title"]) if copy.borrower is None: item["available"] = True else: item["available"] = False item["copyID"] = copy.key.id() movielist.append(item) # Sort library alphabetically, with title as the primary sort key, # author as secondary, and item_subtype as tertiary movielist.sort(key=lambda item: item["item_subtype"]) movielist.sort(key=lambda item: item["title"].lower()) import hashlib import urllib gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(profile_user.email).hexdigest() + "?s=150&d=" + urllib.quote(request.host_url,'') + "static%2Fimg%2Fnoimage.png" return render_response('profile.html',inNetwork=inNetwork,profile_user=profile_user,booklist=booklist,movielist=movielist,gravatar_url=gravatar_url) return redirect(url_for("invalid_profile"))
def text(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() return "%s has requested to borrow '%s'" %(other.name,item.title)
def confirm(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() print "You have accepted a connection request from %s" %(other.name)
def text(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() return "%s reported checking in '%s'" %(other.name,item.title)
def text(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() return "You have requested to borrow '%s' from %s" %(item.title,other.name)
def reject(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() self.cleanup() return "Recorded that %s didn't return %s" %(other.name,item.title)
def search(): itemlist = {} item_type = request.args.get('item_type') subtype_book = request.args.get('subtype_book') subtype_ebook = request.args.get('subtype_ebook') subtype_audiobook = request.args.get('subtype_audiobook') subtype_dvd = request.args.get('subtype_dvd') subtype_bluray = request.args.get('subtype_bluray') user_email = request.args.get('user_email') searchterm = request.args.get('query') attr = request.args.get('refineSearch') src = request.args.get('src') # If searching for a user, redirect to profile if user_email: profile_user = UserAccount.query(UserAccount.email==searchterm).get() if not profile_user: return redirect(url_for("invalid_profile")) else: if profile_user.custom_url: return redirect('/user/' + profile_user.custom_url) else: return redirect('/user/' + str(profile_user.get_id())) subtype_specified = "true" # Used in javascript to determine whether out-of-network cookie should be respected or not if item_type is None and subtype_book is None and subtype_ebook is None and subtype_audiobook is None and subtype_dvd is None and subtype_bluray is None: #Nothing was specified, so act as if they searched books item_type = "book" if subtype_book or subtype_ebook or subtype_audiobook or subtype_dvd or subtype_bluray: # If subtype is included, item_type may not be, so it must be added if subtype_book or subtype_ebook or subtype_audiobook: item_type = "book" if subtype_dvd or subtype_bluray: item_type = "movie" else: # None are included, so only item_type is being pass; set all subtypes to true if item_type == "book": subtype_specified = "false" subtype_book = "true" subtype_ebook = "true" subtype_audiobook = "true" elif item_type == "movie": subtype_specified = "false" subtype_dvd = "true" subtype_bluray = "true" if attr == "all": attr = None if searchterm is None: searchterm = "" else: searchterm = searchterm.lstrip() if searchterm is None or searchterm == "": pass else: cur_user = current_user() logging.info(cur_user) if not cur_user.is_authenticated(): #Assume no books in library or network, return results only itemlist = Item.search_by_attribute(item_type,searchterm,attr) for item in itemlist: item["inLibrary"] = [] item["inNetwork"] = [] else: user = current_user() #Create a dictionary of the user's items librarylist = {} for copy in user.get_library(): copyItemKey = Item.query(Item.key == copy.item).get().item_key copyItemSubtype = copy.item_subtype librarylist[(copyItemKey,copyItemSubtype)] = copy.to_dict() #Create a dictionary of the items in the user's network #The dict, networkitemlist, includes each ItemCopy object, with it's associated item_key. networkitemlist = {} for connection in user.get_connections(): u = UserAccount.getuser(connection.id()) for copy in u.get_library(): copyItemKey = Item.query(Item.key == copy.item).get().item_key copyItemSubtype = copy.item_subtype networkitemlist[(copyItemKey,copyItemSubtype)] = copy.to_dict() itemlist = Item.search_by_attribute(item_type,searchterm,attr) for item in itemlist: item["escapedtitle"] = re.escape(item["title"]) # Check for copies in library and in network, # return "inLibrary" list with all item types in Library # return "inNetwork" list with all item types in Library item["inLibrary"] = [] item["inNetwork"] = [] for item_subtype in ['book', 'ebook', 'audiobook', 'dvd', 'bluray']: if (item["item_key"],item_subtype) in librarylist: item["inLibrary"].append(item_subtype) if (item["item_key"],item_subtype) in networkitemlist: item["inNetwork"].append(item_subtype) return render_response('search.html', itemlist=itemlist, search=searchterm, attribute=attr, include_type=item_type, subtype_book=subtype_book, subtype_ebook=subtype_ebook, subtype_audiobook=subtype_audiobook, subtype_specified=subtype_specified, src=src)
def text(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() return "%s wants to extend the due date of '%s' to %s?" %(other.name,item.title,str(self.due_date))