def get_all_items_sorted(query): # Get the tags if exists tags = ItemTags.get(request.args.get('tags')) # get list of all items using DAO and specifying the tags listOfItems = mongo_item_dao.findAll(tags) # if nothing in db, don't do any similarity comparisons if not listOfItems: return jsonify([]), 200 queriedItem = Item(name=query, desc="") simMatch = ItemSimilarity(simModel) simMatch.addItems(listOfItems) simMatch.scoreItems(queriedItem) items = simMatch.getSortedItems(getScores=True) index = len(items) for i, (_, score) in enumerate(items): if score <= 0: index = i break items = items[0:index] if len(items) == 0: items = simMatch.getSortedItems() items = [(item, 0) for item in items if str(item).lower().find(query.lower()) >= 0] output = [item[0].toDict() for item in items] return jsonify(output), 200
def get_all_items_recent(): # Get the tags if provided tags = ItemTags.get(request.args.get('tags')) listOfItems = mongo_item_dao.findByMostRecent(tags) output = [item.toDict() for item in listOfItems] return jsonify(output), 200
def get_all_items_by_user(query): # Get the tags if provided tags = ItemTags.get(request.args.get('tags')) listOfItems = mongo_item_dao.findByMostRecent(tags, ' ') output = [item.toDict() for item in listOfItems if item.email == query] return jsonify(output), 200
def get_all_items(): # Get the tags if provided tags = ItemTags.get(request.args.get('tags')) # get list of all items using DAO and specifying the tags listOfItems = mongo_item_dao.findAll(tags) output = [item.toDict() for item in listOfItems] return jsonify(output), 200
def add_item(): name = request.form['name'] desc = request.form['desc'] found = request.form['found'] == 'true' location = ItemLocation([float(request.form['longitude']), float(request.form['latitude'])]) radius = float(request.form['radius']) tags = ItemTags.get(request.form['tags']) timestamp = currTime() email = request.form['email'] username = request.form['username'] # Get the list of uploaded images and convert them to ItemImage objects uploadedImages = request.files.getlist('image') images = [] for i, img in enumerate(uploadedImages): # file will be saved as './uploadedImages/<numImage>_<timestamp>_<origFileName&Type> filePath = str(i) + '_' + str(int(timestamp)) + '_' + img.filename img.save(IMAGE_FOLDER / filePath) images.append(ItemImage(img.filename, img.mimetype, filePath)) item = Item(name=name, desc=desc, found=found, location=location, radius=radius, tags=tags, images=images, timestamp=timestamp, username=username, email=email) # add the item to the database mongo_item_dao.insert(item) # get the user who added matchingUser = mongo_user_dao.findAllMatchingEmail(email) # add the item to their user matchingUser[0].listOfItemIds.append(item.Id) print("item id: " + item.Id) # update the info on the databse mongo_user_dao.update(matchingUser[0]) # want to check whenever an item is added if their are similar items to send notifications to listOfItems = mongo_item_dao.findAll(tags) if item.found is True: listOfItems = [item for item in listOfItems if item.found is False] else: listOfItems = [item for item in listOfItems if item.found is True] simMatch = ItemSimilarity(simModel) simMatch.addItems(listOfItems) simItems, foundStatus = getSimItems(item, simMatch) # send email to those who are notified if len(simItems) != 0: matching = [mongo_user_dao.findAllOptIn(simItem.email) for simItem in simItems] print("Users who have matching: " + str(matching)) sendMail(item, simItems, foundStatus, matching) return jsonify(item.toDict()), 200
def get_all_items_proximitysorted(): # Get the tags if provided tags = ItemTags.get(request.args.get('tags')) lat = request.args.get('lat') lon = request.args.get('lon') listOfItems = mongo_item_dao.findByLocation(tags, lat, lon) output = [item.toDict() for item in listOfItems] return jsonify(output), 200
def update_item(Id): name = request.form['name'] desc = request.form['desc'] found = request.form['found'] == 'true' location = ItemLocation([float(request.form['longitude']), float(request.form['latitude'])]) radius = float(request.form['radius']) tags = ItemTags.get(request.form['tags']) user = User(request.form['username'], request.form['email'], request.form['optIn']) item = Item(Id=Id, name=name, desc=desc, found=found, location=location, radius=radius, tags=tags, username=user) mongo_item_dao.update(item) return jsonify(item.toDict()), 200
def find_similar_items(): """ Route returns all item listings that are similar to the item listing created from the information recieved. Returns ------- A list of item listing dictionaries (which are JSON compatible) and a code representing the success of the request. """ # get all the information needed to make Item instance name = request.args.get('name') desc = request.args.get('desc') found = request.args.get('found') == 'true' location = ItemLocation([float(request.args.get('long')), float(request.args.get('lat'))]) radius = float(request.args.get('radius')) tags = ItemTags.get(request.args.get('tags')) item = Item(name=name, desc=desc, found=found, location=location, radius=radius, tags=tags, images=[], timestamp=None, username=None, email=None) # want to check whenever an item is added if their are similar items to send notifications to listOfItems = mongo_item_dao.findAll(tags) if item.found is True: listOfItems = [item for item in listOfItems if item.found is False] else: listOfItems = [item for item in listOfItems if item.found is True] if len(listOfItems) == 0: return jsonify([]), 200 simMatch = ItemSimilarity(simModel) simMatch.addItems(listOfItems) simItems, foundStatus = getSimItems(item, simMatch) if len(simItems) > 3: simItems = simItems[0:3] return jsonify([x.toDict() for x in simItems]), 200