Exemplo n.º 1
0
def add_book():

    #Add a vairable that changes the output of the book page
    #If it is true it is for adding a book
    #If it is false it for editing a book
    add_book = True

    #Add a form
    form = Book_PostForm()
    if form.validate_on_submit():
        book = Books(book=form.book.data,
                     author=form.author.data,
                     description=form.description.data,
                     rating=form.rating.data)
        #Add the information if it is a new book
        #Returns to the book page if the addition is successful
        try:
            db.session.add(book)
            db.session.commit()
            flash('You have successfully added a new book')
        except:
            flash('Error: The book already exists')
        return redirect(url_for('books'))

    return render_template('book.html',
                           action="Add",
                           title='Add Book',
                           form=form,
                           add_book=add_book)
Exemplo n.º 2
0
 def test_books_model(self):
     effectiveExecutive = Books(book="The Effective Executive",
                                author="Peter Drucker",
                                description="A book about business",
                                rating="5")
     db.session.add(effectiveExecutive)
     db.session.commit()
     self.assertEqual(Books.query.count(), 3)
Exemplo n.º 3
0
    def setUp(self):

        #Called before every test
        db.session.commit()
        db.drop_all()
        db.create_all()

        #Create test admin user
        admin = Users(first_name="admin",
                      last_name="admin",
                      email="*****@*****.**",
                      password="******")
        employee = Users(first_name="test",
                         last_name="user",
                         email="*****@*****.**",
                         password="******")
        BenjaminFranklin = Books(
            book="An American Life: Benjamin Franklin",
            author="Walter Isaacson",
            description="It is a biography of Benjamin Franklin",
            rating="5")
        ZeroToOne = Books(book="Zero To One",
                          author="Peter Thiel",
                          description="It stipulates business theory",
                          rating="5")
        ShuaibReview = Reviews(review_author="Shuaib",
                               review="Best book ever",
                               rating="5",
                               book_id="2")
        ThomasReview = Reviews(review_author="Thomas",
                               review="Interesting book",
                               rating="5",
                               book_id="2")

        #Save/Add users to the databse
        db.session.add(admin)
        db.session.add(employee)

        #Save/Add book to database
        db.session.add(BenjaminFranklin)
        db.session.add(ZeroToOne)

        #Save/Add reviews to database
        db.session.add(ShuaibReview)
        db.session.add(ThomasReview)
        db.session.commit()
def update_view(slug):
    book = Books.objects(slug=slug).first()
    book = book.update(
        book_name=book.book_name,
        author=book.author,
        user_id=book.user_id,
        image_url=book.image_url,
        slug=book.slug,
        ISBN=book.ISBN,
        category=book.category,
        view=book.view + 1,
        overview=book.overview,
        date=book.date,
    )
    return book
Exemplo n.º 5
0
def post():
    form = BookForm()
    if form.validate_on_submit():
        postData = Books(book_name=form.book_name.data,
                         author_name=form.author_name.data,
                         genre=form.genre.data,
                         short_content=form.short_content.data)

        db.session.add(postData)
        db.session.commit()

        return redirect(url_for('book'))

    else:
        print(form.errors)

    return render_template('post.html', title='Post', form=form)
 def get_user_books():
     return Books.objects(user_id=session.get('user_id')).order_by('-date')
 def get_single_book(slug):
     return Books.objects(slug=slug)
 def get_category_books(slug):
     return Books.objects(category=Slug.getTitle(slug))
 def get_most_viewed_books():
     return Books.objects().order_by('-view')
 def get_recent_books():
     return Books.objects().order_by('-date')
def account(term='login', slug=''):
    page_data = PageData
    if page_data.check_login():
        active_user = Users.objects(user_id=session.get('user_id')).first()
        books = Books.objects(user_id=session.get('user_id')).order_by('-date')
        user = page_data.get_user_details(session.get('user_id'))
        # this root for user profile
        if term == 'profile':
            return render_template('account/profile.html',
                                   user=user,
                                   page_data=page_data)
        # this root for user edit profile information like name and email
        elif term == 'edit_profile':
            form = EditUser()
            if form.validate_on_submit():
                name = form.name.data
                email = form.email.data
                if user.email == email:
                    user = Users.objects(
                        user_id=session.get('user_id')).update(name=name,
                                                               email=email)
                    flash('You are successfully updated your profile!',
                          'success')
                    return redirect(url_for('account') + '/edit_profile')
                else:
                    count = Users.objects(email=email).count()
                    if count > 0:
                        flash(
                            'This email already existed! Please try another one.',
                            'danger')
                        return redirect(url_for('account') + '/edit_profile')
                    else:
                        user = Users.objects(
                            user_id=session.get('user_id')).update(name=name,
                                                                   email=email)
                        flash('You are successfully update your profile!',
                              'success')
                        return redirect(url_for('account') + '/edit_profile')
            else:
                return render_template('account/edit_profile.html',
                                       page_data=page_data,
                                       user=user,
                                       form=form)
        # this root for user uploaded all books
        elif term == 'books':
            return render_template('account/my_books.html',
                                   slug_ob=Slug,
                                   user=user,
                                   page_data=page_data)
        # this root for user single book view
        elif term == 'view':
            book = page_data.get_single_book(slug).first()
            return render_template('account/view.html',
                                   helper=Helper,
                                   form=DeleteBook(),
                                   user=user,
                                   page_data=page_data,
                                   single_book=book)
        # this root for user can upload new book
        elif term == 'add_new_book':
            form = AddNewBook()
            if form.validate_on_submit():

                # form upload image file details

                form_file_upload = request.files['upload']
                upload_file_name = str(active_user.user_id) + '_' \
                    + str(random.randint(10, 200)) + '_' \
                    + secure_filename(form_file_upload.filename)
                form_file_upload.save('application/static/uploads/2020/' +
                                      upload_file_name)

                book_name = form.book_name.data
                author = form.author.data
                user_id = user.user_id
                image_url = upload_file_name
                slug = Slug.getSlug(form.book_name.data)
                ISBN = form.ISBN.data
                category = form.category.data
                overview = form.overview.data
                date = datetime.now()

                book = Books(
                    book_name=book_name,
                    author=author,
                    user_id=user_id,
                    image_url=image_url,
                    slug=slug,
                    ISBN=ISBN,
                    category=category,
                    overview=overview,
                    date=date,
                )
                book.save()
                flash('You are successfully added a new book!', 'success')
                return redirect(url_for('account') + '/books')
            else:

                return render_template('account/add_new_book.html',
                                       page_data=page_data,
                                       form=form,
                                       user=user)
        # this root for user can edit single bokk information
        elif term == 'edit_book':

            book = page_data.get_single_book(slug).first()
            form = EditBook(request.form, category=book.category)
            print(book)
            if slug:
                if form.validate_on_submit():
                    image_url_link = book.image_url
                    if request.files['upload']:
                        form_file_upload = request.files['upload']
                        image_url_link      = str(user.user_id) + '_' + str(random.randint(10, 200)) + '_' \
                            + secure_filename(form_file_upload.filename)
                        if book.image_url:
                            os.remove('application/static/uploads/2020/' +
                                      book.image_url)
                        form_file_upload.save(
                            'application/static/uploads/2020/' +
                            image_url_link)

                    book_name = form.book_name.data
                    author = form.author.data
                    user_id = user.user_id
                    image_url = image_url_link
                    slug = book.slug
                    ISBN = form.ISBN.data
                    category = form.category.data
                    overview = form.overview.data
                    date = datetime.now()
                    update_book = book.update(
                        book_name=book_name,
                        author=author,
                        user_id=user_id,
                        image_url=image_url,
                        slug=slug,
                        ISBN=ISBN,
                        category=category,
                        overview=overview,
                        date=date,
                    )
                    flash('You are successfully update your book!', 'success')
                    return redirect(
                        url_for('account') + 'edit_book/' + book.slug)
                else:
                    return render_template('account/edit_book.html',
                                           page_data=page_data,
                                           user=user,
                                           form=form,
                                           single_book=book)
            else:
                redirect(url_for('account') + 'mybooks')
        # this root for user can delete book information and image as well form database and server
        elif term == 'delete_book':
            form = DeleteBook()
            if slug:
                book = Books.objects(slug=slug)
                os.remove('application/static/uploads/2020/' +
                          book[0].image_url)
                if form.validate_on_submit():
                    book.delete()
                    flash('You\'r book successfully deleted!', 'success')
                    return redirect(url_for('account') + '/mybooks')
                else:
                    return redirect(url_for('account') + '/book_view/' + title)
            else:
                return redirect(url_for('account') + '/mybooks')
        # this root for user can change their password
        elif term == 'change_password':
            form = ChangePassword()
            if form.validate_on_submit():
                new_password = form.new_password.data
                active_user.set_password(new_password)
                update_book = \
                    active_user.update(password=active_user.password)
                flash('You\'r password successfully updated!', 'success')
                return redirect(url_for('account') + '/change_password')
            else:
                return render_template('account/change_password.html',
                                       page_data=page_data,
                                       user=user,
                                       form=form)
        # this root for user can logout
        elif term == 'logout':
            session['user_id'] = False
            session['login'] = False
            flash('You are successfully logout!', 'danger')
            return redirect(url_for('account') + '/login')
        else:
            return redirect(url_for('account') + '/profile')
    else:
        # this root for new user can register for this webiste
        if term == 'register':
            form = RegisterForm()
            if form.validate_on_submit():
                name = form.name.data
                email = form.email.data
                password = form.password.data
                user = Users.objects(email=email).first()
                if user:
                    flash(
                        'You\'r email already existed! Please try another one.',
                        'danger')
                    return render_template('account/register.html',
                                           page_data=page_data,
                                           form=form)
                else:
                    user_id = Users.objects.all().count()
                    user_id += 1
                    name = form.name.data
                    email = form.email.data
                    password = form.password.data

                    user = Users(user_id=user_id,
                                 name=name,
                                 email=email,
                                 password=password)
                    user.set_password(password)
                    user.save()

                    session['user_id'] = user_id
                    session['login'] = True
                    flash('You are successfully register!', 'success')
                    return redirect(url_for('account') + '/profile')
            else:

                return render_template('account/register.html',
                                       page_data=page_data,
                                       form=form)
        # this root for user can login
        elif term == 'login':
            form = LoginForm()
            page_data = PageData
            if form.validate_on_submit():
                email = form.email.data
                password = form.password.data
                user = Users.objects(email=email).first()
                if user and user.get_password(password):
                    session['user_id'] = user.user_id
                    session['login'] = True

                    flash('You are successfully logged in!', 'success')
                    return redirect(url_for('account') + '/profile')
                else:
                    if user and user.password != password:
                        flash(
                            'Sorry, Email and password didn\'t match! Please try again',
                            'danger')
                    else:
                        flash('Sorry, Invalid users details!', 'danger')
                        return redirect(url_for('account') + '/profile')

            return render_template('account/login.html',
                                   form=form,
                                   page_data=page_data)
        else:
            return redirect(url_for('account') + '/login')