示例#1
0
def __create_transactions(full_phone_number, request_data):
    '''
    Creates a transaction when passing through transaction information, including a date

    When the transaction is created, it is stored with a specific date. However, when it is
    retrieved, it is retrieved with all other transactions with the same month and year
    '''

    try:
        # Loads the request body
        # and checks whether all information is present
        if ('transactions' not in request_data):
            current_app.logger.error(
                'request body not formatted correctly, body is missing required parameters: {0}'
                .format(request_data))
            return error_response(400)

        # Gets the identity of the JWT
        # and gets the user from the DB
        author = User.query.filter(
            User.full_phone_number == full_phone_number).first()

        # Gets the list of transactions
        # And creates transactions for that User
        transactions_list = request_data['transactions']
        for item in transactions_list:
            # Makes sure that the data is formatted correctly
            if ('name' not in item or 'category' not in item
                    or 'price' not in item or 'createdAt' not in item):
                current_app.logger.error(
                    'transaction not formatted correctly, missing required parameters: {0}'
                    .format(item))
                return error_response(400)

            # Creates the new Transaction
            # and attaches the author to it
            transaction = Transaction()
            transaction.from_dict(item, author=author)

            # Logs that the user is being added to the database and then adds to the database
            # We will commit later once everything has been processed correctly
            db.session.add(transaction)
            current_app.logger.info(
                'added transaction {0} {1} to the database session'.format(
                    transaction.category, transaction.name))

        # Commits the user to the database and logs that is has been commited
        db.session.commit()
        current_app.logger.info(
            'commited transactions to the database session')

        # Returns the response with status code 201 to indicate the user has been created
        return error_response(201)
    except Exception as e:
        # Logs the exception that has been raised and rolls back all the changes made
        current_app.logger.fatal(str(e))
        db.session.rollback()
        # Returns a 500 response (Internal Server Error)
        return error_response(500)