def json(self, start_date, end_date): formatted_start_date = datetime.strptime(start_date, "%d-%m-%Y") formatted_end_date = datetime.strptime(end_date, "%d-%m-%Y") health = Health.query.filter( Health.health_date.between(formatted_start_date, formatted_end_date), Health.athlete_id == cherrypy.session.get('id')).order_by(asc(Health.health_date)).all() json_data = {"health":make_jsonable(health)} return json_data
def get_unlocked_achievements(self): unlocked_achievements = [] athlete = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() if athlete is not None: unlocked_achievements = UnlockedAchievement.query.filter_by( athlete_id=athlete.id ).all() return make_jsonable(unlocked_achievements)
def update_email(self, id, email): """update the email adress""" if re.match(r"[^@]+@[^@]+\.[^@]+", email): result = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() result.email = email response = make_jsonable(result) else: response = "Invalid email address" return response
def update_dob(self, id, birth_date): """update the date of birth""" try: time.strptime(birth_date, '%Y-%m-%d') except ValueError: #ath = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() #jathlete = make_jsonable(ath) #jathlete['birth_date'] = "Expected:(YYYY-MM-DD)" return "Expected:(YYYY-MM-DD)"#jathlete result = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() result.birth_date = birth_date return make_jsonable(result)
def test_make_jsonable_with_single_object_as_value(self): athlete = Athlete("user", "pass", "firstname", "lastname", "[email protected]") data = make_jsonable(athlete) expected = { 'achievements': [], 'first_name': 'firstname', 'last_name': 'lastname', 'about_me': '', 'avatar': '', 'address': '', 'birth_date': None, 'email': '[email protected]' } self.assertEqual(data, expected) self.assertEqual(data, athlete.to_dict())
def test_make_jsonable_with_list_of_objects_as_value(self): athlete1 = Athlete("user1", "pass", "firstname1", "lastname", "[email protected]") athlete2 = Athlete("user2", "pass", "firstname2", "lastname", "[email protected]") athlete3 = Athlete("user3", "pass", "firstname3", "lastname", "[email protected]") athlete_list = [athlete1, athlete2, athlete3] data = make_jsonable(athlete_list) expected = [ { 'achievements': [], 'first_name': 'firstname1', 'last_name': 'lastname', 'about_me': '', 'avatar': '', 'address': '', 'birth_date': None, 'email': '[email protected]' }, { 'achievements': [], 'first_name': 'firstname2', 'last_name': 'lastname', 'about_me': '', 'avatar': '', 'address': '', 'birth_date': None, 'email': '[email protected]' }, { 'achievements': [], 'first_name': 'firstname3', 'last_name': 'lastname', 'about_me': '', 'avatar': '', 'address': '', 'birth_date': None, 'email': '[email protected]' } ] self.assertEqual(data, expected) expected = [] expected.append(athlete1.to_dict()) expected.append(athlete2.to_dict()) expected.append(athlete3.to_dict()) self.assertEqual(data, expected)
def get_json(self, goal_id=None): return make_jsonable(self.get(goal_id))
def json(self, start_date, end_date): activities = Activity.query.filter(Activity.date.between(start_date, end_date), Activity.athlete_id == cherrypy.session.get('id')).all() json_data = {"activities":make_jsonable(activities)} return json_data
def get_achievements(self): achievements = Achievement.query.all() return make_jsonable(achievements)
def update_address(self, id, address): """update the address""" result = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() result.address = address return make_jsonable(result)
def update_about(self,id, about_msg): """updates the profiles about""" result = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() result.about_me = about_msg return make_jsonable(result)
def get_unlocked_achievements(self): unlocked_achievements = UnlockedAchievement.query.filter_by(athlete_id=cherrypy.session.get('id')).all() return make_jsonable(unlocked_achievements)
def athlete(self, **kwargs): result = Athlete.query.filter_by(id = cherrypy.session.get('id')).one() return make_jsonable(result)