class AdminGenreDetailsView(TemplateView): template_name = 'Store/admin/genres/details.html' gdao = GenreDao() udao = UserDao() bdao = BookDao() idao = InventoryDao() @never_cache def get(self,request,genre_id): genre = self.gdao.get_byid(genre_id) books = self.idao.getInventoryByGenre(genre_id) sum_inventory = self.gdao.getTotalInventoryByGenreID(genre_id) initial_data = { 'genre':genre.genre } egenre = GenreForm(initial_data) context = { 'genre':genre, 'books': books, 'egenre': egenre, 'sum': self.gdao.getTotalGenreRevenueByGenreID(genre_id), 'sum_inventory':sum_inventory } user_id = request.session['user_id'] username = request.session['username'] context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return render(request, self.template_name, context) def post(self,request,genre_id): user_id = request.session['user_id'] username = request.session['username'] egenre = GenreForm(request.POST) context = {} if 'update-genre' in request.POST: if egenre.is_valid(): g = Genre() g.genre = egenre.cleaned_data['genre'] g.genre_id = genre_id self.gdao.update(g) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse(('admingenredetail'),kwargs={ 'genre_id': genre_id })) elif 'delete-genre' in request.POST: g = Genre() g.genre_id = genre_id self.gdao.delete(g) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('admingenreindex')) else: return redirect(reverse(('admingenredetail'),kwargs={ 'genre_id': genre_id }))
class AdminAuthorDetailView(TemplateView): template_name = 'Store/admin/authors/details.html' adao = AuthorDao() bdao = BookDao() idao = InventoryDao() @never_cache def get(self, request, author_id): author = self.adao.get_byid(author_id) books = self.idao.getInventoryByAuthor(author_id) initial_data = { 'first_name': author.first_name, 'last_name': author.last_name } eauthor = AuthorForm(initial_data) context = { 'author': author, 'books': books, 'eauthor': eauthor, 'sum': self.adao.getTotalAuthorRevenueByAuthorID(author_id), 'sum_inventory': self.adao.getTotalInventoryByAuthorID(author_id) } user_id = request.session['user_id'] username = request.session['username'] context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] return render(request, self.template_name, context) @never_cache def post(self, request, author_id): user_id = request.session['user_id'] username = request.session['username'] context = {} eauthor = AuthorForm(request.POST) if 'update-author' in request.POST: if eauthor.is_valid(): a = Author() a.author_id = author_id a.first_name = eauthor.cleaned_data['first_name'] a.last_name = eauthor.cleaned_data['last_name'] self.adao.update(a) context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] return redirect( reverse(('adminauthordetail'), kwargs={'author_id': author_id})) if 'delete-author' in request.POST: self.adao.delete(author_id) context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] return redirect(reverse('adminauthorindex'))
class AdminInventoryView(TemplateView): template_name = 'Store/admin/inventory/inventory.html' idao = InventoryDao() @never_cache def get(self, request): inventory = self.idao.get_all() context = {'inventory': inventory} user_id = request.session['user_id'] username = request.session['username'] context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return render(request, self.template_name, context)
class CusBookDetailView(TemplateView): template_name = 'Store/customer/books/details.html' book_dao = BookDao() cart_dao = CartDao() image_dao = ImageDao() inventory_dao = InventoryDao() @never_cache def get(self, request, book_id): if 'user_id' in request.session: book = self.book_dao.get_byid(book_id) images = self.image_dao.get_byid(book_id) # Gets the total quantity and generates a list for choices of qty max_quantity = self.inventory_dao.get_byid( book_id).quantity_on_hand qty_choices = [] for i in range(1, (max_quantity + 1)): qty_choices.append((i, i)) cart_form = AddToCartForm(qty_choices=qty_choices) context = { 'user_id': request.session['user_id'], 'book': book, 'images': images, 'cart_form': cart_form } return render(request, self.template_name, context) else: return redirect(reverse('login')) def post(self, request, book_id): # Gets the total quantity and generates a list for choices of qty max_quantity = self.inventory_dao.get_byid(book_id).quantity_on_hand qty_choices = [] for i in range(1, (max_quantity + 1)): qty_choices.append((i, i)) cart_item = Cart() cart_item.book.book_id = book_id cart_item.user_id = request.session['user_id'] cart_form = AddToCartForm(request.POST, qty_choices=qty_choices) if cart_form.is_valid(): cart_item.quantity_ordered = cart_form.cleaned_data['qty_choices'] self.cart_dao.create(cart_item) return redirect(reverse('cart'))
class CartView(TemplateView): template_name = 'Store/customer/cart.html' inventory_dao = InventoryDao() cart_dao = CartDao() @never_cache def get(self, request): context = {} if 'user_id' in request.session: user_id = request.session['user_id'] cart_items = self.cart_dao.get_all(user_id) username = request.session['username'] cart_total = 0 # An array of arrays of choices for each book qtys_choices = [] # An array of book IDs to pass to the cart form book_ids = [] # A dictionary for all of the initial choices initial_data = {} for index, item in enumerate(cart_items): cart_total += (item.book.inventory.retail_price * item.quantity_ordered) # Get quantities on hand of each item in cart qty_on_hand = self.inventory_dao.get_byid( item.book.book_id).quantity_on_hand qty_choices = [] for i in range(1, (qty_on_hand + 1)): qty_choices.append((i, i)) qtys_choices.append(qty_choices) # Append initial data for each item to the dictionary initial_data['qty_choice_%s' % index] = item.quantity_ordered # Append book ID to list book_ids.append(item.book.book_id) cart_form = CartForm(initial_data, book_ids=book_ids, qtys_choices=qtys_choices) context['cart_form'] = cart_form context['cart_items'] = cart_items context['cart_total'] = cart_total if cart_total >= 75: context['discount_price'] = round((cart_total * Decimal(0.9)), 2) context['user_id'] = user_id context['username'] = username else: return redirect(reverse('login')) return render(request, self.template_name, context) def post(self, request): user_id = request.session['user_id'] if 'delete-item' in request.POST: cart_dao = CartDao() book_id = int(request.POST.get('delete-item')) cart_dao.delete_from_cart(book_id, user_id) return redirect(reverse('cart'))
class AdminBookDetailView(TemplateView): template_name = 'Store/admin/books/details.html' book_dao = BookDao() image_dao = ImageDao() inventory_dao = InventoryDao() publisher_dao = PublisherDao() genre_dao = GenreDao() author_dao = AuthorDao() # This gets the choices for the author, publisher, and genre dropdowns authors = [] authors.append(("default", { 'label': "Choose an author", 'disabled': True })) for author in author_dao.get_all(): author_val = (str(author.author_id), str(author.last_name) + ", " + str(author.first_name)) authors.append(author_val) publishers = [] publishers.append(("default", { 'label': "Choose a publisher", 'disabled': True })) for publisher in publisher_dao.get_all(): publisher_val = (str(publisher.publisher_id), str(publisher.company_name)) publishers.append(publisher_val) genres = [] genres.append(("default", {'label': "Choose a genre", 'disabled': True})) for genre in genre_dao.get_all(): genre_val = (str(genre.genre_id), str(genre.genre)) genres.append(genre_val) @never_cache def get(self, request, book_id): book = self.book_dao.get_byid(book_id) images = self.image_dao.get_byid(book_id) inventory = self.inventory_dao.get_byid(book_id) initial_data = { 'title': book.title, 'authors': book.author.author_id, 'isbn10': book.isbn10, 'isbn13': book.isbn13, 'copyright_date': book.copyRightDate, 'edition': book.edition, 'publishers': book.publisher.publisher_id, 'book_type': book.type, 'num_pages': book.numberOfPages, 'genres': book.genre.genre_id, 'quantity_on_hand': inventory.quantity_on_hand, 'cost': inventory.cost, 'retail_price': inventory.retail_price } book_form = BookForm(initial_data, author_choices=self.authors, publisher_choices=self.publishers, genre_choices=self.genres) image_form = BookImageForm() context = { 'book': book, 'images': images, 'book_form': book_form, 'image_form': image_form } context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return render(request, self.template_name, context) def post(self, request, book_id): user_id = request.session['user_id'] username = request.session['username'] book_form = BookForm(request.POST, author_choices=self.authors, publisher_choices=self.publishers, genre_choices=self.genres) book = self.book_dao.get_byid(book_id) context = {'book': book} if 'update-book' in request.POST: if book_form.is_valid(): updated_book = Book() updated_book.book_id = book_id updated_book.title = book_form.cleaned_data['title'] updated_book.isbn10 = book_form.cleaned_data['isbn10'] updated_book.isbn13 = book_form.cleaned_data['isbn13'] updated_book.copyRightDate = book_form.cleaned_data[ 'copyright_date'] updated_book.edition = book_form.cleaned_data['edition'] updated_book.numberOfPages = book_form.cleaned_data[ 'num_pages'] updated_book.type = book_form.cleaned_data['book_type'] author = Author() author.author_id = int(book_form.cleaned_data['authors']) updated_book.author = author publisher = Publisher() publisher.publisher_id = int( book_form.cleaned_data['publishers']) updated_book.publisher = publisher genre = Genre() genre.genre_id = int(book_form.cleaned_data['genres']) updated_book.genre = genre x = self.inventory_dao.get_byid(book_id) updated_inventory = Inventory() updated_inventory.book_id = book_id updated_inventory.quantity_on_hand = book_form.cleaned_data[ 'quantity_on_hand'] updated_inventory.quantity_ordered = x.quantity_ordered updated_inventory.retail_price = book_form.cleaned_data[ 'retail_price'] updated_inventory.cost = book_form.cleaned_data['cost'] self.book_dao.update(updated_book) self.inventory_dao.update(updated_inventory) context['notification'] = "Book updated successfully!" context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect( reverse(('adminbookdetail'), kwargs={'book_id': book_id})) else: context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] context['notification'] = "Not a valid submission." elif 'delete-book' in request.POST: book_id = int(request.POST.get('delete-book')) self.book_dao.delete(book_id) if 'add-image' in request.POST: image_form = BookImageForm(request.POST, request.FILES) if image_form.is_valid(): image_file = request.FILES['image'] fs = FileSystemStorage() filename = fs.save(image_file.name, image_file) uploaded_file_url = fs.url(filename) image = Image() image.image_url = uploaded_file_url image.caption = '' image.book.book_id = book_id self.image_dao.create(image) context['notification'] = filename return redirect( reverse(('adminbookdetail'), kwargs={'book_id': book_id})) else: context['notification'] = "Not a valid submission." return render(request, self.template_name, context)
class AdminPublisherDetailView(TemplateView): template_name = 'Store/admin/publishers/details.html' pdao = PublisherDao() bdao = BookDao() idao = InventoryDao() @never_cache def get(self, request, publisher_id): publisher = self.pdao.get_byid(publisher_id) books = self.idao.getInventoryByPublisher(publisher_id) initial_data = { 'company_name': publisher.company_name, 'city': publisher.city, 'state_code': publisher.state_code, 'zip_code': publisher.zip_code, 'phone_number': publisher.phone_number, 'contact_name': publisher.contact_name } epublisher = PublisherForm(initial_data) context = { 'publisher': publisher, 'books': books, 'epublisher': epublisher, 'sum': self.pdao.getTotalPublisherRevenueByPublisherID(publisher_id), 'sum_inventory': self.pdao.getTotalInventoryByPublisherID(publisher_id) } user_id = request.session['user_id'] username = request.session['username'] context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return render(request, self.template_name, context) @never_cache def post(self, request, publisher_id): user_id = request.session['user_id'] username = request.session['username'] context = {} epublisher = PublisherForm(request.POST) if 'update-publisher' in request.POST: if epublisher.is_valid(): p = Publisher() p.company_name = epublisher.cleaned_data['company_name'] p.city = epublisher.cleaned_data['city'] p.state_code = epublisher.cleaned_data['state_code'] p.zip_code = epublisher.cleaned_data['zip_code'] p.phone_number = epublisher.cleaned_data['phone_number'] p.contact_name = epublisher.cleaned_data['contact_name'] p.publisher_id = publisher_id self.pdao.update(p) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect( reverse(('adminpublisherdetail'), kwargs={'publisher_id': publisher_id})) if 'delete-publisher' in request.POST: self.pdao.delete(publisher_id) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('adminpublisherindex'))