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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)