def order(request, pk): my_user = request.user order = get_object_or_404(restaurant.Order, pk=pk) customer = order.customer customer_address = address.CustomerAddress.objects.get(customer=customer) my_resturant = user.Deliverer.objects.get(user=my_user).restaurant restaurant_address = address.RestaurantAddress.objects.get( restaurant=my_resturant) if (request.method == "POST"): body = parse_req_body(request.body) rating = int(body['rating']) if 0 <= rating or rating <= 5: order.status = 'D' order.customer_rating = rating try: customer_status = restaurant.CustomerStatus.objects.get( customer=customer, restaurant=my_resturant) except: customer_status = restaurant.CustomerStatus( customer=customer, restaurant=my_resturant, status='N') customer_status.save() customer_status.update_status(rating) order.save() rating = order.delivery_rating return render(request, 'deliverer/order.html', context={ 'order': order, 'customerAddress': customer_address, 'restaurantAddress': restaurant_address })
def restaurant(request): print('manager-restaurant') try: user = request.user userIs = userTypeChecker(user) if userIs(Manager ) == True: # returns true if user is manager, else false restaurant = Restaurant.objects.get(manager__user=user) if request.method == 'POST': body = parse_req_body(request.body) task = body['task'] action = body['action'] if task == 'edit' and action == "edit_name": name = body['name'] restaurant.name = name elif task == 'edit' and action == "edit_description": description = body['description'] restaurant.description = description restaurant.save() context = {'restaurant': restaurant} return render(request, 'manager/restaurant.html', context=context) else: print('user not manager') return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def register(request): my_user = None try: my_user = request.user isType = userTypeChecker(my_user) if isType(user.Deliverer) != True: response = redirect('home-nexus') return response except: response = redirect('home-nexus') return response my_deliverer = user.Deliverer.objects.get(user=my_user) registered = len( user.Deliverer.objects.filter(user=my_user).exclude( restaurant__isnull=True)) > 0 and my_deliverer.status == 'H' if registered: return redirect('deliverer-home') registering = my_deliverer.restaurant == None and my_deliverer.status != 'H' restaurants = restaurant.Restaurant.objects.all() context = {'restaurants': restaurants, 'registering': registering} if request.method == "POST": body = parse_req_body(request.body) resturant_id = int(body['id']) reg_resturant = restaurant.Restaurant.objects.get(id=resturant_id) my_deliverer.restaurant = reg_resturant my_deliverer.save() context['registering'] = False return render(request, 'deliverer/register.html', context=context)
def customerdetails(request, pk): #must send customerid try: user = request.user userIs = userTypeChecker(user) if userIs(Manager) == True: restaurant = Restaurant.objects.get(manager__user=user) customer = get_object_or_404(User, pk=pk) if request.method == 'POST': body = parse_req_body(request.body) update_customer = CustomerStatus.objects.filter( restaurant=restaurant).filter(customer=customer) task = body['task'] action = body['action'] if task == 'status_change' and action == 'promote': update_customer.status = 'V' elif task == 'status_change' and action == 'demote': update_customer.status = 'R' elif task == 'status_change' and action == 'remove': update_customer.status = 'N' elif task == 'status_change' and action == 'blacklist': update_customer.status = 'B' update_customer.save() order_info = [] orders = Order.objects.filter(restaurant=restaurant).filter( customer=customer).order_by('created') for order in orders: info_entry = {} info_entry['order'] = order total_items = sum(order.order_Food.quantity) info_entry['total_items'] = total_items order_info.append(info_entry) food_complaints = Order_Food.objects.filter(customer=pk).filter( food_complaint__isnull=False) delivery_complaints = Order.objects.filter( restaurant=restaurant).filter(customer=pk).filter( delivery_rating__lte='2') complaints_received = Order.objects.filter( restaurant=restaurant).filter(customer=pk).filter( customer_rating__lte='2') num_complaints = len(complaints_received) context = { 'customer': customer, 'num_complaints': num_complaints, 'complaints_received': complaints_received, 'food_complaints': food_complaints, 'delivery_complaints': delivery_complaints, 'orders': orders_info, } return render(request, 'customerdetails.html', context=context) else: return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def home(request): my_user = None # makes sure user is deliverer try: my_user = request.user userIs = userTypeChecker(my_user) if userIs(user.Deliverer) != True: response = redirect('home-nexus') return response except Exception as e: print(e) response = redirect('home-nexus') return response except: response = redirect('home-nexus') return response my_deliverer = user.Deliverer.objects.get(user=my_user) registered = len( user.Deliverer.objects.filter(user=my_user).exclude( restaurant__isnull=True)) > 0 and my_deliverer.status == 'H' if registered != True: # if not registered return redirect('deliverer-register') if request.method == "POST": # If bidded body = parse_req_body(request.body) amount = body['amount'] order_id = body['orderId'] order = restaurant.Order.objects.get(id=order_id) new_bid = restaurant.DeliveryBid(deliverer=my_deliverer, win=False, price=amount, order=order) new_bid.save() unchosen_orders = restaurant.Order.objects.filter(chose_bid=False).filter( restaurant=my_deliverer.restaurant) pending_bids = restaurant.DeliveryBid.objects.filter( deliverer=my_deliverer).filter(win=False) won_bids = restaurant.DeliveryBid.objects.filter( deliverer=my_deliverer).filter(win=True) open_orders = [] for order in unchosen_orders: if len(pending_bids.filter(order=order)) == 0: open_orders.append(order) print(open_orders) print(pending_bids) print(won_bids) context = { 'warnings': my_deliverer.warnings, 'openOrders': open_orders, 'pendingBids': pending_bids, 'winningBids': won_bids } return render(request, 'deliverer/home.html', context=context)
def register(request): print('cook register') my_user = None try: my_user = request.user isType = userTypeChecker(my_user) if isType(user.Cook) != True: print('user not cook') response = redirect('home-nexus') return response except: print('user not cook') response = redirect('home-nexus') return response my_cook = user.Cook.objects.get(user=my_user) print('cook status:', my_cook.status) registered = len( user.Cook.objects.filter(user=my_user).exclude( restaurant__isnull=True)) > 0 and my_cook.status == 'H' if registered: print('cook registered') return redirect('cook-home') registering = my_cook.restaurant == None and my_cook.status != 'H' print('registering:', registering) restaurants = restaurant.Restaurant.objects.all() context = {'restaurants': restaurants, 'registering': registering} if request.method == "POST": body = parse_req_body(request.body) resturant_id = int(body['id']) reg_resturant = restaurant.Restaurant.objects.get(id=resturant_id) my_cook.restaurant = reg_resturant my_cook.save() context['registering'] = False return render(request, 'cook/register.html', context=context)
def deliverybids(request): print('manager-deliverybids') try: user = request.user userIs = userTypeChecker(user) if userIs(Manager) == True: user_manager = Manager.objects.get(user=user) # at event that manager selects bid if request.method == 'POST': body = parse_req_body(request.body) button = body['choose_bid'] if button == "Choose Bid": bid_id = int(body['bid_id']) win_bid = DeliveryBid.objects.get(id=bid_id) bid_order = win_bid.order if bid_order.chose_bid == False: win_bid.win = True win_bid.save() bid_order.chose_bid = True bid_order.save() deliverybids_info = [] orders = Order.objects.filter( restaurant=user_manager.restaurant, status='PR', chose_bid=False).order_by('created') for order in orders: info_entry = {} bids = DeliveryBid.objects.filter(order=order).filter( win=False).order_by('price') info_entry['order'] = order info_entry['bids'] = bids deliverybids_info.append(info_entry) context = { 'deliverybids_info': deliverybids_info, } return render(request, 'manager/deliverybids.html', context=context) elif request.method == "GET": deliverybids_info = [] orders = Order.objects.filter( restaurant=user_manager.restaurant, status='PR', chose_bid=False).order_by('created') for order in orders: info_entry = {} bids = DeliveryBid.objects.filter(order=order).filter( win=False).order_by('price') info_entry['order'] = order info_entry['bids'] = bids deliverybids_info.append(info_entry) context = { 'deliverybids_info': deliverybids_info, } return render(request, 'manager/deliverybids.html', context=context) else: return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def pendingregistrations( request): #if post, request must have customer user obj try: user = request.user userIs = userTypeChecker(user) if userIs(Manager) == True: restaurant = Restaurant.objects.get(manager__user=user) if request.method == 'POST': body = parse_req_body(request.body) user_id = int(body['user_id'] ) #this is a user object, COULD CAUSE ERRORS update_user = User.objects.get(pk=user_id) task = body['task'] action = body['action'] print('body') if task == 'approve_customer' and action == 'Approve': update_customer = CustomerStatus.objects.filter( restaurant=restaurant).get(customer__user=update_user) update_customer.status = 'R' update_customer.save() elif task == 'approve_customer' and action == 'Reject': update_customer = CustomerStatus.objects.filter( restaurant=restaurant).get(customer__user=update_user) update_customer.approve_status() update_customer.save() # change cook elif task == 'approve_cook' and action == 'Approve': update_staff = Cook.objects.get(user=update_user) update_staff.status = 'H' update_staff.salary = 600 update_staff.save() elif task == 'approve_cook' and action == 'Reject': update_staff = Cook.objects.get(user=update_user) update_staff.status = 'N' update_staff.restaurant = None update_staff.save() # change salesperson elif task == 'approve_salesperson' and action == 'Approve': update_staff = Salesperson.objects.get(user=update_user) update_staff.status = 'H' update_staff.salary = 600 update_staff.save() elif task == 'approve_salesperon' and action == 'Reject': update_staff = Salesperson.objects.get(user=update_user) update_staff.status = 'N' update_staff.restaurant = None update_staff.save() # change deliverer elif task == 'approve_deliverer' and action == 'Approve': update_staff = Deliverer.objects.get(user=update_user) update_staff.status = 'H' update_staff.salary = 600 update_staff.save() elif task == 'approve_deliverer' and action == 'Reject': update_staff = Deliverer.objects.get(user=update_user) update_staff.status = 'N' update_staff.restaurant = None update_staff.save() pending_customers = list( CustomerStatus.objects.filter( restaurant=restaurant).filter(status='P')) pending_cooks = list( Cook.objects.filter(restaurant=restaurant).filter( status='N')) pending_salespeople = list( Salesperson.objects.filter(restaurant=restaurant).filter( status='N')) pending_deliverers = list( Deliverer.objects.filter(restaurant=restaurant).filter( status='N')) context = { 'pending_customers': pending_customers, 'pending_cooks': pending_cooks, 'pending_salespeople': pending_salespeople, 'pending_deliverers': pending_deliverers } return render(request, 'manager/pendingregistrations.html', context=context) elif request.method == 'GET': user = request.user restaurant = Restaurant.objects.get(manager__user=user) pending_customers = list( CustomerStatus.objects.filter( restaurant=restaurant).filter(status='P')) pending_cooks = list( Cook.objects.filter(restaurant=restaurant).filter( status='N')) pending_salespeople = list( Salesperson.objects.filter(restaurant=restaurant).filter( status='N')) pending_deliverers = list( Deliverer.objects.filter(restaurant=restaurant).filter( status='N')) context = { 'pending_customers': pending_customers, 'pending_cooks': pending_cooks, 'pending_salespeople': pending_salespeople, 'pending_deliverers': pending_deliverers } return render(request, 'manager/pendingregistrations.html', context=context) else: print('not manager') return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def customers(request): try: user = request.user userIs = userTypeChecker(user) if userIs(Manager) == True: restaurant = Restaurant.objects.get(manager__user=user) if request.method == 'POST': body = parse_req_body(request.body) print(body) customer_id = int(body['customer_id']) print(customer_id) update_customer = CustomerStatus.objects.filter( restaurant=restaurant).get(customer__user__id=customer_id) print(update_customer) task = body['task'] action = body['action'] if task == 'status_change' and action == 'Promote to VIP': update_customer.status = 'V' elif task == 'status_change' and action == 'Demote to Regular': update_customer.status = 'R' elif task == 'status_change' and action == 'Remove Registration': update_customer.status = 'N' elif task == 'status_change' and action == 'Blacklist': update_customer.status = 'B' update_customer.save() registered_customers = list( CustomerStatus.objects.filter( restaurant=restaurant).filter( customer__user__id__isnull=False).order_by( 'avg_rating')) # customer_info = [] # for registered_customer in registered_customers: # info_entry = {} # info_entry['registered_customer'] = registered_customer # complaintcount = len(Order.objects.filter(restaurant=restaurant).filter(customer=customer).filter(customerrating__lte='2')) # info_entry['complaintcount'] = complaintcount # customer_info.append(info_entry) context = { 'registered_customers': registered_customers, } return render(request, 'manager/customers.html', context=context) elif request.method == 'GET': registered_customers = list( CustomerStatus.objects.filter( restaurant=restaurant).filter( customer__user__id__isnull=False).order_by( 'avg_rating')) # customer_info = [] # for registered_customer in registered_customers: # info_entry = {} # info_entry['registered_customer'] = registered_customer # complaintcount = len(Order.objects.filter(restaurant=restaurant).filter(customer=customer).filter(customerrating__lte='2')) # info_entry['complaintcount'] = complaintcount # customer_info.append(info_entry) context = { 'registered_customers': registered_customers, # 'customer_info': customer_info } return render(request, 'manager/customers.html', context=context) else: return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def staffdetails(request, pk): try: user = request.user userIs = userTypeChecker(user) if userIs(Manager) == True: staff_user = User.objects.get(pk=pk) staff = Staff.objects.get(user=staff_user) staffIs = userTypeChecker(staff_user) if request.method == 'POST': body = parse_req_body(request.body) task = body['task'] action = body['action'] if task == 'status_change' and action == 'remove_warning': if staff.warnings > 1: staff.warnings -= 1 elif task == 'status_change' and action == 'fire': if staffIs(Cook): # check if there is enough cooks if len( Cook.objects.filter( restaurant=restaurant).filter( status='H')) > 2: staff.status = 'N' staff.restaurant = None staff.warnings = 0 staff.salary = 0 elif staffIs(Salesperson ): # check if there is enough salespeople if len( Salesperson.objects.filter( restaurant=restaurant).filter( status='H')) > 2: staff.status = 'N' staff.restaurant = None staff.warnings = 0 staff.salary = 0 else: print('Not enough staff') elif task == 'status_change' and action == 'edit_salary': salary = body['salary'] staff.salary = salary staff.save() if staffIs(Cook): staff_type = "cook" complaints = Order_Food.filter( order__restaurant=restaurant).filter( food__cook=staff).filter(food_complaint__isnull=False) orders = Order_Food.filter( order__restaurant=restaurant ).filter(food__cook=staff).order_by( '-order__created') #not sure if order_by allows this elif staffIs(Deliverer): staff_type = "deliverer" complaints = Order.objects.filter( restaurant=restaurant).filter(deliverer=staff).filter( delivery_complaint__isnull=False) orders = Order.objects.filter(restaurant=restaurant).filter( deliverer=staff).order_by('-created') elif staffIs(Salesperson): staff_type = "salesperson" complaints = SupplyOrder.objects.filter( restaurant=restaurant).filter(salesperson=staff).filter( supply_complaint__isnull=False) orders = SupplyOrder.objects.filter( restaurant=restaurant).filter( salesperson=staff).order_by('-created') context = { 'staff': staff, 'staff_type': staff_type, 'complaints': complaints, 'orders': orders, } return render(request, 'manager/staffdetails.html', context=context) else: return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def staff(request): #removewarnings, #view applications try: user = request.user userIs = userTypeChecker(user) if userIs(Manager) == True: restaurant = Restaurant.objects.get(manager__user=user) if request.method == 'POST': body = parse_req_body(request.body) staff_id = body[ 'user_id'] #this is a user object, COULD CAUSE ERRORS print(body) staff_user = User.objects.get(id=staff_id) staffIs = userTypeChecker(staff_user) task = body['task'] action = body['action'] if task == 'cook_status_change': update_staff = Cook.objects.get(user__id=staff_id) elif task == 'salesperson_status_change': update_staff = Salesperson.objects.get(user__id=staff_id) elif task == 'deliverer_status_change': update_staff = Deliverer.objects.get(user__id=staff_id) if action == 'Remove 1 Warning': if update_staff.warnings > 1: update_staff.warnings -= 1 elif action == 'Fire': if staffIs(Cook): # check if there is enough cooks if len(Cook.objects.filter(restaurant=restaurant)) > 2: update_staff.status = 'N' update_staff.restaurant = None update_staff.warnings = 0 update_staff.salary = 0 elif staffIs(Salesperson ): # check if there is enough salespeople if len( Salesperson.objects.filter( restaurant=restaurant)) > 2: update_staff.status = 'N' update_staff.restaurant = None update_staff.warnings = 0 update_staff.salary = 0 else: update_staff.status = 'N' update_staff.restaurant = None update_staff.warnings = 0 update_staff.salary = 0 elif action == 'Edit Salary': salary = body['salary'] update_staff.salary = salary update_staff.save() cooks = Cook.objects.filter(restaurant=restaurant, status='H') salespeople = Salesperson.objects.filter(restaurant=restaurant, status='H') deliverers = Deliverer.objects.filter(restaurant=restaurant, status='H') context = { 'cooks': cooks, 'salespeople': salespeople, 'deliverers': deliverers, } return render(request, 'manager/staff.html', context=context) elif request.method == 'GET': cooks = Cook.objects.filter(restaurant=restaurant, status='H') salespeople = Salesperson.objects.filter(restaurant=restaurant, status='H') deliverers = Deliverer.objects.filter(restaurant=restaurant, status='H') context = { 'cooks': cooks, 'salespeople': salespeople, 'deliverers': deliverers, } return render(request, 'manager/staff.html', context=context) else: return redirect('home-nexus') except: import traceback traceback.print_exc() return redirect('home-nexus')
def signup(request): """ signup """ states = [choice[1] for choice in Address.STATE_CHOICES] error = None if (request.method == "POST"): body = parse_req_body(request.body) addr_str_accept = [] for string in [ body['staddr'], body['city'], body['state'], body['zipcode'] ]: word_acceptable = False for char in string: if char != ' ': word_acceptable = True break addr_str_accept.append(word_acceptable) addr_acceptable = False in addr_str_accept addr_acceptable = not addr_acceptable rest_acceptable = False for char in body['restname']: if char != ' ': rest_acceptable = True break if (body['usertype'] == 'cust'): if addr_acceptable: new_user = User.objects.create_user( body['usrname'], body['email'].replace('%', '@'), body['psw']) new_customer = Customer(user=new_user) new_customer.save() new_customer_address = CustomerAddress( street_address=body['staddr'], apt=body['apt'], city=body['city'], state=body['state'], zip_code=int(body['zipcode']), customer=new_customer) new_customer_address.save() else: error = 'must enter address' return render(request, 'home/signup.html', { 'states': states, 'error': error }) elif (body['usertype'] == 'mang'): if addr_acceptable and rest_acceptable: new_user = User.objects.create_user( body['usrname'], body['email'].replace('%', '@'), body['psw']) new_manager = Manager(user=new_user) new_manager.save() new_resturant = Restaurant(name=body['restname'], manager=new_manager) new_resturant.save() new_resturant_address = RestaurantAddress( street_address=body['staddr'], apt=body['apt'], city=body['city'], state=body['state'], zip_code=body['zipcode'], restaurant=new_resturant) new_resturant_address.save() else: error = 'must enter address and restaurant name' return render(request, 'home/signup.html', { 'states': states, 'error': error }) elif (body['usertype'] == 'cook'): new_user = User.objects.create_user( body['usrname'], body['email'].replace('%', '@'), body['psw']) new_staff = Cook(user=new_user) new_staff.save() elif (body['usertype'] == 'salespers'): new_user = User.objects.create_user( body['usrname'], body['email'].replace('%', '@'), body['psw']) new_staff = Salesperson(user=new_user) new_staff.save() elif (body['usertype'] == 'deliverer'): new_user = User.objects.create_user( body['usrname'], body['email'].replace('%', '@'), body['psw']) new_staff = Deliverer(user=new_user) new_staff.save() else: error = 'must pick a user type' return render(request, 'home/signup.html', { 'states': states, 'error': error }) response = redirect('home-nexus') return response else: return render(request, 'home/signup.html', { 'states': states, 'error': error })
def home(request): my_user = None # makes sure user is deliverer print('cook home') try: my_user = request.user userIs = userTypeChecker(my_user) if userIs(user.Cook) != True: print('user not cook') response = redirect('home-nexus') return response except Exception as e: print(e) response = redirect('home-nexus') return response except: print('exception occured') response = redirect('home-nexus') return response my_cook = user.Cook.objects.get(user=my_user) registered = len( user.Cook.objects.filter(user=my_user).exclude( restaurant__isnull=True)) > 0 and my_cook.status == 'H' if registered != True: # if not registered print('cook not registered') return redirect('cook-register') if request.method == "POST": body = parse_req_body(request.body) task = body['task'] if task == 'deletefood': deletefood(my_cook, body) elif task == 'addfood': addfood(my_cook, body) elif task == 'ratesupply': supply_rate(my_cook, body) elif task == 'requestsupply': supply_request(my_cook, body) elif task == 'finishorder': finishorder(my_cook, body) cookfood = restaurant.Food.objects.filter(cook=my_cook) supplyorders = restaurant.SupplyOrder.objects.filter(cook=my_cook) warnings = my_cook.warnings orders = restaurant.Order.objects.filter(restaurant=my_cook.restaurant) order_data = [] for order in orders: data_entry = { 'order': order, 'description': order.food_description, 'finished': len( restaurant.Order_Food.objects.filter(order=order).filter( food__cook=my_cook).filter(isFinished=True)) > 0 } order_data.append(data_entry) # print(supplyorders) context = { 'cookfood': cookfood, 'supplyorders': supplyorders, 'warnings': warnings, 'orderData': order_data, } return render(request, 'cook/home.html', context=context)