コード例 #1
0
def get_all_neighborhoods(city=None, state=None):
    filter_ = {} if state or city else None
    if city:
        filter_.update({'city': [city]})
    if state:
        filter_.update({'state': [state]})
    result, code = db.get_all(Neighborhood, filter_)
    if result:
        if code == 200:
            neighborhoods = [get_row_dict(u) for u in result]
            # getting statistics
            for index, _ in enumerate(neighborhoods):
                filter_ = {
                          "id_neighborhood": [
                                neighborhoods[index]['id_neighborhood']
                          ]
                         }
                ratings, status = db.get_all(Rating, filter_)
                if status != 200:
                    return ratings, status
                ratings = [r.to_dict() for r in ratings]
                if ratings:
                    statistics = get_neighborhood_statistics(ratings)
                    neighborhoods[index].update({'statistics': statistics})

            return neighborhoods, code
        return result, code
    return [], 200
コード例 #2
0
def get_all_users():
    result, code = db.get_all(User)

    if code == 200:  # if successful, returns the data
        users = [u.to_dict() for u in result]  # converts rows to dict
        return users, code
    return result, code
コード例 #3
0
def get_all_occurrences(user=None, occurrence_type=None):
    filter_ = {} if user or occurrence_type else None
    # formating occurrence_type query param
    if occurrence_type:
        occurrence_type = occurrence_type.split(',')

        # strip white spaces from start and end
        occurrence_type = [o.strip() for o in occurrence_type]

        # validating occurrence_type
        if (False in [
                validate_occurrence_type(occur_type)
                for occur_type in occurrence_type
        ]):
            return "Tipo de ocorrência inválido", 400

        filter_.update({"occurrence_type": occurrence_type})

    if user:
        filter_.update({'user': [user]})

    result, code = db.get_all(Occurrence, filter_)

    if code == 200:  # if successful, returns the data
        # converts rows to dict
        occurrences = [occurrence.to_dict() for occurrence in result]
        return occurrences, code
    return result, code
コード例 #4
0
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
コード例 #5
0
def get_favorite_places(username):
    result, code = db.get_all(FavoritePlace, {'user': [username]})

    if result:
        if code == 200:  # if successful, returns the data
            # converts rows to dict
            favorite_places = [get_row_dict(place) for place in result]
            return favorite_places, code
    return [], 200
コード例 #6
0
def get_all_ratings(user=None, neighborhood=None):
    # formatting filters
    filter_ = {} if user or neighborhood else None
    if user:
        filter_.update({'user': [user]})
    if neighborhood:
        filter_.update({'id_neighborhood': [neighborhood]})

    result, code = db.get_all(Rating, filter_)

    if result:
        if code == 200:
            ratings_neighborhood = [u.to_dict() for u in result]
            return ratings_neighborhood, code
        return result, code
    return [], 200
コード例 #7
0
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
コード例 #8
0
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
コード例 #9
0
ファイル: api.py プロジェクト: MichalDotPy/Modul_9.4
def get_all_albums():
    return jsonify(db.get_all())
コード例 #10
0
def list_display():
    albums = db.get_all().values()
    return render_template('list.html',
                           albums=albums,
                           urls=URLS_DICT,
                           db_stat=db.get_stats())