API: use of f-strings

This commit is contained in:
Sam 2018-05-23 17:30:22 +02:00
parent 91566b27e0
commit 8b90eb1566
4 changed files with 13 additions and 19 deletions

View File

@ -77,9 +77,7 @@ def get_activity_gpx(auth_user_id, activity_id):
if not activity.gpx or activity.gpx == '':
response_object = {
'status': 'fail',
'message': 'No gpx file for this activity (id: {})'.format(
activity_id
)
'message': f'No gpx file for this activity (id: {activity_id})'
}
return jsonify(response_object), 400
@ -103,7 +101,7 @@ def get_activity_gpx(auth_user_id, activity_id):
else:
gpx_content = ''
status = 'not found'
message = 'Activity not found (id: {})'.format(activity_id)
message = f'Activity not found (id: {activity_id})'
code = 404
response_object = {

View File

@ -86,7 +86,7 @@ class Sport(db.Model):
backref=db.backref('sports', lazy='joined'))
def __repr__(self):
return '<Sport {!r}>'.format(self.label)
return f'<Sport {self.label!r}>'
def __init__(self, label):
self.label = label
@ -149,8 +149,7 @@ class Activity(db.Model):
single_parent=True))
def __str__(self):
return '<Activity \'{}\' - {}>'.format(
self.sports.label, self.activity_date, )
return f'<Activity \'{self.sports.label}\' - {self.activity_date}>'
def __init__(self, user_id, sport_id, activity_date, distance, duration):
self.user_id = user_id
@ -270,8 +269,8 @@ class ActivitySegment(db.Model):
ave_speed = db.Column(db.Numeric(6, 2), nullable=True) # km/h
def __str__(self):
return '<Segment \'{}\' for activity \'{}\'>'.format(
self.segment_id, self.activity_id, )
return (f'<Segment \'{self.segment_id}\' '
f'for activity \'{self.activity_id}\'>')
def __init__(self, segment_id, activity_id):
self.segment_id = segment_id
@ -319,11 +318,9 @@ class Record(db.Model):
_value = db.Column("value", db.Integer, nullable=True)
def __str__(self):
return '<Record {} - {} - {}>'.format(
self.sports.label,
self.record_type,
self.activity_date.strftime('%Y-%m-%d')
)
return (f'<Record {self.sports.label} - '
f'{self.record_type} - '
f"{self.activity_date.strftime('%Y-%m-%d')}>")
def __init__(self, activity, record_type):
self.user_id = activity.user_id

View File

@ -47,8 +47,7 @@ def create_activity(
new_activity.title = title
else:
sport = Sport.query.filter_by(id=new_activity.sport_id).first()
new_activity.title = '{} - {}'.format(sport.label,
new_activity.activity_date)
new_activity.title = f'{sport.label} - {new_activity.activity_date}'
if gpx_data:
new_activity.gpx = gpx_data['filename']
@ -189,9 +188,9 @@ def get_file_path(auth_user_id, activity_file):
def get_new_file_path(auth_user_id, activity_date, activity_file, sport):
old_filename = secure_filename(activity_file.filename)
extension = '.{}'.format(old_filename.rsplit('.', 1)[1].lower())
extension = f".{old_filename.rsplit('.', 1)[1].lower()}"
_, new_filename = tempfile.mkstemp(
prefix='{}_{}_'.format(activity_date, sport),
prefix=f'{activity_date}_sport_',
suffix=extension
)
dir_path = os.path.join(

View File

@ -27,7 +27,7 @@ class User(db.Model):
backref=db.backref('users', lazy='joined'))
def __repr__(self):
return '<User %r>' % self.username
return f'<User {self.username!r}>'
def __init__(
self, username, email, password,