API & Client: add max speed in filters

This commit is contained in:
Sam 2018-06-11 22:42:04 +02:00
parent 941c9958f9
commit 5d749f3f47
8 changed files with 83 additions and 4 deletions

View File

@ -41,6 +41,8 @@ def get_activities(auth_user_id):
duration_to = params.get('duration_to') duration_to = params.get('duration_to')
ave_speed_from = params.get('ave_speed_from') ave_speed_from = params.get('ave_speed_from')
ave_speed_to = params.get('ave_speed_to') ave_speed_to = params.get('ave_speed_to')
max_speed_from = params.get('max_speed_from')
max_speed_to = params.get('max_speed_to')
order = params.get('order') order = params.get('order')
sport_id = params.get('sport_id') sport_id = params.get('sport_id')
per_page = int(params.get('per_page')) if params.get('per_page') else 5 per_page = int(params.get('per_page')) if params.get('per_page') else 5
@ -60,6 +62,10 @@ def get_activities(auth_user_id):
if ave_speed_from else True, if ave_speed_from else True,
Activity.ave_speed <= float(ave_speed_to) Activity.ave_speed <= float(ave_speed_to)
if ave_speed_to else True, if ave_speed_to else True,
Activity.max_speed >= float(max_speed_from)
if max_speed_from else True,
Activity.max_speed <= float(max_speed_to)
if max_speed_to else True,
).order_by( ).order_by(
Activity.activity_date.asc() Activity.activity_date.asc()
if order == 'asc' if order == 'asc'

View File

@ -155,6 +155,8 @@ class Activity(db.Model):
duration_to = params.get('duration_to') if params else None duration_to = params.get('duration_to') if params else None
ave_speed_from = params.get('ave_speed_from') if params else None ave_speed_from = params.get('ave_speed_from') if params else None
ave_speed_to = params.get('ave_speed_to') if params else None ave_speed_to = params.get('ave_speed_to') if params else None
max_speed_from = params.get('max_speed_from') if params else None
max_speed_to = params.get('max_speed_to') if params else None
sport_id = params.get('sport_id') if params else None sport_id = params.get('sport_id') if params else None
previous_activity = Activity.query.filter( previous_activity = Activity.query.filter(
Activity.id != self.id, Activity.id != self.id,
@ -177,6 +179,10 @@ class Activity(db.Model):
if ave_speed_from else True, if ave_speed_from else True,
Activity.ave_speed <= float(ave_speed_to) Activity.ave_speed <= float(ave_speed_to)
if ave_speed_to else True, if ave_speed_to else True,
Activity.max_speed >= float(max_speed_from)
if max_speed_from else True,
Activity.max_speed <= float(max_speed_to)
if max_speed_to else True,
).order_by( ).order_by(
Activity.activity_date.desc() Activity.activity_date.desc()
).first() ).first()

View File

@ -487,6 +487,37 @@ def test_get_activities_ave_speed_filter(
assert 'Fri, 23 Feb 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa assert 'Fri, 23 Feb 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
def test_get_activities_max_speed_filter(
app, user_1, sport_1_cycling, sport_2_running, activity_cycling_user_1,
activity_running_user_1
):
activity_cycling_user_1.max_speed = 25
activity_running_user_1.max_speed = 11
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(
email='test@test.com',
password='12345678'
)),
content_type='application/json'
)
response = client.get(
'/api/activities?max_speed_from=10&max_speed_to=20',
headers=dict(
Authorization='Bearer ' + json.loads(
resp_login.data.decode()
)['auth_token']
)
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert 'Sun, 01 Apr 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa
def test_get_activities_sport_filter( def test_get_activities_sport_filter(
app, user_1, sport_1_cycling, seven_activities_user_1, sport_2_running, app, user_1, sport_1_cycling, seven_activities_user_1, sport_2_running,
activity_running_user_1 activity_running_user_1

View File

@ -133,6 +133,36 @@ export default function ActivitiesList (props) {
</div> </div>
</label> </label>
</div> </div>
<div className="form-group">
<label>
Max speed (km/h):
<div className="container">
<div className="row">
<div className="col-5">
<input
className="form-control"
min={0}
name="max_speed_from"
onChange={e => updateParams(e)}
step="1"
type="number"
/>
</div>
<div className="col-2 align-middle text-center">to</div>
<div className="col-5">
<input
className="form-control"
min={0}
name="max_speed_to"
onChange={e => updateParams(e)}
step="1"
type="number"
/>
</div>
</div>
</div>
</label>
</div>
<input <input
className="btn btn-primary btn-lg btn-block" className="btn btn-primary btn-lg btn-block"
onClick={() => loadActivities()} onClick={() => loadActivities()}

View File

@ -7,7 +7,7 @@ import { getDateWithTZ } from '../../utils'
export default function ActivitiesList (props) { export default function ActivitiesList (props) {
const { activities, sports, user } = props const { activities, sports, user } = props
return ( return (
<div className="card"> <div className="card activity-card">
<div className="card-body"> <div className="card-body">
<table className="table"> <table className="table">
<thead> <thead>
@ -18,6 +18,7 @@ export default function ActivitiesList (props) {
<th scope="col">Distance</th> <th scope="col">Distance</th>
<th scope="col">Duration</th> <th scope="col">Duration</th>
<th scope="col">Ave. speed</th> <th scope="col">Ave. speed</th>
<th scope="col">Max. speed</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -49,6 +50,7 @@ export default function ActivitiesList (props) {
</td> </td>
<td className="text-right">{activity.duration}</td> <td className="text-right">{activity.duration}</td>
<td className="text-right">{activity.ave_speed} km/h</td> <td className="text-right">{activity.ave_speed} km/h</td>
<td className="text-right">{activity.max_speed} km/h</td>
</tr> </tr>
))} ))}
</tbody> </tbody>

View File

@ -59,7 +59,7 @@ class Activities extends React.Component {
updateParams={e => this.setParams(e)} updateParams={e => this.setParams(e)}
/> />
</div> </div>
<div className="col-md-9"> <div className="col-md-9 activities-result">
<ActivitiesList <ActivitiesList
activities={activities} activities={activities}
sports={sports} sports={sports}

View File

@ -83,6 +83,10 @@ input, textarea {
margin-top: 20px; margin-top: 20px;
} }
.activities-result {
font-size: 0.9em;
}
.activity-sport { .activity-sport {
margin-right: 1px; margin-right: 1px;
max-width: 20px; max-width: 20px;

View File

@ -48,10 +48,10 @@ export default function RecordsCard (props) {
<td> <td>
{rec.record_type} {rec.record_type}
</td> </td>
<td> <td className="text-right">
{rec.value} {rec.value}
</td> </td>
<td> <td className="text-right">
<Link to={`/activities/${rec.activity_id}`}> <Link to={`/activities/${rec.activity_id}`}>
{rec.activity_date} {rec.activity_date}
</Link> </Link>