Esempio n. 1
0
def seed_db():
    """Seeds the database."""
    db.session.add(
        Exercise(
            body=(
                "Define a function called sum that takes two integers as "
                "arguments and returns their sum."
            ),
            test_code="print(sum(2, 3))",
            test_code_solution="5",
        )
    )
    db.session.add(
        Exercise(
            body=(
                "Define a function called reverse that takes a string as "
                "an argument and returns the string in reversed order."
            ),
            test_code="print(reverse(racecar))",
            test_code_solution="racecar",
        )
    )
    db.session.add(
        Exercise(
            body=(
                "Define a function called factorial that takes a random number "
                "as an argument and then returns the factorial of that given "
                "number."
            ),
            test_code="print(factorial(5))",
            test_code_solution="120",
        )
    )
    db.session.commit()
Esempio n. 2
0
def seed_db():
    """Seeds the database."""
    db.session.add(
        Exercise(
            body=('Define a function called sum that takes two integers as '
                  'arguments and returns their sum.'),
            test_code='sum(2, 3)',
            test_code_solution='5'))
    db.session.add(
        Exercise(
            body=('Define a function called reverse that takes a string as '
                  'an argument and returns the string in reversed order.'),
            test_code='reverse("racecar")',
            test_code_solution='racecar'))
    db.session.commit()
Esempio n. 3
0
    def post(self, resp):
        """Save a new exercise"""
        post_data = request.get_json()
        response_object = {'status': 'fail', 'message': 'Invalid payload.'}
        #check authorization
        if not ensure_authenticated(resp):
            response_object['message'] = \
                'You do not have permission to do that.'
            return response_object, 401
        #check for valid json body
        post_data = request.get_json()
        if not post_data:
            return response_object, 400

        # init params
        body = post_data.get('body')
        test_code = post_data.get('test_code')
        test_code_solution = post_data.get('test_code_solution')

        # do db stuff
        try:
            db.session.add(
                Exercise(body=body,
                         test_code=test_code,
                         test_code_solution=test_code_solution))
            db.session.commit()
            response_object['status'] = 'success'
            response_object['message'] = 'New exercise was added!'
            return response_object, 201
        except exc.IntegrityError:
            db.session.rollback()
            return response_object, 400
        except (exc.IntegrityError, ValueError):
            db.session.rollback()
            return response_object, 400
Esempio n. 4
0
 def post(self, resp):
     """Add exercise"""
     if not resp['admin']:
         response_object = {
             'status': 'error',
             'message': 'You do not have permission to do that.'
         }
         return response_object, 401
     post_data = request.get_json()
     if not post_data:
         response_object = {'status': 'fail', 'message': 'Invalid payload.'}
         return response_object, 400
     body = post_data.get('body')
     test_code = post_data.get('test_code')
     test_code_solution = post_data.get('test_code_solution')
     try:
         db.session.add(
             Exercise(body=body,
                      test_code=test_code,
                      test_code_solution=test_code_solution))
         db.session.commit()
         response_object = {
             'status': 'success',
             'message': 'New exercise was added!'
         }
         return response_object, 201
     except (exc.IntegrityError, ValueError):
         db.session().rollback()
         response_object = {'status': 'fail', 'message': 'Invalid payload.'}
         return response_object, 400
Esempio n. 5
0
 def post(self, resp):
     """Add exercise"""
     if not resp["admin"]:
         response_object = {
             "status": "error",
             "message": "You do not have permission to do that.",
         }
         return response_object, 401
     post_data = request.get_json()
     if not post_data:
         response_object = {"status": "fail", "message": "Invalid payload."}
         return response_object, 400
     body = post_data.get("body")
     test_code = post_data.get("test_code")
     test_code_solution = post_data.get("test_code_solution")
     try:
         db.session.add(
             Exercise(
                 body=body,
                 test_code=test_code,
                 test_code_solution=test_code_solution,
             ))
         db.session.commit()
         response_object = {
             "status": "success",
             "message": "New exercise was added!",
         }
         return response_object, 201
     except (exc.IntegrityError, ValueError):
         db.session().rollback()
         response_object = {"status": "fail", "message": "Invalid payload."}
         return response_object, 400
Esempio n. 6
0
def add_exercises(resp):
    post_data = request.get_json()
    response_object = {'status': 'fail', 'message': 'Invalid payload.'}
    if not resp['admin']:
        response_object['message'] = 'You do not have permission to do that.'
        return jsonify(response_object), 401
    if not post_data:
        return jsonify(response_object), 400
    body = post_data.get('body')
    test_code = post_data.get('test_code')
    test_code_solution = post_data.get('test_code_solution')
    try:
        db.session.add(
            Exercise(body=body,
                     test_code=test_code,
                     test_code_solution=test_code_solution))
        db.session.commit()
        response_object['status'] = 'success'
        response_object['message'] = 'New exercise was added!'
        return jsonify(response_object), 201
    except exc.IntegrityError as e:
        db.session.rollback()
        return jsonify(response_object), 400
    except (exc.IntegrityError, ValueError) as e:
        db.session.rollback()
        return jsonify(response_object), 400
Esempio n. 7
0
def add_exercise(body, test_code, test_code_solution):
    exercise = Exercise(body=body,
                        test_code=test_code,
                        test_code_solution=test_code_solution)
    db.session.add(exercise)
    db.session.commit()

    return exercise
def seed_db():
    """Seeds the database."""

    exercise = Exercise(
        'Define a function called sum that takes two\
             integers as arguments and returns their sum.', 'sum(2,2)', '4')

    db.session.add(exercise)
    db.session.commit()
Esempio n. 9
0
def add_exercise(
        body=('Define a function called sum that takes two integers as '
              'arguments and returns their sum'),
        test_code='sum(2, 2)',
        test_code_solution='4'):
    exercise = Exercise(body=body,
                        test_code=test_code,
                        test_code_solution=test_code_solution)
    db.session.add(exercise)
    db.session.commit()
    return exercise
Esempio n. 10
0
def seed_db():
    db.session.add(Exercise(
        body=('Define a function called sum that takes two integers as arguments and returns their sum.'),
        test_code='sum(2, 3)',
        test_code_solution='5'
    ))
    db.session.add(Exercise(
        body=('Define a function called reverse that takes a string as '
              'an argument and returns the string in reversed order.'),
        test_code='reverse("racecar")',
        test_code_solution='racecar'
    ))
    db.session.add(Exercise(
        body=('Define a function called factorial that takes a random number '
              'as an argument and then returns the factorial of that given '
              'number.'),
        test_code='factorial(5)',
        test_code_solution='120'
    ))
    db.session.commit()
Esempio n. 11
0
def add_exercise(
    body="Define a function called sum that takes two integers as "
    "arguments and returns their sum",
    test_code="sum(2, 2)",
    test_code_solution="4",
):
    exercise = Exercise(body=body,
                        test_code=test_code,
                        test_code_solution=test_code_solution)
    db.session.add(exercise)
    db.session.commit()
    return exercise
Esempio n. 12
0
    def test_add_exercise(self):

        exercise = Exercise(
            'Define a function called sum that takes two\
             integers as arguments and returns their sum.', 'sum(2,2)', '4')

        db.session.add(exercise)
        db.session.commit()

        self.assertTrue(exercise.id)
        self.assertEquals(
            exercise.body, 'Define a function called sum that takes two\
             integers as arguments and returns their sum.')

        self.assertEquals(exercise.test_code, 'sum(2,2)')
        self.assertEquals(exercise.test_code_solution, '4')
Esempio n. 13
0
def seed_db():
    """Seeds the database."""
    db.session.add(
        Exercise(
            body=('Define a function called sum that takes two integers as '
                  'arguments and returns their sum.'),
            test_code='sum(2, 3)',
            test_code_solution='5'))
    db.session.add(
        Exercise(body=(
            'Define a function called sum_list that takes a list of numbers '
            'as an argument and returns their sum.'),
                 test_code='sum_list([1, 2, 3, 4, 5])',
                 test_code_solution='15'))
    db.session.add(
        Exercise(body=(
            'Define a function called doubleletters that takes a string as '
            'an argument and returns the string with two of each letter.'),
                 test_code='doubleletters("Hello")',
                 test_code_solution='HHeelllloo'))
    db.session.add(
        Exercise(
            body=('Define a function called slicer that takes a string as '
                  'an argument and returns the first three characters.'),
            test_code='slicer(\'abcdefgh\')',
            test_code_solution='abc'))
    db.session.add(
        Exercise(
            body=('Define a function called dicer that takes a string as '
                  'an argument and returns the third and fifth characters.'),
            test_code='dicer(\'abcdefgh\')',
            test_code_solution='ce'))
    db.session.add(
        Exercise(body=(
            'Define a function called factorial that takes a random number '
            'as an argument and returns the factorial of that given number.'),
                 test_code='factorial(5)',
                 test_code_solution='120'))
    db.session.add(
        Exercise(body=(
            'Define a function called isPrime that takes a random number '
            'as an argument and returns whether or not it is a prime number.'),
                 test_code='isPrime(5)',
                 test_code_solution='True'))
    db.session.add(
        Exercise(body=(
            'Define a function called dectohex that takes a random number '
            'as an argument and returns the hex value of that number.'),
                 test_code='dectohex(32)',
                 test_code_solution='0x20'))
    db.session.add(
        Exercise(body=(
            'Define a function called squared that takes a list of numbers '
            'as an argument and returns a list of each number squared.'),
                 test_code='squared([1, 2, 3, 4, 5])',
                 test_code_solution='[1, 4, 9, 16, 25]'))
    db.session.add(
        Exercise(body=(
            'Define a function called evens that takes a list of numbers '
            'as an argument and returns a list containing only the even numbers.'
        ),
                 test_code='evens([1, 2, 3, 4, 5, 6])',
                 test_code_solution='[2, 4, 6]'))
    db.session.commit()