def add_student(body, subject=None):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes
    :param subject: The subject name
    :type subject: str

    :rtype: int
    """
    if connexion.request.is_json:
        
        student_json = connexion.request.get_json()
        if 'first_name' not in student_json.keys() or student_json['first_name'] == '':
            return 'invalid input', 405

        if 'last_name' not in student_json.keys() or student_json['last_name'] == '':
            return 'invalid input', 405
        
        student = Student.from_dict(student_json)  # noqa: E501

        try:
            return swagger_server.service.student_service.add_student(student)
        except ValueError:
            return 'already exists', 409
def get_student_by_nuid(nuid):  # noqa: E501
    """Get student by nuid

     # noqa: E501

    :param nuid: The name that needs to be fetched. Use student1 for testing.
    :type nuid: int

    :rtype: Student
    """
    select_string = """
        SELECT
            name,
            email,
            nuid
        FROM
            student
        WHERE
            nuid = {}
        """.format(nuid)
    try:
        db_conn = connexion.DB(connexion.DB_ENG)
        result = db_conn.execute(select_string)
        db_conn.close()
        for row in result:
            res = {
                'name': row["name"],
                'nuid': row["nuid"],
                'email': row["email"]
            }
            res = Student.from_dict(res)
            return res, 200
        return "Object not found", 404
    except exc.IntegrityError:
        return "Internal Server Error", 500
def update_student(body):  # noqa: E501
    """Updated student

    This can only be done by the logged in student. # noqa: E501

    :param nuid: nuid that need to be updated
    :type nuid: int
    :param body: Updated student object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        update_string = """
            UPDATE student
            SET pass = "******"
            WHERE nuid = {0};
            """.format(body.nuid, body.password)
        try:
            session_cookie = connexion.request.cookies.get("session")
            session_NUID = connexion.JWT_verify(session_cookie)
            db_conn = connexion.DB(connexion.DB_ENG)
            db_conn.execute(update_string)
            db_conn.close()
            return "Accepted", 201
        except exc.IntegrityError:
            return "Already Exists", 202
        except KeyError:
            return "Forbidden", 403
    return "Bad Request", 400
Ejemplo n.º 4
0
def add_student(body):  # noqa: E501
    """Add a new student
     # noqa: E501
    :param body: Student object that needs to be added
    :type body: dict | bytes
    :rtype: str
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
    return student_service.add_student(body)
Ejemplo n.º 5
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        if not body or not body.last_name or not body.first_name:
            return 'Invalid input', 405
    return student_service.add_student(body)
Ejemplo n.º 6
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        return student_service.add_student(body)
    else:
        return "Your request must include JSON, with at least one of the following: 'first_name', 'last_name'", 200
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        try:
            body = Student.from_dict(
                connexion.request.get_json())  # noqa: E501
        except ValueError:
            return 'Invalid Input', 405
    return student_service.add_student(body)
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: int
    """
    if connexion.request.is_json:
        student = Student.from_dict(connexion.request.get_json())  # noqa: E501
        # print(student)
        result = student_service.add_student(student)
        return result

    return 'input no bueono', 400
Ejemplo n.º 9
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: int
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        # print('default controller body', body)
        if (not body.first_name) or (not body.last_name):
            return "Not content", 405
    # return 'do some magic!'
    return student_service.add_student(body)
    def test_delete_student(self, mock_delete_student):
        """
        Test case for delete_student
        """

        mock_student = Student(789, "first_name", "last_name", grades={})

        mock_delete_student.return_value = mock_student

        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=789),
            method='DELETE')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        response_student = Student.from_dict(json.loads(response.data))

        self.assertEqual(response_student, mock_student)
Ejemplo n.º 11
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: int
    """
    # try:
    #     res = connexion.request.is_json.get_json()
    #     if "first_name" not in res.keys() or "last_name" not in res.keys():
    #         return 'Full name required', 405
    # except ValueError:
    #     'invalid entry', 405

    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json()) # noqa: E501
        if body.first_name == None or body.last_name == None:
            return 'Full name required', 405
    return student_service.add_student(body)
def create_student(body):  # noqa: E501
    """Create student

    This can only be done by the logged in student. # noqa: E501

    :param body: Created student object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        insert_string = """
            INSERT INTO student (name, email, nuid, pass)
            VALUES ("{0}", "{1}", {2}, "{3}");
            """.format(body.name, body.email, body.nuid, body.password)
        try:
            db_conn = connexion.DB(connexion.DB_ENG)
            db_conn.execute(insert_string)
            db_conn.close()
            return "Accepted", 201
        except exc.IntegrityError:
            return "User for that NUID and/or email already exists", 404
    return "Bad Request", 400