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 simple_add_connection(otherUserID): cur_user = current_user() otherUser = UserAccount.getuser(int(otherUserID)) if cur_user.add_connection(otherUser): return jsonify({"Message":"Connection successfully created"}) else: return jsonify({"Message":"Connection already existed"})
def searchbooks(): booklist = {} searchterm = request.args.get('value') attr = request.args.get('refineSearch') 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 booklist = Book.search_books_by_attribute(searchterm,attr) for book in booklist: booklist[book] = booklist[book].to_dict() #Assume not in booklist or networkbooklist booklist[book]["inLibrary"] = "False" booklist[book]["inNetwork"] = "False" else: user = current_user() #Create a dictionary of the user's books mybooklist = {} for copy in user.get_library(): mybooklist[copy.OLKey] = copy #Create a dictionary of the books in my network networkbooklist = {} string = "" for connection in user.get_connections(): u = UserAccount.getuser(connection.id()) for copy in u.get_library(): networkbooklist[copy.OLKey] = copy booklist = Book.search_books_by_attribute(searchterm,attr) for book in booklist: booklist[book] = booklist[book].to_dict() booklist[book]["escapedtitle"] = re.escape(booklist[book]["title"]) if booklist[book]['OLKey'] in mybooklist: booklist[book]["inLibrary"] = "True" else: booklist[book]["inLibrary"] = "False" if booklist[book]['OLKey'] in networkbooklist: booklist[book]["inNetwork"] = "True" else: booklist[book]["inNetwork"] = "False" return render_response('searchbooks.html', books=booklist, search=searchterm, attribute=attr)
def discover(): user = current_user() booklist = [] string = "" for connection in user.get_connections(): u = UserAccount.getuser(connection.id()) for copy in u.get_library(): book = Book.query(Book.key == copy.book).get() booklist.append(book) #Sort booklist alphabetically, with title as the primary sort key and author as secondary booklist.sort(key=lambda book: book.author.lower()) booklist.sort(key=lambda book: book.title.lower()) return render_response('discover.html',books=booklist)
def manage_connections(otherUserID = None): cur_user = current_user() if request.method == 'GET': connections = cur_user.get_all_connections() users = [] result = "you have " + str(len(connections)) + " connections" for connection in connections: result += "<br>" + connection.name user = dict() user["name"] = connection.name user["email"] = connection.email #user["username"] = connection.username user["id"] = connection.get_id() users.append(user) return jsonify({"connectedUsers":users}) elif request.method == 'POST': cur_user = current_user() otherUser = UserAccount.getuser(int(otherUserID)) result = cur_user.send_invite(otherUser) if(result == 0): return jsonify({"Message":"Invitation successfully sent"}) elif(result == 1): return jsonify({"Message":"Connection already existed"}) elif(result == 2): return jsonify({"Message":"Cannot create a connection with yourself"}) elif request.method == 'DELETE': cur_user = current_user() otherUser = UserAccount.getuser(int(otherUserID)) if cur_user.remove_connection(otherUser): return jsonify({"Message":"Connection successfully deleted"}) else: return jsonify({"Message":"Connection didn't existed"}) else: #this should never be reached return jsonify({"Message":"Error: http request was invalid"})
def setup_item_borrow_actions(lenderID, itemCopyID): 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() return jsonify({"Message":"OK"})
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 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 see_who_in_network_has_book(OLKey): user = current_user() networkuserlist = {} string = "" for connection in user.get_connections(): u = UserAccount.getuser(connection.id()) for copy in u.get_library(): if copy.OLKey == OLKey: user = {} user["username"] = u.name user["bookCopyID"] = copy.key.id() if copy.borrower == None: user["available"] = "True" else: user["available"] = "False" networkuserlist[u.get_id()] = user return jsonify(networkuserlist)
def load_user(id): return UserAccount.getuser(int(id))
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)