def getJanuaryOrders(self): orders = [] try: # Setup connection to the DB db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() # Calls the stored procedure cursor.callproc('getJanuaryOrders') udao = UserDao() cadao = CustomerAddressDao() pdao = PaymentInfoDao() # This loop iterates through the resultsets for result in cursor.stored_results(): # This loop iterates through the rows in each resultset for x in result.fetchall(): order = RetailOrder() order.order_id = x[0] order.date_ordered = x[1] order.total_price = x[2] order.discount = x[3] order.customer = x[4] orders.append(order) # Close the connection to the DB cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return orders
class AdminGenreIndexView(TemplateView): template_name = 'Store/admin/genres/genres.html' gdao = GenreDao() udao = UserDao() @never_cache def get(self,request): genres = self.gdao.get_all() agenre = GenreForm() context = { 'genres':genres, 'agenre': agenre } 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): user_id = request.session['user_id'] username = request.session['username'] agenre = GenreForm(request.POST) context = {} if 'create-genre' in request.POST: if agenre.is_valid(): genre = Genre() genre.genre = agenre.cleaned_data['genre'] self.gdao.create(genre) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('admingenreindex')) else: return redirect(reverse('adminindex'))
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 CustomerCardView(TemplateView): template_name = 'Store/customer/ccard.html' udao = UserDao() pdao = PaymentInfoDao() cadao = CustomerAddressDao() rdao = RetailOrderDao() @never_cache def get(self,request,card_id): user_id = request.session['user_id'] username = request.session['username'] card = self.pdao.get_byid(card_id) orders = self.rdao.getOrdersByCardID(card_id) intitial_data = { 'street': card.billing_address.street, 'city': card.billing_address.city, 'state_code': card.billing_address.state_code, 'zip_code': card.billing_address.zip_code } eaddress = AddAddressForm2(intitial_data) context = { 'card': card, 'eaddress': eaddress, 'orders': orders } context['username'] = username context['user_id'] = user_id return render(request, self.template_name, context) @never_cache def post(self,request,card_id): user_id = request.session['user_id'] username = request.session['username'] card = self.pdao.get_byid(card_id) context={} eaddress = AddAddressForm2(request.POST) if 'update-address' in request.POST: if eaddress.is_valid(): a = CustomerAddress() a.address_id = card.billing_address.address_id a.customer_id = user_id a.street = eaddress.cleaned_data['street'] a.city = eaddress.cleaned_data['city'] a.state_code = eaddress.cleaned_data['state_code'] a.zip_code = eaddress.cleaned_data['zip_code'] a.address_type = 'Billing' self.cadao.update(a) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse(('customercard'),kwargs={'card_id':card_id})) else: context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse(('customercard'),kwargs={'card_id':card_id}))
class AdminCustomerByStateView(TemplateView): template_name = 'Store/admin/customers/states.html' udao = UserDao() @never_cache def get(self, request): customers = self.udao.getUsersByState() context = {'customers': customers} 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 getOrdersByShippingAddressID(self, addresss_id): orders = [] try: # Setup connection to the DB db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() args = [addresss_id] # Calls the stored procedure cursor.callproc('getOrdersByShippingAddressID', args) udao = UserDao() cadao = CustomerAddressDao() pdao = PaymentInfoDao() # This loop iterates through the resultsets for result in cursor.stored_results(): # This loop iterates through the rows in each resultset for x in result.fetchall(): order = RetailOrder() order.order_id = x[0] order.date_ordered = x[1] order.total_price = x[2] order.discount = x[3] order.customer = udao.get_byid(x[4]) order.shipping_address = cadao.get_byid(x[5]) order.card = pdao.get_byid(x[6]) order.status = x[7] orders.append(order) # Close the connection to the DB cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return orders
class CustomerIndexView(TemplateView): template_name = 'Store/customer/index.html' user = User() udao = UserDao() @never_cache def get(self,request): if 'user_id' in request.session: context = {} context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return render(request, self.template_name,context) else: return redirect(reverse('login')) @never_cache def post(self,request): context = {} context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return render(request, self.template_name,context)
class CustomerAccountView(TemplateView): template_name = 'Store/customer/customeraccount.html' user = User() udao = UserDao() customer = CustomerInfo() cdao = CustomerInfoDAO() @never_cache def get(self,request): user_id = request.session['user_id'] username = request.session['username'] user = self.udao.get_byid(user_id) customer = self.cdao.get_byid(user_id) initial_data = { 'first_name': customer.user.first_name, 'last_name': customer.user.last_name, 'email': customer.user.email, 'work_phone': customer.work_phone, 'home_phone': customer.home_phone } info_form = CustomerInfoForm(initial_data) Cuserpass = ChangeUsernamePassword() context = { 'customer': customer, 'info_form': info_form, 'Cuserpass': Cuserpass } 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): user_id = request.session['user_id'] username = request.session['username'] info_form = CustomerInfoForm(request.POST) Cuserpass = ChangeUsernamePassword(request.POST) context = {} if 'edit-info' in request.POST: if info_form.is_valid(): updateinfo = CustomerInfo() u = User() updateinfo.customer_id = user_id updateinfo.home_phone = info_form.cleaned_data['home_phone'] updateinfo.work_phone = info_form.cleaned_data['work_phone'] u.first_name = info_form.cleaned_data['first_name'] u.last_name = info_form.cleaned_data['last_name'] u.email = info_form.cleaned_data['email'] updateinfo.set_user(u) self.cdao.update(updateinfo) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('customeraccount')) elif 'changeusernamepassword' in request.POST: if Cuserpass.is_valid(): u = self.udao.get_byid(user_id) u.id = user_id u.username = Cuserpass.cleaned_data['username'] current_password = Cuserpass.cleaned_data['password'] if check_password(current_password, u.password): x = Cuserpass.cleaned_data['password2'] u.password = make_password(x,salt=None,hasher='default') self.udao.updateUserPass(u) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('customeraccount')) elif 'deactivate' in request.POST: self.udao.deactivateUser(user_id) return redirect(reverse('login')) else: return redirect(reverse('customeraccount'))
class CustomerAddCardView(TemplateView): template_name = 'Store/customer/addcard.html' user = User() udao = UserDao() customer = CustomerInfo() cdao = CustomerInfoDAO() cusadd = CustomerAddress() cadao = CustomerAddressDao() payment = PaymentInfo() pdao = PaymentInfoDao() @never_cache def get(self,request): context={} user_id = request.session['user_id'] username = request.session['username'] user = self.udao.get_byid(user_id) customer = self.cdao.get_byid(user_id) cusadd =CustomerAddress() cusadd.customer_id = user_id caddress = self.cadao.get_all_addresses_by_customer_id(user_id) bill_addresses = self.cadao.get_by_customer_and_type(user_id, "Billing") bill_address_choices = [] for address in bill_addresses: address_val = (str(address.address_id), str(address.street) + " " + str(address.city) + ", " + str(address.state_code) + " " + str(address.zip_code)) bill_address_choices.append(address_val) num_bill_address = len(bill_address_choices) aaddress = AddAddressForm2() addcard = AddPaymentInfoForm(bill_address_choices=bill_address_choices) context['addcard'] = addcard context['aaddress'] = aaddress context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] context['num_bill_address'] = num_bill_address return render(request, self.template_name,context) @never_cache def post(self,request): context={} user_id = request.session['user_id'] username = request.session['username'] bill_addresses = self.cadao.get_by_customer_and_type(user_id, "Billing") bill_address_choices = [] for address in bill_addresses: address_val = (str(address.address_id), str(address.street) + " " + str(address.city) + ", " + str(address.state_code) + " " + str(address.zip_code)) bill_address_choices.append(address_val) addcard = AddPaymentInfoForm(request.POST,bill_address_choices=bill_address_choices) aaddress = AddAddressForm2(request.POST) if 'add-card' in request.POST: if addcard.is_valid(): p = PaymentInfo() p.customer_id = user_id p.card_number = addcard.cleaned_data['card_number'] p.cvc = addcard.cleaned_data['cvc'] p.expir_date = addcard.cleaned_data['expir_date'] p.card_issuer = addcard.cleaned_data['card_issuer'] p.billing_address.address_id = addcard.cleaned_data['billing_addresses'] self.pdao.create(p) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('customeraccount')) elif 'add-address' in request.POST: if aaddress.is_valid(): a = CustomerAddress() a.customer_id = user_id a.street = aaddress.cleaned_data['street'] a.city = aaddress.cleaned_data['city'] a.state_code = aaddress.cleaned_data['state_code'] a.zip_code = aaddress.cleaned_data['zip_code'] a.address_type = 'Billing' self.cadao.create(a) context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] return redirect(reverse('customeraddcard')) else: return redirect(reverse('customeraccount'))
class CAddressAccountView(TemplateView): udao = UserDao() user = User() cadao = CustomerAddressDao() customer = CustomerAddress() padao = PaymentInfoDao() rdao = RetailOrderDao() template_name = 'Store/customer/caddressaccount.html' @never_cache def get(self,request,address_id): user_id = request.session['user_id'] username = request.session['username'] address = self.cadao.get_byid(address_id) payment = self.padao.get_by_address_id(address_id,user_id) orders = self.rdao.getOrdersByCardID(address_id) initial_data = { 'street': address.street, 'city': address.city, 'state_code': address.state_code, 'zip_code': address.zip_code, 'address_type': address.address_type } eaddress = EditAddressForm(initial_data) daddress = DeleteAddressForm() apayment = AddPaymentInfoForm2() context = { 'address':address, 'eaddress': eaddress, 'payment': payment, 'apayment': apayment, 'daddress': daddress, 'orders': orders } 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,address_id): user_id = request.session['user_id'] username = request.session['username'] eaddress = EditAddressForm(request.POST) apayment = AddPaymentInfoForm2(request.POST) daddress = DeleteAddressForm(request.POST) address = self.cadao.get_byid(address_id) user_id = address.customer_id context = {} if 'edit-address' in request.POST: if eaddress.is_valid(): a = CustomerAddress() a.address_id = address_id a.customer_id = user_id a.street = eaddress.cleaned_data['street'] a.city = eaddress.cleaned_data['city'] a.state_code = eaddress.cleaned_data['state_code'] a.zip_code = eaddress.cleaned_data['zip_code'] a.address_type = eaddress.cleaned_data['address_type'] self.cadao.update(a) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse(('customeraddress'), kwargs={'address_id': address_id})) elif 'add-card' in request.POST: if apayment.is_valid(): p = PaymentInfo() p.card_number = apayment.cleaned_data['card_number'] p.cvc = apayment.cleaned_data['cvc'] p.expir_date = apayment.cleaned_data['expir_date'] p.card_issuer = apayment.cleaned_data['card_issuer'] p.customer_id = user_id p.billing_address.address_id = address_id self.padao.create(p) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse(('customeraddress'), kwargs={'address_id': address_id})) elif 'delete-address' in request.POST: if daddress.is_valid(): a = CustomerAddress() a.address_id = daddress.cleaned_data['address_id'] a.customer_id = user_id self.cadao.delete(a) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('customeraccount')) else: return redirect(reverse(('customeraddress'), kwargs={'address_id': address_id}))
class LoginView(TemplateView): user = User() udao = UserDao() template_name = 'Store/login.html' cus_loggedin_template = 'Store/index.html' admin_loggedin_template = 'Store/admin/index.html' user.username = '******' customer = CustomerInfo() cdao = CustomerInfoDAO() @never_cache def get(self, request): loginform = LoginForm() registerform = RegisterUserForm() context = { 'loginform': loginform, 'registerform': registerform } return render(request, self.template_name, context) def post(self, request): #settings.configure(DEBUG=True) loginform = LoginForm(request.POST) registerform = RegisterUserForm(request.POST) context = { 'user_id': None } if 'login-user' in request.POST: if loginform.is_valid(): # Get user data from database user = User() user.username = loginform.cleaned_data['username'] user = self.udao.get_byusername(user.username) input_password = loginform.cleaned_data['password'] # Validate login if check_password(input_password, user.password): # Store session data request.session['user_id'] = user.id request.session['username'] = user.username request.session['user_is_staff'] = user.is_staff context['text'] = 'Yay password' context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] self.udao.updateLastLogin(user.id) if user.is_staff == 0 and user.is_active == 1: return redirect(reverse('home')) elif user.is_active == 0 and user.is_staff == 0: self.udao.activateUser(user.id) return redirect(reverse('home')) else: return redirect(reverse('adminindex')) # Handle if password is bad else: loginform = LoginForm() registerform = RegisterUserForm() context = { 'loginform': loginform, 'registerform': registerform, 'login_error': 'Either username or password is incorrect' } return render(request, self.template_name, context) else: context['text'] = 'try again' return render(request, self.template_name, context) if 'create-user' in request.POST: if registerform.is_valid(): u = User() u.first_name = registerform.cleaned_data['first_name'] u.last_name = registerform.cleaned_data['last_name'] u.email = registerform.cleaned_data['email'] u.username = registerform.cleaned_data['username'] x = registerform.cleaned_data['password'] u.is_superuser = 0 u.is_active = 1 u.is_staff = 0 u.password = make_password(x,salt=None,hasher='default') self.udao.create(u) user = self.udao.get_byusername(u.username) customer = CustomerInfo() customer.customer_id = user.id customer.work_phone = registerform.cleaned_data['work_phone'] customer.home_phone = registerform.cleaned_data['home_phone'] self.cdao.create(customer) loginform = LoginForm() registerform = RegisterUserForm() context = { 'loginform': loginform, 'registerform': registerform } return render(request, self.template_name, context)