Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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)