def edit_tool(request, tool_id, template_name='tools/edit_tool.html'): """ Update tool info with data from request """ tool = get_object_or_404(Tool, pk=tool_id) if request.user == tool.owner: if request.method == 'POST': if not request.POST.get( 'shed'): # set tool to previous shed if none selected request.POST = request.POST.copy() request.POST['shed'] = tool.shed.id form = ToolForm(request.user, request.POST, request.FILES, instance=tool) if form.is_valid(): form.save() url = reverse('tool_detail', kwargs={'tool_id': tool.id}) return HttpResponseRedirect(url) else: form = ToolForm(user=request.user, instance=tool) return render(request, template_name, { 'form': form, 'tool': tool }) #no editing done else: url = reverse('tool_detail', kwargs={'tool_id': tool.id}) return HttpResponseRedirect(url)
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 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 edit_tool(request, tool_id, template_name='tools/edit_tool.html'): """ Update tool info with data from request """ tool = get_object_or_404(Tool, pk=tool_id) if request.user == tool.owner: if request.method == 'POST': if not request.POST.get('shed'): # set tool to previous shed if none selected request.POST = request.POST.copy() request.POST['shed'] = tool.shed.id form = ToolForm(request.user, request.POST, request.FILES, instance=tool) if form.is_valid(): form.save() url = reverse('tool_detail', kwargs={'tool_id':tool.id}) return HttpResponseRedirect(url) else: form = ToolForm(user=request.user, instance=tool) return render(request, template_name, {'form':form, 'tool':tool}) #no editing done else: url = reverse('tool_detail', kwargs={'tool_id':tool.id}) return HttpResponseRedirect(url)