Example #1
0
    def post(self):
        data = _user_parser.parse_args()

        if UserModel.find_by_email(data['email']):
            return {"message": "A user with that email already exits."}, 400

        user = UserModel(data['user_name'], data['email'],
                         data['password'], data['auth_level'])
        user.save_to_db()

        return {"message": "Student created successfully."}, 201
Example #2
0
 def get(self):
     claims = get_jwt_claims()
     user = UserModel.find_by_id(claims['user_id'])
     if user:
         return{
             'contents': list(map(lambda x: x.json(), user.query.filter_by(id=user.id).first().contents))}, 200
     return {'message': 'User not found.'}, 404
Example #3
0
    def post(self):
        claims = get_jwt_claims()
        req_data = request.get_json()
        code = req_data.get('code')
        subject = req_data.get('subject_id')
        question = req_data.get('question')
        answers = req_data.get('options')
        user = UserModel.find_by_id(claims['user_id'])

        if QuestionModel.find_by_code(code):
            return {'message': 'Question code is already exists.'}, 400
        question_add = QuestionModel(code, question, subject)
        question_add.owner.append(user)
        for opt in answers:
            answer = ''
            is_true = False
            for add in opt:
                if 'answer' == add:
                    answer = opt[add]
                elif 'is_true' == add:
                    is_true = opt[add]
            save_answer = OptionModel(question=question_add,
                                      answer=answer,
                                      is_answer=is_true)
            question_add.options.append(save_answer)
            question_add.save_to_db()
            #save_answer.save_to_db()
        return {
            'question_id': question_add.id,
            'code': question_add.code,
            'subject_id': question_add.subject_id
        }, 201
Example #4
0
 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
Example #5
0
 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
Example #6
0
 def delete(self, id):
     claims = get_jwt_claims()
     if claims['role'] != 'admin':
         return {'message': 'You dont have authority to delete.'}, 401
     users = UserModel.find_by_id(id)
     if users:
         users.delete_from_db()
         return {'message': 'User deleted.'}
     return {'message': 'User not found.'}, 404
Example #7
0
 def post(self):
     claims = get_jwt_claims()
     req_data = request.get_json()
     get_user = UserModel.find_by_id(claims['user_id'])
     get_game = GameModel.find_by_id(req_data.get('game_id'))
     if get_user and get_game:
         get_game.subscribe.append(get_user)
         get_game.save_to_db()
         return {'message': 'Subscribed to ' + get_game.name}
     return {'message': 'Game or User not found.'}
Example #8
0
 def delete(self, id):
     game = GameModel.find_by_id(id)
     users = game.query.filter_by(id=game.id).first().owner
     if game:
         if users:
             for user in users:
                 rm = UserModel.find_by_id(user.id)
                 game.owner.remove(rm)
                 game.save_to_db()
             game.delete_from_db()
             return {'message': game.name + ' is deleted.'}, 200
     return {'message': 'There is not such a game.'}
Example #9
0
 def post(self):
     claims = get_jwt_claims()
     data = ContentPost.parser.parse_args()
     content = ContentModel.find_by_name(data['content_name'])
     subject = SubjectModel.find_by_subject_id(data['subject_id'])
     user = UserModel.find_by_id(claims['user_id'])
     if content:
         return {'message': 'Content is already exists.'}, 400
     else:
         content = ContentModel(data['content_name'], data['content'])
         content.subject.append(subject)
         content.owner.append(user)
         content.save_to_db()
         return {'content_id': content.id, 'content_name': content.name}, 201
Example #10
0
 def post(self):
     claims = get_jwt_claims()
     req_data = request.get_json()
     questions = req_data.get('questions')
     test = req_data.get('test_name')
     user = UserModel.find_by_id(claims['user_id'])
     if TestModel.find_by_name(test):
         return {'message': 'Test is already exists.'}, 400
     name = TestModel(test)
     if questions:
         for quest in questions:
             for q in quest:
                 question = QuestionModel.find_by_id(quest[q])
                 name.question.append(question)
     name.owner.append(user)
     name.save_to_db()
     return {'test_id': name.id, 'test_name': name.name}, 201
Example #11
0
    def put(self, id):
        claims = get_jwt_claims()
        data = _user_parser.parse_args()
        user = UserModel.find_by_id(id)
        if user or claims['role'] == 'admin':
            user.username = data['user_name']
            user.email = data['email']
            user.auth_level = data['auth_level']
            if data['password']:
                user.password = data['password']
        else:
            user = UserModel(id, **data)

        user.save_to_db()
        return user.json()
Example #12
0
    def post(cls):
        login_parser = reqparse.RequestParser()
        login_parser.add_argument('email',
                    type=str,
                    required=True,
                    help="This field cannot be blank."
                    )
        login_parser.add_argument('password',
                    type=str,
                    required=True,
                    help="This field cannot be blank."
                    )
        data = login_parser.parse_args()

        user= UserModel.find_by_email(data['email'])
        if user and safe_str_cmp(user.password, data['password']):
            access_token = create_access_token(identity=user.json(),
            fresh=True)
            refresh_token = create_refresh_token(user.json())
            return {
                'access_token' : access_token,
                'refresh_token' : refresh_token
            }, 200
        return {'message': 'Invalid credentials'}, 401
Example #13
0
 def get(self, id):
     user = UserModel.find_by_id(id)
     if user:
         return user.json()
     return {'message': 'User not found'}, 404