def createNewScore(request_data): if request_data["rol"] in ["Comprador", "Vendedor"]: try: scored_user_id = ScoreTransactions.create_new_score( request_data) except Exception as e: LOGGER.error("No se pudo crear la calificacion! Error: " + str(e)) return Responses.internalServerError('Error al calificar', "") if scored_user_id == "Calificado": LOGGER.warn("No se pudo crear la calificacion, ya existe") return Responses.badRequest('Ya calificado', "") if scored_user_id is None: LOGGER.warn("No se puede calificar a si mismo") return Responses.badRequest('No se puede calificar a si mismo', "") score_average = ScoreTransactions.find_scored_user_average( scored_user_id) UserTransactions.updateUserScorePoints(scored_user_id, score_average) UserTransactions.pushUserActivitiy( request_data["facebookId"], "Has calificado una publicacion") UserTransactions.pushUserActivitiy( scored_user_id, "Has sido calificado por una publicacion") FirebaseCommunication.send_notification(scored_user_id, "Has recibido una calificacion: " + \ str(request_data["value"]) + " puntos.", "") return Responses.created('Calificado correctamente', "") else: LOGGER.warn("No se pudo crear la calificacion, Rol invalido") return Responses.badRequest('Rol invalido')
def get(self): """Endpoint for checking requests stats""" try: pipeline = [ { '$group': { '_id': {'route': '$route', 'method': '$method', 'day': '$day', 'hour': '$hour'}, 'totalRequests': {'$sum': 1}, 'averageTimeElapsed': {'$avg': '$time_elapsed_ms'} } }, {'$sort': { '_id.day': 1, '_id.hour': 1} } ] aware_colection = monitor_collection.with_options( codec_options=CodecOptions(tz_aware=True, tzinfo=pytz.timezone('America/Argentina/Buenos_Aires'))) cursor = aware_colection.aggregate(pipeline) data = {} for row in cursor: request_string = row['_id']['method'] + ": " + row['_id']['route'] if request_string not in data: data[request_string] = [] data[request_string].append({ 'daytime': str(row['_id']['day']) + ' - ' + str(row['_id']['hour']) + ' hs', 'avg_time_elapsed': row['averageTimeElapsed'], 'totalRequests': row['totalRequests'] }) return_data = Responses.success('Stats obtenidos satisfactoriamente', data) return return_data["data"], return_data["status"], {'message': return_data["message"]} except Exception: return_data = Responses.internalServerError('Error al obtener los stats') return return_data["data"], return_data["status"], {'message': return_data["message"]}
def createNewBuy(request_data): try: BuyTransactions.newBuy(request_data) except Exception: return Responses.internalServerError( 'Error en la comunicacion con el sharedServer') post_data = PostTransactions.find_post_by_post_id( request_data['postId']) FirebaseCommunication.new_chat(request_data['facebookId'], post_data) UserTransactions.pushUserActivitiy(request_data['facebookId'], "Has realizado una compra") UserTransactions.pushUserActivitiy(post_data['_id']['facebookId'], "Has realizado una venta") FirebaseCommunication.send_notification(post_data['_id']['facebookId'], "Has realizado una venta", post_data["title"]) return Responses.created('Compra creada satisfactoriamente', "")
def calculateShipping(shipping_data): try: headers = (SharedServerRequests.__auth()) request_data = SharedServerRequests.__parseEstimation( shipping_data) response = requests.post(SHARED_SERVER_URL + '/api/deliveries/estimate', headers=headers, data=json.dumps(request_data)) if response.status_code == 200: return Responses.success( 'Estimacion realizada satisfactoriamente', { "ShipmentCost": json.loads( response.text)['ShipmentCost'] }) else: return Responses.internalServerError( 'Error en la comunicacion con el Shared Server', "") except Exception: return Responses.badRequest("Direccion invalida!", "")