Пример #1
0
def get_one_user(username):
    result, code = db.get_one(User, username)

    if code == 200:  # if successful, returns the data
        user = result.to_dict()  # converts row to dict
        return user, 200
    return result, code  # else, returns database error and error code
Пример #2
0
def update_user(username, body):
    fields = [
        'email', 'username', 'full_name', 'password', 'device_token',
        'show_notifications'
    ]

    result, code = db.get_one(User, username)

    if code == 404:
        return result, code

    user = get_row_dict(result)

    params = get_params_by_body(fields, body)

    errors = validate_update_user(body, params)
    if errors:
        return errors, 400

    if 'password' in params:
        params['password'] = BCRYPT.generate_password_hash(
            params['password']).decode('utf-8')

    result, code = db.update(User, username, params)

    if code == 200:  # if successful, returns the data
        if 'device_token' in body:
            send_notification_on_signin(user, username, body)
        user = get_row_dict(result)  # converts row to dict
        return user, code
    return result, code  # else, returns database error and error code
Пример #3
0
def update_rating(rating_id, body, username=None):

    result, status = db.get_one(Rating, rating_id)
    rating_before_update = result.to_dict(del_null_attr=False)

    if status == 404:
        return 'Erro ao achar avaliação', 404

    errors = validate_update_rating(body, rating_before_update)
    if errors:
        return errors, 400

    details = []
    for attr, value in rating_before_update['details'].items():
        if attr in body:
            details.append(body[attr])
        else:
            details.append(value)
    body = dict(filter(lambda x: x[0] == 'rating_neighborhood', body.items()))
    body['details'] = details

    result, code = db.update(Rating, rating_id, body, username)

    if code == 200:
        rating = result.to_dict()
        return rating, code

    return result, code
Пример #4
0
def get_one_rating(rating_id):
    result, code = db.get_one(Rating, rating_id)

    if code == 200:
        rating = result.to_dict()
        return rating, 200
    return result, code
def get_one_occurrence(id_occurrence):
    result, code = db.get_one(Occurrence, id_occurrence)

    if code == 200:  # if successful, returns the data
        occurrence = result.to_dict()  # converts row to dict
        return occurrence, 200
    return result, code  # else, returns database error and error code
def delete_favorite_place(username, id_place):
    result, code = db.get_one(FavoritePlace, id_place)

    if code == 200:
        favorite_place = get_row_dict(result)

        if favorite_place['user'] != username:
            return "O usuário não tem permissão para deletar esse local favorito", 401

        result, code = db.delete(FavoritePlace, id_place)

    return result, code
def authentication(auth=None):
    if not auth:
        status = 401
        response = "É necessário realizar o login"
    elif not auth['username'] or not auth['password']:
        status = 401
        response = "O nome de usuário e senha são obrigatórios"
    else:
        result, status_ = get_one(User, auth['username'])
        if status_ == 200:
            response, status = get_auth_response(result, auth)
        else:
            response = result
            status = status_
    return response, status
def get_one_neighborhood(neighborhood_id):
    result, code = db.get_one(Neighborhood, neighborhood_id)

    if code == 200:
        # formating filter
        filter_ = {"id_neighborhood": [neighborhood_id]}
        ratings, status = db.get_all(Rating, filter_)
        if status != 200:
            return ratings, status
        ratings = [r.to_dict() for r in ratings]

        neighborhood = get_row_dict(result)
        if ratings:
            statistics = get_neighborhood_statistics(ratings)
            neighborhood.update({'statistics': statistics})
        return neighborhood, 200
    return result, code
def update_occurrence(id_occurrence, body, username=None):

    current_occurrence, code = db.get_one(Occurrence, id_occurrence)
    if code != 200:
        return current_occurrence, code

    fields = [
        'occurrence_date_time', 'physical_aggression', 'victim',
        'police_report', 'gun', 'location', 'occurrence_type'
    ]

    params = get_params_by_body(fields, body)

    errors = validate_update_occurrence(body, params, current_occurrence)
    if errors:
        return errors, 400

    result, code = db.update(Occurrence, id_occurrence, params, username)

    if code == 200:  # if successful, returns the data
        occurrence = result.to_dict()  # converts row to dict
        return occurrence, code
    return result, code  # else, returns database error and error code
def send_notifications_near_occcurrences(occurrence, username):
    result, code = get_all(FavoritePlace)

    favorite_places = [get_row_dict(u) for u in result]

    near_favorite_places = set()

    for favorite_place in favorite_places:
        if haversine(favorite_place, occurrence) != None:
            user = get_row_dict(get_one(User, favorite_place['user'])[0])

            if user['show_notifications'] and user['username'] != username:
                near_favorite_places.add(user['device_token'])

    message = {
        'to': [token for token in near_favorite_places],
        'sound': 'default',
        'title': 'Atenção!',
        'body':
        f'Uma ocorrência foi reportada perto de um de seus locais favoritos!',
        'data': {
            'data': occurrence
        },
    }

    headers = {
        "Accept": "*/*",
        'Accept-encoding': 'gzip, deflate',
        "Content-Type": "application/json"
    }

    response = requests.post('https://exp.host/--/api/v2/push/send',
                             data=json.dumps(message),
                             headers=headers)

    return occurrence, 200
Пример #11
0
def create_rating(body, username, neighborhood_id):

    neighborhood, code = db.get_one(Neighborhood, neighborhood_id)

    if code == 404:
        return 'Bairro não existe', 404
    errors = validate_create_rating(body)
    if errors:
        return errors, 400

    details = dict(
        filter(lambda x: x[0] != 'rating_neighborhood', body.items()))

    try:
        rating = Rating(user=username,
                        id_neighborhood=neighborhood_id,
                        rating_neighborhood=body['rating_neighborhood'],
                        details=details)
        result, code = db.insert_one(rating)

        return result, code
    except Exception as error:
        logger.error(error)
        return str(error), 401