def post(self): claims = get_jwt_claims() req_data = request.get_json() name = req_data.get('level_name') contents = req_data.get('contents') description = req_data.get('level_description') user = UserModel.find_by_id(claims['user_id']) if LevelModel.find_by_name(name): return {"message": " Level already exits."}, 400 level = LevelModel(name, description) if contents: for cont in contents: con_id = 0 con_weg = 0 te_id = 0 te_weg = 0 if 'content_id' in cont: con_id = cont['content_id'] con_weg = cont['weight'] #return{'cont': cont['content_id'], 'weg': cont['weight']} cont = ContentModel.find_by_id(con_id) level.add_contents(cont, con_weg) elif 'test_id' in cont: te_id = cont['test_id'] te_weg = cont['weight'] test = TestModel.find_by_id(te_id) level.add_tests(test, te_weg) level.owner.append(user) level.save_to_db() #return{'error':'There is an error!!!'}, 500 return { 'level_id': level.id, 'level_name': level.name, 'level_description': level.description }, 201
def put(self, id): req_data = request.get_json() level = LevelModel.find_by_id(id) contents = req_data.get('contents') if level: level.name = req_data.get('level_name') level.description = req_data.get('level_description') delete_test = TestInLevelModel.find_by_level_id(level.id) delete_content = ContentInLevelModel.find_by_level_id(level.id) for delete in delete_content: delete.delete_from_db() for delete in delete_test: delete.delete_from_db() for cont in contents: con_id = 0 con_weg = 0 te_id = 0 te_weg = 0 if 'content_id' in cont: con_id = cont['content_id'] con_weg = cont['weight'] #return{'cont': cont['content_id'], 'weg': cont['weight']} cont = ContentModel.find_by_id(con_id) level.add_contents(cont, con_weg) elif 'test_id' in cont: te_id = cont['test_id'] te_weg = cont['weight'] #return{'cont': cont['test_id'], 'weg': cont['weight']} test = TestModel.find_by_id(te_id) level.add_tests(test, te_weg) level.save_to_db() return {'level_name': level.name}, 200 return {'message': 'There is not such a level.'}, 404
def get(self, id): level = LevelModel.find_by_id(id) if level: contents = list(map(lambda x: x.json(), level.contents)) tests = list(map(lambda x: x.json(), level.tests)) test_weights = TestInLevelModel.find_by_level_id(level.id) content_weights = ContentInLevelModel.find_by_level_id(level.id) content_weight = list(map(lambda x: x.json(), content_weights)) test_weight = list(map(lambda x: x.json(), test_weights)) combine = [] for content in contents: for c_weight in content_weight: if content['content_id'] == c_weight['content_id']: content.update(c_weight) combine.append(content) for test in tests: for t_weight in test_weight: if test['test_id'] == t_weight['test_id']: test.update(t_weight) combine.append(test) #combine = sorted(combine, key=lambda x: x['weight']) return { 'level_name': level.name, 'level_description': level.description, 'contents': combine }, 200 return {'message': 'There is not such a level.'}, 404
def put(self, id): data = request.get_json() game = GameModel.find_by_id(id) if game: game.name = data.get('game_name') game.description = data.get('game_description') game.is_published = data.get('game_published') if data.get('levels'): level_del = LevelInGameModel.find_by_game_id(game.id) for delete in level_del: delete.delete_from_db() levels = data.get('levels') for level in levels: l_id = 0 l_weg = 0 if 'level_id' in level: l_id = level['level_id'] l_weg = level['weight'] #return{'level': l_id, 'weg': l_weg}, 200 find_level = LevelModel.find_by_id(l_id) game.add_levels(find_level, l_weg) game.save_to_db() return { 'game_name': game.name, 'levels': list(map(lambda x: x.json(), game.levels)) }, 200 return {'message': 'There is not such a game.'}, 404
def post(self): claims = get_jwt_claims() data = request.get_json() name = data.get('game_name') description = data.get('game_description') is_publish = False levels = data.get('levels') user = UserModel.find_by_id(claims['user_id']) if GameModel.find_by_name(name): return {"message": " Game already exits."}, 400 game = GameModel(name, description, is_publish) if levels: for level in levels: l_id = 0 l_weg = 0 if 'level_id' in level: l_id = level['level_id'] l_weg = level['weight'] #return{'level': l_id, 'weg': l_weg}, 200 find_level = LevelModel.find_by_id(l_id) game.add_levels(find_level, l_weg) game.owner.append(user) game.save_to_db() return { "message": "Game created successfully.", 'game_id': game.id, 'game_name': game.name }, 201
def get(self, _id): levels = LevelModel.find_by_id(_id) if levels: combine = list(map(lambda x: x.json(), levels.contents)) + list( map(lambda x: x.json(), levels.tests)) return {'contents': combine}, 200 return {'message': 'Level not found.'}, 404
def get(self, id): game = GameModel.find_by_id(id) if game: level_body = [] for levels in game.levels: app = {} level = LevelModel.find_by_id(levels.level_id) app['level'] = level.name app['weight'] = levels.weight level_body.append(app) level_body = sorted(level_body, key=lambda x: x['weight']) return {'game_name': game.name, 'levels': level_body} return {'message': 'There is not such a game.'}
def delete(self, id): level = LevelModel.find_by_id(id) if level: level.delete_from_db() return {'message': 'level deleted.'}, 200 return {'message': 'There is not such a level.'}, 404