コード例 #1
0
    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
コード例 #2
0
    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")
コード例 #3
0
ファイル: entry.py プロジェクト: robsoza/cohort4-group
    def post(self, name):
        if EntryModel.find_by_name(name):
            return {
                'message':
                "A store with name '{}' already exists.".format(name)
            }, 400

        store = StoreModel(name)
        try:
            store.save_to_db()
        except:
            return {
                'message': 'An error occurred while creating the store.'
            }, 500

        return store.json(), 201
コード例 #4
0
    def delete(self, name):
        item = EntryModel.find_by_name(name).first()
        if item:
          item.delete_from_db()

        return {'message': 'Entry deleted'}, 200
コード例 #5
0
 def get(self, name):
   entry = EntryModel.find_by_name(name)
   if entry:
     return {name: [_entry.json() for _entry in entry]}
   return{'message': 'Entry not found'}, 404