def create_occurrence(username, body):

    last_occurrences, code = db.get_all(Occurrence, {'user': [username]})
    if code != 200:
        return "Erro ao carregar ocorrências passadas do usuário"

    errors = validate_create_occurrence(body, last_occurrences)
    if errors:
        return errors, 400

    try:
        occurrence = Occurrence(
            user=username,
            occurrence_date_time=datetime.datetime.strptime(
                body['occurrence_date_time'], '%Y-%m-%d %H:%M:%S'),
            physical_aggression=body['physical_aggression'],
            victim=body['victim'],
            police_report=body['police_report'],
            gun=body['gun'],
            location=body['location'],
            occurrence_type=body['occurrence_type'])
        result, code = db.insert_one(occurrence)

        return result, code
    except Exception as error:
        logger.error(error)
        return str(error), 401
Exemple #2
0
def create_user(body):

    errors = validate_create_user(body)
    if errors:
        return errors, 400

    user = User(body['username'], body['email'], body['password'],
                body['full_name'])
    result, code = db.insert_one(user)

    return result, code
def create_neighborhood(body):

    try:
        neighborhood = Neighborhood(
            neighborhood=body['neighborhood'],
            city=body['city'],
            state=body['state']
        )
        result, code = db.insert_one(neighborhood)

        return result, code
    except Exception as error:
        logger.error(error)
        return str(error), 401
def create_favorite_place(username, body):
    error = validate_create_favorite_place(body)
    if error:
        return error, 400

    try:
        place = FavoritePlace(
            user=username,
            name=body['name'],
            latitude=body['latitude'],
            longitude=body['longitude'],
        )
        result, code = db.insert_one(place)

        return result, code
    except Exception as error:
        logger.error(error)
        return str(error), 401
Exemple #5
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