def get(self): object_id = request.args.get('object_id') current_user = get_jwt_identity() user = UserModel.find_by_id(current_user) if user: i = ObjectModel.find_by_id(object_id) if i: if i.owner_id != user.id: return "non hai accesso a questo elemento", 407 hashtot = [] hashtags = HashtagObjects.find_by_object_id(object_id) for l in hashtags: k = AllHashtags.find_by_id(l) hashtot.append({ "hashtag_id": k.id, "hashtag_name": k.name, "total_times_used": k.total_times_used }) object = { "name": i.name, "description": i.description, "value": i.object_value, "currency": "TODO", "must_be_returned": i.must_be_returned, "must_be_returned_date": i.must_be_returned_date, "shipping_possible": i.shipping_possible, "is_away": i.is_away, "keeper": "TODO", "id": i.id, "hashtags": hashtot } return object, 200 return "object does not exist", 402 return "user does not exist", 401
def add_hashtag(user, id, hashtag_name): if user: object = ObjectModel.find_by_id(int(id)) if object is None: return "oggetto non esiste", 402 if not (object.owner_id == user.id): return "non sei possessore di questo oggetto (in teoria non dovresti essere qui allora)", 406 if object.is_away == True: return "you can't modify object while it's away", 403 hashtag = AllHashtags.find_by_name(hashtag_name) all_object_hashtags_id = HashtagObjects.find_by_object_id( object.id) #lo faccio prima cosi non mi da anche se stesso' if hashtag is None: hashtag = AllHashtags(hashtag_name) else: if hashtag.id in all_object_hashtags_id: return "hashtag gia' associato a questo ogetto", 408 hashtag.total_times_used = hashtag.total_times_used + 1 hashtag.save_to_db() newPair = HashtagObjects(hashtag.id, object.id) newPair.save_to_db() for i in all_object_hashtags_id: if hashtag.id < i: a = hashtag.id b = i else: b = hashtag.id a = i HashtagsPairs.add_or_create_pair(a, b) return "hashtag added successfully", 200
def put(self): id = request.args.get('object_id') name = request.args.get('name') description = request.args.get('description') object_value = request.args.get('object_value') must_be_returned = request.args.get('must_be_returned') must_be_returned_date = request.args.get('must_be_returned_date') shipping_possible = request.args.get('shipping_possible') is_borrowable = request.args.get('is_borrowable') current_user = get_jwt_identity() user = UserModel.find_by_id(current_user) if user: a = ObjectModel.find_by_id(int(id)) if a is None: return "oggetto non esiste", 402 if not (a.owner_id == user.id): return "non sei possessore di questo oggetto (in teoria non dovresti essere qui allora)", 406 if a.is_away == True: return "you can't modify object while it's away", 403 a.name = name a.description = description a.object_value = object_value a.must_be_returned = bool(must_be_returned) a.must_be_returned_date = must_be_returned_date a.shipping_possible = bool(shipping_possible) a.is_borrowable = bool(is_borrowable) a.save_to_db() return "ok", 200 return "user does not exist", 401
def post(self): name = request.args.get('name') description = request.args.get('description') object_value = request.args.get('object_value') must_be_returned = request.args.get('must_be_returned') must_be_returned_date = request.args.get('must_be_returned_date') shipping_possible = request.args.get('shipping_possible') is_borrowable = request.args.get('is_borrowable') hashtags = request.args.get('hashtags') rental_period_id = request.args.get('rental_period_id') rental_cost = request.args.get('rental_cost') current_user = get_jwt_identity() user = UserModel.find_by_id(current_user) if user: #we must check if the user has completed his account setup if not user.has_completed_account(): return "you must complete account setup", 444 currency_id = 1 shipping_possible = True object = ObjectModel(name, description, user.id, object_value, None, must_be_returned, must_be_returned_date, shipping_possible, is_borrowable) object.save_to_db() try: hashtags_array = hashtags.split(",") except: hashtags_array = [] for i in hashtags_array: if i == "": continue add_hashtag(user, object.id, i.strip()) #.replace("+", "")) rental_currency_id = 1 #bla bla metti a posto costs = ObjectCostsModel(object.id, rental_period_id, rental_cost, rental_currency_id) costs.save_to_db() return "object created successfully", 200 return "user does not exist", 401
def get(self): current_user = get_jwt_identity() user = UserModel.find_by_id(current_user) if user: a = ObjectModel.find_by_user_id(user.id) objects = [] for i in a: #ricorda di mettere anche i tag objects.append({ "name": i.name, "description": i.description, "value": i.object_value, "id": i.id }) return objects, 200 return "user does not exist", 401
def delete(self): object_id = int(request.args.get('object_id')) user=UserModel.find_by_id(l.owner_id) if user: object=ObjectModel.find_by_id(object_id) if object is None: return "object does not exist", 403 requestModel=ObjectRequests.find_by_object_id(object_id) if requestModel and requestModel.from_user_id==user.id: requestModel.was_deleted==True requestModel.save_to_db() object.is_requested=False object.save_to_db() return "object un-requested successfully", 200 return "user does not exist", 409
def post(self): number_of_times = request.args.get('number_of_times') object_id = int(request.args.get('object_id')) user=UserModel.find_by_id(l.owner_id) if user: object=ObjectModel.find_by_id(object_id) if object is None: return "object does not exist", 403 if object.is_requested: return "object is already requested, you shouldn't be here", 406 if not object.is_borrowable: return "object isn't borrowable, you shouldn't be here", 406 object_cost=ObjectCosts.find_by_object_id(object_id) if object_cost is None: return "l'oggetto non ha costi e' un casino non dovresti essere qui", 406 requestModel=ObjectRequests(object_id, user.id, object.keeper_id, object_cost.id, int(number_of_times)) requestModel.save_to_db() object.is_requested=True object.save_to_db() return "object requested successfully", 200 return "user does not exist", 409
def get(self): hashtag_name = request.args.get('hashtag') max_distance = request.args.get('distance') hashtag = allHashtags.find_by_name(hashtag_name) if hashtag is None: return [] all = HashtagObjects.find_objects_by_hashtag_id(hashtag.id) total = [] #metti in ordine di posizione prima di iterare for a in all: l = ObjectModel.find_by_id(a) total.append({ 'name': l.name, 'description': l.description, 'posizione': 'Modena', 'value': l.object_value }) possible_matches = HashtagsPairs.find_matching_hashtags(hashtag.id) possible_other_objects = [] for i in possible_matches: objs = HashtagObjects.find_objects_by_hashtag_id(hashtag.id) for j in objs: if j in possible_other_objects: break hashtags = HashtagObjects.find_by_object_id(j) if hashtag.id in hashtags: break object_points = 0 for k in hashtags: hashtag_points = HashtagsPairs.find_pair(k, hashtag_id).count object_points = object_points + hashtag_points object_with_points = ObjectWithPoints(k, object_points) possible_other_objects.append(object_with_points) d = sorted(possible_other_objects, key=lambda x: x.points, reverse=True) return total + d
def get(self): name = request.args.get('name') max_distance = request.args.get('distance') all = ObjectModel.find_all() if name is None: return list(reversed(all)) b = [] for a in all: if a.name: seq = difflib.SequenceMatcher(None, a.name, name) d = seq.ratio() * 100 b.append({"object": a, "simily": d}) c = sorted(b, key=lambda x: x["simily"], reverse=True) final = [] for l in c: object = l["object"] final.append({ "name": object.name, "description": object.description, "location": "Modena", "value": object.object_value }) return final
def get(self): hashtag_name = request.args.get('hashtag') max_distance = request.args.get('distance') hashtag = AllHashtags.find_by_name(hashtag_name) total = [] if hashtag_name == "": all = ObjectModel.find_all() for l in all: user = UserModel.find_by_id(l.owner_id) #devi trovare la posizione dell'oggetto... per il momento (16/06/19), non si possono ancora fare gli scambi quindi metto la posizione dell'utente total.append({ 'name': l.name, 'description': l.description, 'posizione': user.city, 'value': l.object_value, "id": l.id }) return list(reversed(total)) hashtag = AllHashtags.find_by_name(hashtag_name) if hashtag is None: return [{ "name": "HASHTAG INESISTENTE", "description": "", "posizione": "mmm", "value": 0 }] all = HashtagObjects.find_objects_by_hashtag_id(hashtag.id) #metti in ordine di posizione prima di iterare for a in all: l = ObjectModel.find_by_id(a) user = UserModel.find_by_id(l.owner_id) total.append(l) #per il momento li metto solo in ordine di creazione magari nel futuro anche per piu' scambiati o roba cosi' total = sorted(total, key=lambda x: x.creation_date, reverse=True) dict_total = [] for l in total: #devi trovare la posizione dell'oggetto... per il momento (16/06/19), non si possono ancora fare gli scambi quindi metto la posizione dell'utente dict_total.append({ 'name': l.name, 'description': l.description, 'posizione': user.city, 'value': l.object_value, "id": l.id }) possible_matches = HashtagsPairs.find_matching_hashtags(hashtag.id) possible_other_objects = [] for i in possible_matches: objs = HashtagObjects.find_objects_by_hashtag_id(i) for j in objs: hashtags = HashtagObjects.find_by_object_id(j) if hashtag.id in hashtags: continue object_points = 0 for k in hashtags: a = HashtagsPairs.find_pair(k, hashtag.id) if a is None: a = HashtagsPairs.find_pair(hashtag.id, k) if a is None: continue object_points = object_points + a.count object_with_points = ObjectWithPoints(j, object_points) if object_with_points in possible_other_objects: break possible_other_objects.append(object_with_points) d = sorted(possible_other_objects, key=lambda x: x.points, reverse=True) real_objects = [] for l in d: objl = ObjectModel.find_by_id(l.object_id) real_objects.append({ 'name': objl.name, 'description': objl.description, 'location': 'Modena', 'value': objl.object_value }) return total + real_objects