Exemple #1
0
 def get_books(self):
     book_obj_lst = UserBooks.get_books(user_id=self.user_id)
     bmap = {b.get("book_id"): b for b in book_obj_lst}
     books = Books.get_list(ids=[b.get("book_id") for b in book_obj_lst])
     res = []
     for b in books:
         b.__dict__["reading_state"] = bmap[
             b.__dict__["book_id"]]["reading_state"]
         res.append(b.__dict__)
     return res
Exemple #2
0
def index(page):
    pagination = Books.get_books_pagination(
        page=page,
        per_page=current_app.config['BOOKS_PER_PAGE'],
        db_filter=True,
        order=Books.timestamp.desc())
    books = pagination.items
    return render_template('main/index.html',
                           books=books,
                           pagination=pagination,
                           title="Home")
Exemple #3
0
def upload():
    for file in request.files.getlist('btn-upload'):
        if not uploads.extension_allowed(extension(file.filename)):
            flash(
                Markup(
                    'File extension "%s" is not allowed to be uploaded to this server'
                    % extension(file.filename)), "warning")
            return redirect(url_for('main.index'))

        # save file
        file_name, file_extension = os.path.splitext(file.filename)
        folder = os.path.join(current_user.email, file_name)
        file_path = helper.resolve_folder_conflict(
            current_app.config['UPLOAD_FOLDER'], folder)
        saved_file_name = uploads.save(file,
                                       folder=file_path,
                                       name=file.filename)

        # get book meta
        meta = book_format.process(uploads.path(saved_file_name), file_path,
                                   file_name, file_extension[1:])

        # move file
        if meta.title != meta.file_name:
            new_file_path = os.path.join(current_app.config['UPLOAD_FOLDER'],
                                         file_path,
                                         meta.title + file_extension)
            move(uploads.path(saved_file_name), new_file_path)
            if meta.cover_path:
                new_cover_path = os.path.join(
                    current_app.config['UPLOAD_FOLDER'], file_path,
                    meta.title + '.jpg')
                move(meta.cover_path, new_cover_path)

        # add book
        Books.add_book(meta)
    flash(Markup('File upload completed.'), "info")
    return redirect(url_for('main.index'))
Exemple #4
0
def delete_book(book_id):
    Books.delete_book(book_id)
    flash(Markup('Book successfully deleted'), "info")
    return redirect(url_for('main.index'))
Exemple #5
0
def explore():
    page = request.args.get("page", 1, type=int)
    books = Books.get_paginated(page=page)
    return render_template("pages/home.html",
                           form=SignupForm(request.form),
                           books=books)
Exemple #6
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)