def put(self): parser = reqparse.RequestParser() parser.add_argument('exercise_id', help='Send Exerccise id', required=True) parser.add_argument('difficulty', help='Send difficulty', required=False) data = parser.parse_args() exercise = Exercise.find_by_id(data["exercise_id"]) current_user = get_jwt_identity() user = UserModel.find_by_username(current_user) new_resolution = Resolution(start_date=datetime.datetime.now(), end_date=datetime.datetime.now(), ended=False, difficulty=data["difficulty"], final_time=0, user_id=user.id, exercise_id=exercise.id) new_resolution.save_to_db() try: return {'resolution': Resolution.to_json(new_resolution)} except: traceback.print_exc() return {'message': 'Error sending request'}
def put(domain_name): try: dn = DomainName.get(domain_name) ip_address = dn.resolve() if ip_address is None: return error_view(404, "resolution not found") if not IPAddress.exists(ip_address): # the DN resolves a new address ip = IPAddress.new(ip_address) ip.insert() if not Resolution.exists(domain_name, ip_address): # ip change detected resolution = Resolution.new(domain_name, ip_address) resolution.insert() else: resolution = Resolution.get(domain_name, ip_address) resolution.update() return dn_modified_view(dn) except ObjectNotFound as o: return error_view(404, str(o)) except DomainNameResolutionError as dre: return error_view(500, str(dre))
def post(self): parser = reqparse.RequestParser() parser.add_argument('resolution', help='resolution', required=True, action='append') parser.add_argument('elements', help='resolution', required=True, action='append') parser.add_argument('bagWeight', help='resolution', required=True) parser.add_argument('resolution_id', help='Send resolution id', required=True) data = parser.parse_args() resolution = data["resolution"] elements = data["elements"], bag_weight = int(data["bagWeight"]) items_resolution = [] elements_resolution = [] resolution_benefit = 0 for res in resolution: values = res.replace("}", "").split(",") items_resolution.append( Item(values[0].split(":")[1], int(values[1].split(":")[1]), int(values[2].split(":")[1]))) resolution_benefit += int(values[2].split(":")[1]) for res in elements[0]: values = res.replace("}", "").split(",") elements_resolution.append( Item(values[0].split(":")[1], int(values[1].split(":")[1]), int(values[2].split(":")[1]))) max_sum = knapsack_resolver(0, bag_weight, elements_resolution) resolution = Resolution.end_resolution(data["resolution_id"], bag_weight - resolution_benefit) if resolution is not False: return { 'resolution': Resolution.to_json(resolution), "resolution_value": int(max_sum) } else: return {'resolution': False}
def get_reverse(ip_address): if request.method == 'GET': resolution_list = Resolution.list_from_ip(ip_address) if len(resolution_list) == 0: return error_view(404, "no resolution found for this IP") return dn_resolutions_list_view(resolution_list)
def get(domain_name): dn = None dn_tags = [] owned = False followed = False username = get_jwt_identity() try: dn = DomainName.get(domain_name) dn_tags = TagDnIP.list_tags_from_object(dn.domain_name, DOMAIN_NAME_COLLECTION) if not UserDn.exists(username, domain_name): owned = False followed = False else: user_dn = UserDn.get(username, domain_name) owned = user_dn.owned followed = True resolution = Resolution.get_current_from_domain(dn.domain_name) ip = IPAddress.get(resolution.ip_address) ip_tags = TagDnIP.list_tags_from_object(ip.address, IP_ADDRESS_COLLECTION) return dn_retrieved_view(dn, dn_tags, ip, ip_tags, owned, followed) except ObjectNotFound as o: return error_view(404, str(o)) except DomainNameResolutionError: return dn_retrieved_view(dn, dn_tags, None, [], owned, followed)
def post(domain_name): try: if DomainName.exists(domain_name): return error_view(500, f"domain name {domain_name} already exists") dn = DomainName.new(domain_name) dn.insert() ip_address = dn.resolve() # in case resolutions went fine if ip_address is not None: if not IPAddress.exists(ip_address): ip = IPAddress.new(ip_address) ip.insert() resolution = Resolution.new(domain_name, ip_address) resolution.insert() # create user link username = get_jwt_identity() user_dn = UserDn.new(username, dn.domain_name, True) user_dn.insert() return dn_created_view(dn) except DomainNameResolutionError as dre: return error_view(500, str(dre))
def get_resolutions(domain_name): if request.method == 'GET': try: # return the list of IPs resolved by this domain name r = Resolution.get_current_from_domain(domain_name) return dn_resolutions_view(r) except DomainNameResolutionError as de: return error_view(404, str(de))
def post(self): parser = reqparse.RequestParser() parser.add_argument('resolution_id', help='Send resolution id', required=True) data = parser.parse_args() try: resolution = Resolution.end_resolution(data["resolution_id"]) if resolution is not False: return {'resolution': Resolution.to_json(resolution)} else: return {'resolution': False} except: traceback.print_exc() return {'message': 'Error sending request'}
def get(self, exercise_id): try: return { "resolutions": Resolution.get_best_times_at_exercise(exercise_id) } except: traceback.print_exc() return {'Token': False}
def delete(domain_name): try: username = get_jwt_identity() if not UserDn.exists(username, domain_name): return error_view(403, "no ownership found") # remove ownership user_dn = UserDn.get(username, domain_name) if not user_dn.owned: return error_view(403, "you do not own this domain") user_dn.delete() # remove tags dn_linked_tags = TagDnIP.list_tags_from_object(domain_name, DOMAIN_NAME_COLLECTION) for t in dn_linked_tags: t.delete() # remove resolution / IP resolution_list = Resolution.list_from_domain(domain_name) for r in resolution_list: r.delete() ip_address = r.ip_address res_ip_list = Resolution.list_from_ip(ip_address) if len(res_ip_list) == 0: ip_linked_tags = TagDnIP.list_tags_from_object( ip_address, IP_ADDRESS_COLLECTION) for t in ip_linked_tags: t.delete() ip = IPAddress.get(ip_address) ip.delete() dn = DomainName.get(domain_name) dn.delete() return dn_deleted_view(dn) except ObjectNotFound as o: return error_view(404, str(o))
def get(self): try: current_user = get_jwt_identity() exercises_list = Exercise.return_all()["exercises"] user = UserModel.find_by_username(current_user) return { "resolutions": Resolution.get_all_my_resolutions(user.id, exercises_list) } except: traceback.print_exc() return {'Token': False}
def get_resolution_history(domain_name): if request.method == 'GET': out = [] resolution_list = Resolution.list_from_domain(domain_name) for resolution in resolution_list: ip = IPAddress.get(resolution.ip_address) out.append({ "first_updated_at": resolution.first_updated_at.isoformat(), "last_updated_at": resolution.last_updated_at.isoformat(), "ip": ip.json() }) return dn_resolution_history_view(out)
def dn_resolutions_view(resolution: Resolution): return jsonify({ "msg": f"domain name resolutions retrieved", "resolution": resolution.json() }), 200