Пример #1
0
    def get(self, id):
        """
        Return all the calories for the specified user
        """
        current_user = User.find_by_username(get_jwt_identity()['username'])

        if current_user.is_admin() or current_user.has_child(id):
            user = User.query.filter_by(id=id).first()
            if user is None:
                return custom_response(
                    404,
                    "User {} not found".format(id)
                )
            calories = Calorie.query.with_parent(user).all()
            calories = list_to_array(calories)
            return custom_response(
                200,
                "{} calories".format(user.username),
                calories
            )
        else:
            return custom_response(
                401,
                "Permission denied. User {} not a child".format(id)
            )
Пример #2
0
    def post(self):
        """
        Return the emotion predicted for the specified image
        """
        if not request.files:
            abort(400)

        image = request.files.get("image", "")

        timestamp = calendar.timegm(time.gmtime())
        image_name = str(timestamp) + ".jpg"
        image.save(os.path.join(image_save_path, image_name))

        new_face_path = make_scraper(image_save_path + image_name)

        if new_face_path is None:
            if not SAVE_IMG:
                os.remove(image_save_path + image_name)
            return custom_response(404, "No face found")
        else:
            prediction = predict_emotion(new_face_path)
            if not SAVE_IMG:
                os.remove(image_save_path + image_name)
                os.remove(new_face_path)
            if prediction is None:
                return custom_response(404, "No emotion recognise")
            else:
                return custom_response(200, prediction)
Пример #3
0
 def delete(self, id):
     """
     Delete the specified user
     """
     user = User.query.filter_by(id=id).first()
     if user is None:
         return custom_response(404, "User {} not found".format(id))
     user.delete()
     return custom_response(200, "User {} deleted".format(id))
Пример #4
0
 def get(self, id):
     """
     Return the information of the specified user
     """
     user = User.query.filter_by(id=id).first()
     if user is None:
         return custom_response(404, "User {} not found".format(id))
     return custom_response(200, "User {}".format(user.username),
                            user.to_dict())
Пример #5
0
 def post(self):
     """
     Revoke user refresh token
     """
     jti = get_raw_jwt()['jti']
     try:
         revoked_token = RevokedToken(jti=jti)
         revoked_token.save()
         return custom_response(200, 'Refresh token has been revoked')
     except:
         return custom_response(500, 'Something went wrong')
Пример #6
0
    def delete(self, id):
        """
        Delete an activity by its id
        """
        current_user = User.find_by_username(get_jwt_identity()['username'])
        activity = Activity.query.with_parent(current_user).filter_by(
            id=id).first()

        if activity is None:
            return custom_response(404,
                                   "Activity with id {} not found".format(id))

        activity.delete()
        return custom_response(200, "Activity {} deleted".format(id))
Пример #7
0
 def get(self):
     """
     Return all the users
     """
     users = User.query.all()
     users = list_to_array(users)
     return custom_response(200, "All users", users)
Пример #8
0
 def get(self):
     """
     Return all the activities
     """
     activities = Activity.query.all()
     activities = list_to_array(activities)
     return custom_response(200, "All activities", activities)
Пример #9
0
 def get(self):
     """
     Return all the users' personal info
     """
     personal_info = PersonalInfo.query.all()
     personal_info = list_to_array(personal_info)
     return custom_response(200, "All personal info", personal_info)
Пример #10
0
 def get(self):
     """
     Return all the steps
     """
     steps = Step.query.all()
     steps = list_to_array(steps)
     return custom_response(200, "All steps", steps)
Пример #11
0
 def get(self):
     """
     Return all the meters
     """
     meters = Meter.query.all()
     meters = list_to_array(meters)
     return custom_response(200, "All meters", meters)
Пример #12
0
 def get(self):
     """
     Return all the heart rates
     """
     heart_rates = HeartRate.query.all()
     heart_rates = list_to_array(heart_rates)
     return custom_response(200, "All Heart Rates", heart_rates)
Пример #13
0
    def post(self):
        """
        Add a new image for the caller user
        """

        current_user = User.find_by_username(get_jwt_identity()['username'])

        current_dir = "{}{}/".format(image_save_path, current_user.id)

        if not os.path.exists(current_dir):
            os.makedirs(current_dir, exist_ok=True)

        data = request.get_json()
        image = base64.b64decode(data['image'])

        timestamp = calendar.timegm(time.gmtime())
        image_name = str(timestamp) + ".jpg"

        current_path = "{}/{}".format(current_dir, image_name)

        with open(current_path, 'wb') as f:
            f.write(image)

        custom_link = "{}/images/{}/{}".format(server_url, current_user.id,
                                               image_name)
        return custom_response(200, "Image saved", custom_link)
Пример #14
0
 def get(self):
     """
     Return the information for the caller user
     """
     user = User.find_by_username(get_jwt_identity()['username'])
     return custom_response(200, "User {}".format(user.username),
                            user.to_dict())
Пример #15
0
 def get(self):
     """
     Return all the steps for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     steps = Step.query.with_parent(current_user).all()
     steps = list_to_array(steps)
     return custom_response(200, "Your steps", steps)
Пример #16
0
 def get(self):
     """
     Return all the meters for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     meters = Meter.query.with_parent(current_user).all()
     meters = list_to_array(meters)
     return custom_response(200, "Your meters", meters)
Пример #17
0
 def get(self):
     """
     Return all the heart rates for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     heart_rates = HeartRate.query.with_parent(current_user).all()
     heart_rates = list_to_array(heart_rates)
     return custom_response(200, "Your heart rates", heart_rates)
Пример #18
0
 def get(self):
     """
     Return all activities for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     activities = Activity.query.with_parent(current_user).all()
     activities = list_to_array(activities)
     return custom_response(200, "Your activities", activities)
Пример #19
0
 def get(self):
     """
     Return all the personal info for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     personal_info = PersonalInfo.query.with_parent(current_user).order_by(
         PersonalInfo.timestamp.desc()).all()
     personal_info = list_to_array(personal_info)
     return custom_response(200, "Your personal info", personal_info)
Пример #20
0
    def post(self):
        """
        Add a new calorie for the caller user
        """
        current_user = User.find_by_username(get_jwt_identity()['username'])

        calorie = request.get_json()

        current_date = datetime.now()
        day_stat = current_date.replace(hour=0, minute=0, second=0, microsecond=0)
        day_end = current_date.replace(hour=23, minute=59, second=59, microsecond=59)

        old_calorie = Calorie.query.filter(User.id == current_user.id,
                                           Calorie.timestamp >= day_stat,
                                           Calorie.timestamp <= day_end).first()

        if old_calorie is None:
            new_calorie = Calorie(
                timestamp=current_date,
                value=calorie['value'],
                user=current_user
            )
            new_calorie.save()
            return custom_response(
                200,
                "Calorie added",
                new_calorie.id
            )

        else:
            if old_calorie.value <= calorie['value']:
                old_calorie.value = calorie['value']
                old_calorie.update()
                return custom_response(
                    200,
                    "Calorie updated",
                    old_calorie.id
                )
            else:
                return custom_response(
                    200,
                    "Calorie already updated",
                    old_calorie.id
                )
Пример #21
0
 def get(self):
     """
     Return all the children for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     children = current_user.family_members.all()
     children = list_to_array(children)
     return custom_response(200,
                            "{}'s children".format(current_user.username),
                            children)
Пример #22
0
 def get(self):
     """
     Return all the calories
     """
     calories = Calorie.query.all()
     calories = list_to_array(calories)
     return custom_response(
         200,
         "All calories",
         calories
     )
Пример #23
0
 def post(self):
     """
     Add a new activity for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     activity = request.get_json()
     new_activity = Activity(name=activity['name'],
                             datetime=activity['datetime'],
                             duration=activity['duration'],
                             user=current_user)
     new_activity.save()
     return custom_response(200, "Activity added", new_activity.id)
Пример #24
0
 def get(self):
     """
     Return all the calories for the caller user
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     calories = Calorie.query.with_parent(current_user).all()
     calories = list_to_array(calories)
     return custom_response(
         200,
         "Your calories",
         calories
     )
Пример #25
0
 def put(self, id):
     """
     Update information for the specified user
     """
     user = User.query.filter_by(id=id).first()
     new_user = request.get_json()
     user.full_name = new_user['full_name']
     user.date_of_birth = new_user['date_of_birth']
     user.gender = new_user['gender']
     user.update()
     return custom_response(200, "Your information was updated",
                            user.to_dict())
Пример #26
0
    def get(self, user, image):
        """
        Return the image requested
        """
        current_user = User.find_by_username(get_jwt_identity()['username'])

        if current_user.id != user:
            return custom_response(401, "Permission denied")

        current_dir = "{}{}".format(image_save_path, user)

        return send_from_directory(current_dir, image)
Пример #27
0
 def put(self):
     """
     Update the information of the caller user
     """
     user = User.find_by_username(get_jwt_identity()['username'])
     new_user = request.get_json()
     user.full_name = new_user['full_name']
     user.date_of_birth = new_user['date_of_birth']
     user.gender = new_user['gender']
     user.update()
     return custom_response(200, "Your information was updated",
                            user.to_dict())
Пример #28
0
 def post(self):
     """
     Refresh token
     """
     current_user = User.find_by_username(get_jwt_identity()['username'])
     tmp_user = current_user.to_dict()
     tmp_user.update({
         'access_token':
         create_access_token(create_identity(current_user),
                             expires_delta=False),
         'refresh_token':
         create_refresh_token(create_identity(current_user))
     })
     return custom_response(200, 'Access token refreshed', tmp_user)
Пример #29
0
    def post(self):
        """
        Add a new user
        """
        body = request.get_json()

        if User.find_by_username(body['username']):
            return custom_response(
                401, 'User {} already exists'.format(body['username']))

        if (body['username'] == '' or body['password'] == ''
                or body['password'] != body['confirm_password']
                or body['full_name'] == '' or body['gender'] > 1
                or body['date_of_birth'] == ''):
            return custom_response(400, "Invalid parameters")

        user = User(
            username=body['username'],
            password=User.generate_hash(body['password']),
            role=USER_ROLE['user'],
            full_name=body['full_name'],
            gender=body['gender'],
            date_of_birth=body['date_of_birth'],
        )
        try:
            user.save()
            access_token = create_access_token(identity=create_identity(user))
            refresh_token = create_refresh_token(
                identity=create_identity(user))
            return custom_response(200,
                                   'User {} was created'.format(user.username),
                                   {
                                       'access_token': access_token,
                                       'refresh_token': refresh_token
                                   })
        except:
            return custom_response(500, 'Something went wrong')
Пример #30
0
    def post(self):
        """
        Add new step for the caller user
        """
        current_user = User.find_by_username(get_jwt_identity()['username'])

        step = request.get_json()

        current_date = datetime.now()
        day_stat = current_date.replace(hour=0,
                                        minute=0,
                                        second=0,
                                        microsecond=0)
        day_end = current_date.replace(hour=23,
                                       minute=59,
                                       second=59,
                                       microsecond=59)

        old_step = Step.query.filter(User.id == current_user.id,
                                     Step.timestamp >= day_stat,
                                     Step.timestamp <= day_end).first()

        if old_step is None:
            new_step = Step(timestamp=current_date,
                            value=step['value'],
                            user=current_user)
            new_step.save()
            return custom_response(200, "Step added", new_step.id)

        else:
            if old_step.value <= step['value']:
                old_step.value = step['value']
                old_step.update()
                return custom_response(200, "Step updated", old_step.id)
            else:
                return custom_response(200, "Step already updated",
                                       old_step.id)