def post(self, name): _entry = EntryModel.find_by_name(name) competition = CompetitionModel.find_by_name(name) if competition: if competition.isFinished: return {'message': "The competition '{}' is Finished! Can not accept more entries!".format(name)} competition_num_tries = competition.numTrys else: return {'message': "The competition '{}' doesen't exists.".format(name)} data = Entry.parser.parse_args() competition_id = CompetitionModel.find_competition_id(name) athlete_num_tries = EntryModel.find_athlete_tries(competition_id, data['atleta']) if _entry and athlete_num_tries and athlete_num_tries >= competition_num_tries: return {'message': "{} has reached the maximum number of attempts in {} competition.".format(data['atleta'], name)}, 500 competition = CompetitionModel.find_by_name(name) entry = EntryModel(data['atleta'], data['value'], data['unidade'], competition.id) try: entry.save_to_db() except: return {"message": "An error occurred inserting the entry."}, 500 return entry.json(), 201
def test_crud(self): with self.app_context(): competition = CompetitionModel('test', False, 1) competition.save_to_db() entry = EntryModel('test', 19.99, 'm', 1) self.assertIsNone(EntryModel.find_by_name('test').first(), "Found an entry with name 'test' before save_to_db") self.assertEqual(EntryModel.find_athlete_tries(1, 'test'), 0, "The number of tries for the athelte 'test' before save_to_db was not 0") entry.save_to_db() self.assertIsNotNone(EntryModel.find_by_name('test'), "Did not find an entry with name 'test' after save_to_db") self.assertEqual(EntryModel.find_athlete_tries(1, 'test'), 1, "The number of tries for the athelte 'test' after save_to_db was not 1") entry.delete_from_db() self.assertIsNone(EntryModel.find_by_name('test').first(), "Found an entry with name 'test' after delete_from_db")