Exemple #1
0
def editItemPage(item_id):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    # This page is only for registered users
    if auth is False:
        query.close()
        return render_template('edititem.html', auth=auth, valid=False)
    # Request book data from DB including ownership
    book_data = query.select_books(columns=[
        'id', 'owner', 'title', 'author', 'pub_year', 'description', 'category'
    ],
                                   limits={'id': item_id},
                                   number=1)[0]
    # Checks if user is the owner of the book
    if book_data['owner'] != user['id']:
        query.close()
        return render_template('edititem.html',
                               auth=auth,
                               user=user,
                               valid=False,
                               STATE=state)
    # Handling POST request
    if request.method == 'POST':
        edit_book = {
            'id': item_id,
            'owner': book_data['owner'],
            'title': form_data(request.form, 'title', None),
            'author': form_data(request.form, 'author', None),
            'pub_year': form_data(request.form, 'pub_year', None),
            'description': form_data(request.form, 'description', None),
            'category': form_data(request.form, 'category', None),
            'img_url': None,  # TODO: in case file upload implemented
        }
        try:
            edit_book['pub_year'] = int(edit_book['pub_year'])
        except:
            edit_book['pub_year'] = None
        book_id = query.edit_book(edit_book)
        query.close()
        return redirect(
            url_for('showItemPage',
                    category=edit_book['category'],
                    item_id=book_id,
                    STATE=state))
    categories = query.select_categories()
    query.close()
    return render_template('edititem.html',
                           auth=auth,
                           user=user,
                           valid=True,
                           book=book_data,
                           categories=categories,
                           STATE=state)
Exemple #2
0
def showItemPage(category, item_id):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    book = query.select_books(
        columns=['id', 'title', 'category', 'pub_year', 'author', 'description', 'owner', 'owner_name', 'only_date'],
        limits={'id': item_id},
        number=1
    )[0]
    query.close()
    return render_template('showitem.html', auth=auth, user=user, book=book, category=category, STATE=state)
Exemple #3
0
def showCategoryPage(category):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    categories = query.select_categories()
    books = query.select_books(
        columns=['id', 'title', 'category', 'owner'],
        limits={'category': category}
    )
    query.close()
    return render_template('showcategory.html', categories=categories, books=books, auth=auth, user=user, STATE=state)
Exemple #4
0
def showCatalogPage():
    state = get_state()
    query = Query()
    categories = query.select_categories()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    recent = query.select_books(
        columns=['id', 'title', 'category'],
        recent=True,
        number=5)
    query.close()
    return render_template('showcatalog.html', categories=categories, recent=recent, auth=auth, user=user, STATE=state)
Exemple #5
0
def showCategoryPage(category):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    categories = query.select_categories()
    books = query.select_books(columns=['id', 'title', 'category', 'owner'],
                               limits={'category': category})
    query.close()
    return render_template('showcategory.html',
                           categories=categories,
                           books=books,
                           auth=auth,
                           user=user,
                           STATE=state)
Exemple #6
0
def showCatalogPage():
    state = get_state()
    query = Query()
    categories = query.select_categories()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    recent = query.select_books(columns=['id', 'title', 'category'],
                                recent=True,
                                number=5)
    query.close()
    return render_template('showcatalog.html',
                           categories=categories,
                           recent=recent,
                           auth=auth,
                           user=user,
                           STATE=state)
Exemple #7
0
def showItemPage(category, item_id):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    book = query.select_books(columns=[
        'id', 'title', 'category', 'pub_year', 'author', 'description',
        'owner', 'owner_name', 'only_date'
    ],
                              limits={'id': item_id},
                              number=1)[0]
    query.close()
    return render_template('showitem.html',
                           auth=auth,
                           user=user,
                           book=book,
                           category=category,
                           STATE=state)
Exemple #8
0
def editItemPage(item_id):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    # This page is only for registered users
    if auth is False:
        query.close()
        return render_template('edititem.html', auth=auth, valid=False)
    # Request book data from DB including ownership
    book_data = query.select_books(
        columns=['id', 'owner', 'title', 'author', 'pub_year', 'description', 'category'],
        limits={'id': item_id},
        number=1
    )[0]
    # Checks if user is the owner of the book
    if book_data['owner'] != user['id']:
        query.close()
        return render_template('edititem.html', auth=auth, user=user, valid=False, STATE=state)
    # Handling POST request
    if request.method == 'POST':
        edit_book = {
            'id': item_id,
            'owner': book_data['owner'],
            'title': form_data(request.form, 'title', None),
            'author': form_data(request.form, 'author', None),
            'pub_year': form_data(request.form, 'pub_year', None),
            'description': form_data(request.form, 'description', None),
            'category': form_data(request.form, 'category', None),
            'img_url': None,  # TODO: in case file upload implemented
        }
        try:
            edit_book['pub_year'] = int(edit_book['pub_year'])
        except:
            edit_book['pub_year'] = None
        book_id = query.edit_book(edit_book)
        query.close()
        return redirect(url_for('showItemPage', category=edit_book['category'], item_id=book_id, STATE=state))
    categories = query.select_categories()
    query.close()
    return render_template('edititem.html', auth=auth, user=user,
                           valid=True, book=book_data, categories=categories, STATE=state)
Exemple #9
0
def showJSON():
    query = Query()
    categories = query.select_categories()
    books = query.select_books(columns=[
        'id', 'title', 'category', 'pub_year', 'author', 'description',
        'owner', 'owner_name', 'only_date', 'add_date'
    ], )
    query.close()
    books_by_cat = {}
    for book in books:
        book['only_date'] = str(book['only_date'])
        book['add_date'] = str(book['add_date'])
        try:
            books_by_cat[book['category']].append(book)
        except KeyError:
            books_by_cat[book['category']] = [book]
    for cat in categories:
        cat['books'] = books_by_cat[cat['name']]
    result = {'categories': categories}
    return jsonify(result)
Exemple #10
0
def deleteItemPage(item_id):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    # This page is only for registered users
    if auth is False:
        query.close()
        return render_template('deleteitem.html', auth=auth, valid=False, STATE=state)
    book_data = query.select_books(columns=['id', 'owner', 'category'], limits={'id': item_id}, number=1)[0]
    # Checks if user is the owner of the book
    if book_data['owner'] != user['id']:
        query.close()
        return render_template('deleteitem.html', auth=auth, user=user, valid=False, STATE=state)
    # Handling POST request
    if request.method == 'POST':
        query.delete_book(book_data)
        query.close()
        return redirect(url_for('showCategoryPage', category=book_data['category'], STATE=state))
    query.close()
    return render_template('deleteitem.html', auth=auth, user=user, valid=True, book=book_data, STATE=state)
Exemple #11
0
def showJSON():
    query = Query()
    categories = query.select_categories()
    books = query.select_books(
        columns=[
            'id', 'title', 'category', 'pub_year', 'author', 'description',
            'owner', 'owner_name', 'only_date', 'add_date'
        ],
    )
    query.close()
    books_by_cat = {}
    for book in books:
        book['only_date'] = str(book['only_date'])
        book['add_date'] = str(book['add_date'])
        try:
            books_by_cat[book['category']].append(book)
        except KeyError:
            books_by_cat[book['category']] = [book]
    for cat in categories:
        cat['books'] = books_by_cat[cat['name']]
    result = {'categories': categories}
    return jsonify(result)
Exemple #12
0
def deleteItemPage(item_id):
    state = get_state()
    query = Query()
    users = query.get_user_ids()
    auth, user = is_authenticated(user_session, users)
    # This page is only for registered users
    if auth is False:
        query.close()
        return render_template('deleteitem.html',
                               auth=auth,
                               valid=False,
                               STATE=state)
    book_data = query.select_books(columns=['id', 'owner', 'category'],
                                   limits={'id': item_id},
                                   number=1)[0]
    # Checks if user is the owner of the book
    if book_data['owner'] != user['id']:
        query.close()
        return render_template('deleteitem.html',
                               auth=auth,
                               user=user,
                               valid=False,
                               STATE=state)
    # Handling POST request
    if request.method == 'POST':
        query.delete_book(book_data)
        query.close()
        return redirect(
            url_for('showCategoryPage',
                    category=book_data['category'],
                    STATE=state))
    query.close()
    return render_template('deleteitem.html',
                           auth=auth,
                           user=user,
                           valid=True,
                           book=book_data,
                           STATE=state)