Beispiel #1
0
    def post(self):

        data = UserParser.parser.parse_args()
        data['semester'] = StudentRegister.student_parser.parse_args(
        )['semester']

        # ensure username is proper
        if not data['username'][0].isalpha():
            return {"message": f"Invalid username."}, 400

        if UserModel.find_by_username(data['username']):
            return {
                "message":
                f"User with username: {data['username']} already exists."
            }, 400

        if UserModel.find_by_code(data['code']):
            return {
                "message": f"User with code: {data['code']} already exists."
            }, 400

        if UserModel.find_by_email(data['email']):
            return {
                "message": f"User with email: {data['email']} already exists."
            }, 400

        temp_pass = bcrypt.generate_password_hash(data['password'],
                                                  10).decode('UTF-8')
        data['password'] = temp_pass

        user = StudentModel(**data)  # unpacking the dictionary
        user.save_to_db()

        return {"message": f"Student was created successfully."}, 201
Beispiel #2
0
    def put(self, code):

        # This parser will be used to update fields that can me modifiable
        parser = reqparse.RequestParser()
        parser.add_argument('username', type=str, required=False)
        parser.add_argument('first_name', type=str, required=False)
        parser.add_argument('last_name', type=str, required=False)
        parser.add_argument('description', type=str, required=False)
        parser.add_argument('picture', type=str, required=False)

        student = StudentModel.find_by_code(code)
        data = parser.parse_args()

        if not data['username'][0].isalpha():
            return {'message': 'Invalid username.'}, 400

        if student is None:
            # If the student isn't found, create a new student using the User parser

            # Creates a copy of the User parser and removes the 'code' argument so that
            #   it is no longer necessary in the request body
            put_parser = copy.copy(UserParser.parser)
            put_parser.remove_argument('code')

            data1 = put_parser.parse_args()

            # add 'code' and 'semester' to the data1 dictionary so that it can unpacked properly
            data1['code'] = code
            data1['semester'] = StudentRegister.student_parser.parse_args(
            )['semester']

            admin = StudentModel(**data1)
            admin.save_to_db()
            return admin.json(), 201

        student.username = data['username']
        student.first_name = data['first_name']
        student.last_name = data['last_name']
        student.description = data['description']
        student.picture = data['picture']

        student.save_to_db()
        return student.json(), 200