Exemple #1
0
def updateUserStatistics(account_id):
    try:
        req = request.get_json()
        user = User.query.filter_by(account_id=account_id).options(
            db.load_only("id")).first()
        stats = Statistics(user_id=user.id,
                           wins=req['wins'],
                           losses=req['losses'],
                           score=req['score'],
                           level=req['level'])
        db.session.add(stats)
        db.session.commit()
        resp = {"success": "statistics has been added successfully"}
        return respond(resp, 201)
    except:
        return respond({"error": "user not found"}, 404)
def add_statistics():
    try:
        for data in request.json['country_statistics']:
            country = data['country']
            code = data['code']
            flag = data['flag']
            coordinates = data['coordinates']
            confirmed = data['confirmed']
            deaths = data['deaths']
            recovered = data['recovered']

            all_states = []
            for state in data['states']:
                states = States(key=state['key'],
                                name=state['name'],
                                address=state['address'],
                                latitude=state['latitude'],
                                longitude=state['longitude'],
                                confirmed=state['confirmed'],
                                deaths=state['deaths'],
                                recovered=state['recovered'])
                states.save()
                all_states.append(states)

            statistics = Statistics(country=country,
                                    code=code,
                                    flag=flag,
                                    coordinates=coordinates,
                                    confirmed=confirmed,
                                    deaths=deaths,
                                    recovered=recovered,
                                    states=all_states)
            statistics.save()

        return make_response(jsonify({"success": request.json}), 201)

    except KeyError:
        abort(400)
    h_name=game['home_team']['country']
    h_ball_possession = game['home_team_statistics']['ball_possession']
    a_goals = game['away_team']['goals']
    a_name=game['away_team']['country']
    a_ball_possession = game['away_team_statistics']['ball_possession']
    h_distance_covered = game['home_team_statistics']['distance_covered']
    a_distance_covered = game['away_team_statistics']['distance_covered']
    h_pass_accuracy = game['home_team_statistics']['pass_accuracy']
    a_pass_accuracy = game['away_team_statistics']['pass_accuracy']
    h_on_target = game['home_team_statistics']['on_target']
    a_on_target = game['away_team_statistics']['on_target']

    h_stats = Statistics(name=h_name,game = game_object,
                         team = h_team,
                        ball_possession =h_ball_possession,
                        goals = h_goals,
                        distance_covered = h_distance_covered,
                        pass_accuracy = h_pass_accuracy,
                        on_target = h_on_target)
    a_stats = Statistics(name=a_name,game = game_object,
                        team = a_team,
                        ball_possession =a_ball_possession,
                        goals = a_goals,
                        distance_covered = a_distance_covered,
                        pass_accuracy = a_pass_accuracy,
                        on_target = a_on_target)
    all_stats.append(h_stats)
    all_stats.append(a_stats)
# db.session.add_all(all_game_objects)
# db.session.commit()