def onReturnToolRequest(request, toolid): requested_tool = ActiveTransactions.objects.get(tool=toolid) requested_tool.is_set_for_return = True tool = Tool.objects.get(id=toolid) tool_return_date = getattr(requested_tool, "return_date") if tool_return_date != datetime.now(): requested_tool.return_date = datetime.now() # requested_tool.notification_info = 'requestToolReturn' requested_tool.save() if requested_tool.tool.location == 'Home': notification_type = Notification.objects.get(code="TR") Notification_User(tool_name=requested_tool.tool, user=requested_tool.owner_id, notification=notification_type).save() else: print('tool is from the shed') shed_coordinators = UserProfile.objects.filter(is_coordinator=True) for i in shed_coordinators: print(i.user_id) current_user = UserProfile.objects.get(user_id=request.user) print(current_user.user_id) try: #the user who is returning the tool is a shed coordinator? UserProfile.objects.filter(is_coordinator=True).get( user_id=current_user.user_id) print('shed coordinator is borrower of the tool') toolHistory = ToolHistory( tool_id=tool, borrower_id=request.user, owner_id=tool.tool_owner_id, transaction_date=datetime.now(), condition='Like New', transaction_type='Returned', owner_comments='', return_date=datetime.now(), ) tool.is_borrowed = False tool.condition = 'Like New' toolHistory.save() tool.save() requested_tool.delete() messages.success(request, 'Tool has been successfully returned.') return HttpResponseRedirect('/displayShedTools') except: print('in except of the get for shed_coordinators') notification_type = Notification.objects.get(code="CVR") for coordinator in shed_coordinators: Notification_User(tool_name=requested_tool.tool, user=coordinator.user_id, notification=notification_type).save() messages.success(request, 'A return request has been sent successfully') return HttpResponseRedirect('/borrowedTools')
def onApproveReturn(request, toolid): context_instance = default_context(request) requested_tool = ActiveTransactions.objects.get(tool=toolid) tool = Tool.objects.get(id=toolid) tool_owner = User.objects.get(id=tool.tool_owner_id.id) borrower = getattr(requested_tool, "borrower_id") return_date = getattr(requested_tool, "return_date") notification_type = Notification.objects.get(code="VR") print(request.method) form = TransactionCompletionForm() if request.method == 'POST': form = TransactionCompletionForm(request.POST) print('inside post') if form.is_valid(): print('form is valid') print(borrower) print(request.user) print(form.data['condition']) toolHistory = ToolHistory( tool_id=tool, borrower_id=borrower, owner_id=tool_owner, transaction_date=datetime.now(), condition=form.data['condition'], transaction_type='Returned', owner_comments=form.data['message'], return_date=return_date, ) tool.is_borrowed = False tool.condition = form.data['condition'] toolHistory.save() tool.save() Notification_User(notification=notification_type, tool_name=tool, user=borrower).save() requested_tool.delete() messages.success(request, 'Tool return has been approved') if tool.location == 'Shed': return HttpResponseRedirect('/displayShedTools') else: return HttpResponseRedirect('/lentTools') updateNotifications(request, context_instance) context_instance['tool'] = tool context_instance['form'] = form context_instance['tool_owner'] = tool_owner return render(request, 'app/toolreturn.html', context_instance)
def onAcceptToolRequest(request, toolid): print('In accept request') requested_tool = ActiveTransactions.objects.get(tool=toolid) requested_tool.is_request_approved = True requested_tool.save() notification_type = Notification.objects.get(code='AR') borrower = getattr(requested_tool, "borrower_id") borrower_id = getattr(borrower, "id") return_date = getattr(requested_tool, "return_date") # update tool with tool_id borrower ID tool = Tool.objects.get(id=toolid) tool.save() toolHistory = ToolHistory( tool_id=tool, borrower_id=borrower, owner_id=request.user, transaction_date=datetime.now(), condition=tool.condition, transaction_type='Borrowed', owner_comments='Request has been accepted.', return_date=return_date, borrower_message=requested_tool.borrower_message, ) toolHistory.save() # update the notifications in ActiveTransactions model accepted_tool = ActiveTransactions.objects.get(tool=toolid) Notification_User(notification=notification_type, user=borrower, tool_name=tool).save() # accepted_tool.notification_info = 'acceptToolRequest' accepted_tool.save() if tool.location != 'Shed': messages.success(request, 'The tool request has been accepted') return HttpResponseRedirect('/borrowRequests')
def onAcceptToolRequest(request, toolid): print('In accept request') requested_tool = ActiveTransactions.objects.get(tool=toolid) requested_tool.is_request_approved = True requested_tool.save() notification_type = Notification.objects.get(code='AR') borrower = getattr(requested_tool, "borrower_id") borrower_id = getattr(borrower, "id") return_date = getattr(requested_tool, "return_date") # update tool with tool_id borrower ID tool = Tool.objects.get(id=toolid) tool.save() toolHistory = ToolHistory(tool_id=tool, borrower_id=borrower, owner_id=request.user, transaction_date=datetime.now(), condition=tool.condition, transaction_type='Borrowed', owner_comments='Request has been accepted.', return_date=return_date, borrower_message=requested_tool.borrower_message, ) toolHistory.save() # update the notifications in ActiveTransactions model accepted_tool = ActiveTransactions.objects.get(tool=toolid) Notification_User(notification=notification_type,user=borrower,tool_name=tool).save() #accepted_tool.notification_info = 'acceptToolRequest' accepted_tool.save() if tool.location != 'Shed': messages.success(request, 'The tool request has been accepted') return HttpResponseRedirect('/registeredTools')
def onApproveReturn(request, toolid): requested_tool = ActiveTransactions.objects.get(tool=toolid) tool = Tool.objects.get(id=toolid) tool_owner = User.objects.get(id=tool.tool_owner_id.id) borrower = getattr(requested_tool, "borrower_id") return_date = getattr(requested_tool, "return_date") notification_type = Notification.objects.get(code="VR") tool.is_borrowed = False print(request.method) form = TransactionCompletionForm() if request.method == 'POST': form = TransactionCompletionForm(request.POST) print('inside post') if form.is_valid(): print('form is valid') toolHistory = ToolHistory(tool_id=tool, borrower_id=borrower, owner_id=request.user, transaction_date=datetime.now(), condition=form.data['condition'], transaction_type='Returned', owner_comments=form.data['message'], return_date=return_date, ) tool.condition = form.data['condition'] toolHistory.save() Notification_User(notification=notification_type, tool_name=tool, user=borrower).save() requested_tool.delete() messages.success(request, 'Tool return has been approved') return HttpResponseRedirect('/') tool.save() return render(request, 'app/toolreturn.html', {'tool': tool, 'form': form, 'tool_owner': tool_owner})