def create_tool(request, template_name='tools/create_tool.html'): """ Create a new tool with data from request and add it to database. """ if request.method == 'POST': form = ToolForm(request.user, request.POST, request.FILES) if form.is_valid(): # form.clean_image() # shed = request.user.shed_owned.all()[0] image = None if form.cleaned_data['image']: image = form.cleaned_data['image'] tool = Tool(name=form.cleaned_data['name'], category=form.cleaned_data['category'], description=form.cleaned_data['description'], owner=request.user, shed=form.cleaned_data['shed'], image=image, available=form.cleaned_data['available']) tool.save() if image: tool.image.name = rename_file(tool) tool.save() activity_msg = "Added %s to %s" % (tool.name, tool.shed.name) Activity.objects.create(user=request.user, message=activity_msg) url = reverse('tool_detail', kwargs={'tool_id': tool.id}) return HttpResponseRedirect(url) else: form = ToolForm(request.user) return render(request, template_name, {'form': form})
def create_tool_to_shed(request, shed_id, template_name='tools/create_tool.html'): """ Create a new tool with the specified shed selected by default in the form. """ shed = get_object_or_404(Shed, pk=shed_id) if request.method == 'POST': form = ToolForm(request.user, request.POST, request.FILES) image = None if form.is_valid(): if form.cleaned_data['image']: image = form.cleaned_data['image'] tool = Tool(name=form.cleaned_data['name'], category=form.cleaned_data['category'], description=form.cleaned_data['description'], owner=request.user, shed=shed, image=image, available=form.cleaned_data['available']) tool.save() if image: tool.image.name = rename_file(tool) tool.save() activity_msg = "Added %s to %s" % (tool.name, tool.shed.name) Activity.objects.create(user=request.user, message=activity_msg) url = reverse('tool_detail', kwargs={'tool_id': tool.id}) return HttpResponseRedirect(url) else: form = ToolForm(user=request.user, initial={'shed': shed}) return render(request, template_name, {'form': form})
def test_is_available_without_borrower(self): """ Tool.is_available() should return true if it has no borrower. """ tool = Tool(name="test", owner=self.profile1, borrower=None) self.assertEqual(tool.is_available(), True)
def test_tool_borrow_with_no_existing_borrower(self): """ borrow_tool should return false if the tool is already has a borrower """ tool = Tool(name="test", owner=self.profile1, borrower=None) self.assertEqual(tool.borrow_tool(self.profile2), True)
def test_is_available_with_borrower(self): """ Tool.is_available() should return false if it has a borrower. """ tool = Tool(name="test", owner=self.profile1, borrower=self.profile2) self.assertEqual(tool.is_available(), False)