def register(): try: data = registerSchema.validate(request.json) except SchemaError as e: error = { 'code': 'auth.register.invalid', 'message': 'Invalid data for register', 'details': str(e) } return jsonify(error=error), 403 if User.by_username(request.json['username']).count() > 0: error = { 'code': 'auth.register.username_taken', 'message': 'Username already taken' } return jsonify(error=error), 403 if User.by_email(request.json['email']).count() > 0: error = { 'code': 'auth.register.email_in_use', 'message': 'A user with the supplied email already exists' } return jsonify(error=error), 403 user = userstore.create_user(**data) session.add(user) session.commit() login_user(user, True) auth_token = user.get_auth_token() return jsonify(user=user.as_personal_json(), auth_token=auth_token), 201
def user_workouts(username): user = User.by_username(username) user = first_or_404(user) workouts = Workout.query.filter_by(user_id=user.id) \ .filter_by(is_private=False) \ .order_by(Workout.created_at.desc()) return jsonify(workouts=[workout.as_json() for workout in workouts], user=user.as_json())
def test_can_register(self, flask, dbtransaction): data = { 'full_name': 'Alice', 'email': '*****@*****.**', 'password': '******', 'username': '******' } result = flask.post('/auth/register', data=json.dumps(data), headers={'Content-Type': 'application/json'}) assert result.status_code == 201 assert result.json['user']['email'] == '*****@*****.**' assert result.json['user']['username'] == 'alice' assert result.json['user']['full_name'] == 'Alice' assert User.by_username('alice').count() == 1
def sync_strava_workouts(user_id, auth_token): user = User.by_id(user_id) if user.count() != 1: raise SyncWorkerException("strava") user = user.one() page = 0 items_per_page = 25 while True: response = fetch_strava_activities(auth_token, page, items_per_page) total_pages = math.ceil(len(response) / items_per_page) for activity in response: if not has_synced_workout(response["id"], "strava"): sync_strava_activity(user, auth_token, activity) page += 1 if page >= total_pages: break
def sync_runkeeper_workouts(user_id, auth_token): user = User.by_id(user_id) if user.count() != 1: raise SyncWorkerException('runkeeper') user = user.one() page = 0 items_per_page = 25 while True: response = fetch_runkeeper_activities(auth_token, page, items_per_page) total_pages = math.ceil(response['size' / items_per_page]) for activity in response['items']: identifier = parse_runkeeper_identifier(activity['uri']) if not has_synced_workout(identifier, 'runkeeper'): sync_runkeeper_activtiy(identifier, user, auth_token) page += 1 if page >= total_pages: break
def user_by_username(username): user = User.by_username(username) user = first_or_404(user) return jsonify(user=user.as_json())