Exemplo n.º 1
0
def populate_db():
    """
    Standaone utility to populate database from Ice And Fire API for testing
    :return: inserts records to database
    """

    api_url = BOOK_URL

    api_response = requests.get(api_url)
    print(api_response.text)
    json_api_response = api_response.json()
    if api_response.status_code == 200:
        for book in json_api_response:
            name = book['name']
            isbn = book["isbn"]
            authors = json.dumps(book["authors"])
            country = book['country']
            number_of_pages = int(book["numberOfPages"])
            publisher = book["publisher"]
            release_date = datetime.strptime(book["released"],
                                             "%Y-%m-%dT%H:%M:%S").date()

            book_insert = Books(None, name, isbn, authors, country,
                                number_of_pages, publisher, release_date,
                                datetime.now(), datetime.now())
            db.session.add(book_insert)
            db.session.commit()
Exemplo n.º 2
0
    def post(self):
        """Takes input in form of json
        @name: string
        @isbn: string
        @authors: list of string
        @country: string
        @number of pages: integer
        @publisher: string
        @release date: 'YYYY-MM-DD'
        :return: status code 201 on successful creation of a book record in database
        """
        self.parser = reqparse.RequestParser()
        self.parser.add_argument("name",
                                 type=non_empty_string,
                                 required=True,
                                 help='Name cannot be Empty',
                                 location='json')
        self.parser.add_argument('isbn',
                                 type=non_empty_string,
                                 required=True,
                                 help='ISBN cannot be Empty',
                                 location='json')
        self.parser.add_argument('authors',
                                 type=non_empty_list,
                                 required=True,
                                 help='Authors cannot be Empty',
                                 location='json')
        self.parser.add_argument('country',
                                 type=non_empty_string,
                                 required=True,
                                 help='Country cannot be Empty',
                                 location='json')
        self.parser.add_argument('number_of_pages',
                                 type=non_empty_integer,
                                 required=True,
                                 help='Number of pages cannot be Empty',
                                 location='json')
        self.parser.add_argument('publisher',
                                 type=non_empty_string,
                                 required=True,
                                 help='Publisher cannot be Empty',
                                 location='json',
                                 nullable=False)
        self.parser.add_argument('release_date',
                                 type=check_date_format,
                                 required=True,
                                 location='json')
        print('Parsing')
        request.get_json(force=True)
        args = self.parser.parse_args()
        name = args["name"]
        isbn = args["isbn"]
        authors = args["authors"]
        country = args["country"]
        number_of_pages = args["number_of_pages"]
        publisher = args["publisher"]
        release_date = args["release_date"]

        response = []
        response_dict = {}
        data_dict = {}
        book_dict = {}
        author_list = []
        book_dict["name"] = name
        book_dict["isbn"] = isbn
        book_dict['authors'] = authors
        book_dict["number_of_pages"] = number_of_pages
        book_dict["publiser"] = publisher
        book_dict["country"] = country
        book_dict["release_date"] = release_date
        data_dict["book"] = book_dict
        response_dict["data"] = data_dict
        response_dict["status_code"] = 201
        response_dict["status"] = "success"

        # Save to Db
        save_book = Books(None, name, isbn, authors, country, number_of_pages,
                          publisher, release_date, datetime.now(),
                          datetime.now())
        db.session.add(save_book)
        db.session.commit()
        return jsonify(response_dict)