def get_current_check_in(id): """Get the current check-in for a given goal. Returns 404 if the current check-in or goal does not exist""" goal = Goal.pull_by_id(id) if (not goal): raise NotFoundError() if goal.user != current_user.get_id(): raise UnauthorizedError check_in = CheckIn.pull_by_goal_timeframe(id, Timeframe.get_current_timeframe(goal.check_in_frequency_name).get_id()) if (not check_in): raise NotFoundError() return check_in.to_json(), 200
def get_check_in(id, tfid): """Get the check-in for a given timeframe. Returns 404 if there is no check-in for that timeframe""" goal = Goal.pull_by_id(id) if (not goal): raise NotFoundError() if goal.user != current_user.get_id(): raise UnauthorizedError check_in = CheckIn.pull_by_goal_timeframe(id, tfid) if (not check_in): raise NotFoundError() return check_in.to_json(), 200
def get_goal(id): goal = Goal.pull_by_id(id) if (not goal): raise NotFoundError() if (not goal.public and goal.user != current_user.get_id()): raise UnauthorizedError() return goal.to_json(), HTTP_200_OK
def get_goals_by_user(id): """Show all public goals for a given user""" user = User.pull_by_id(id) if (not user): raise NotFoundError() validate_sort(request.args, ["id", "created", "name", "frequency"]) goals = Goal.pull_by_user(id, request.args, True) return jsonify({"goals": [x.to_dict() for x in goals]}), HTTP_200_OK
def update_goal(): validate_form(request.form, ['id']) goal = Goal.pull_by_id(request.form['id']) if (not goal): raise NotFoundError() if (goal.user != current_user.get_id()): raise UnauthorizedError() goal.update(request.form) return empty_ok()
def check_in(id): validate_form(request.form, ["value"]) goal = Goal.pull_by_id(id) if (not goal): raise NotFoundError() if goal.user != current_user.get_id(): raise UnauthorizedError if ('timeframe' in request.form): timeframe = Timeframe.pull_by_id(request.form['timeframe']) else: timeframe = Timeframe.get_current_timeframe(goal.check_in_frequency_name) check_in = CheckIn(goal, timeframe, request.form['value']) return_code = 200 if check_in.exists() else 201 check_in.persist() return check_in.to_json(), return_code
def get_by_time(id): goal = Goal.pull_by_id(id) if (not goal): raise NotFoundError() if goal.user != current_user.get_id(): raise UnauthorizedError if (not 'start' in request.args or not 'end' in request.args): raise InvalidRequest(["start and end are required parameters"]) try: start = datetime.datetime.strptime(request.args['start'], "%Y-%m-%d %H:%M:%S") end = datetime.datetime.strptime(request.args['end'], "%Y-%m-%d %H:%M:%S") except ValueError as e: raise InvalidRequest([e.message]) check_ins = CheckIn.pull_by_goal_start_end(id, start, end) return jsonify({"check-ins": [check_in.to_dict() for check_in in check_ins]}), 200
def get_user(id): user = User.pull_by_id(id) if (not user): raise NotFoundError() return user.to_json(), HTTP_200_OK