def addItemPage(): state = get_state() query = Query() users = query.get_user_ids() auth, user = is_authenticated(user_session, users) # If user is not authenticated, then auth is False and # html-template has rule that if false show message that access is restricted if auth is False: categories = [] return render_template('additem.html', auth=auth, user=user, categories=categories, STATE=state) # in Post request user sends information on book and new book is created. if request.method == 'POST' and auth is True: new_book = { '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), 'owner': user_session['user_id'], 'img_url': None, # TODO: in case file upload implemented } book_id = query.create_book(new_book) return redirect(url_for('showItemPage', category=new_book['category'], item_id=book_id)) categories = query.select_categories() query.close() return render_template('additem.html', auth=auth, user=user, categories=categories, 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 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 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 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 addItemPage(): state = get_state() query = Query() users = query.get_user_ids() auth, user = is_authenticated(user_session, users) # If user is not authenticated, then auth is False and # html-template has rule that if false show message that access is restricted if auth is False: categories = [] return render_template('additem.html', auth=auth, user=user, categories=categories, STATE=state) # in Post request user sends information on book and new book is created. if request.method == 'POST' and auth is True: new_book = { '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), 'owner': user_session['user_id'], 'img_url': None, # TODO: in case file upload implemented } book_id = query.create_book(new_book) return redirect( url_for('showItemPage', category=new_book['category'], item_id=book_id)) categories = query.select_categories() query.close() return render_template('additem.html', auth=auth, user=user, categories=categories, STATE=state)