def editTool(request, tool_id): """ Edits the tool's information obtained by the POST from editToolForm. form: form obtained by tool_id """ if request.user.is_authenticated(): if request.user.id == Tool.objects.get(pk=tool_id).owner_id: tool = Tool.objects.get(pk=tool_id) form = ToolForm(request.POST, instance=Tool.objects.get(pk=tool_id)) if tool.availability == 1: if form.is_valid(): form.save() tool = Tool.objects.get(pk=tool_id) setattr(tool, 'shed_string', Shed.objects.get(pk=tool.shed_id).name) #Sets the string name of the tool's shed setattr(tool, 'image', form.cleaned_data['image']) tool.save() messages.success(request, 'The tool has been edited.') return redirect('/../tools/viewTools') else: messages.error(request, 'The information you have entered is invalid.') return redirect('tools:editToolForm', tool_id) else: messages.error(request, 'Cannot edit tool. Tool is either in request or is unavailable.') return redirect('tools:editToolForm', tool_id) else: messages.error(request, 'You can only edit tools that you own.') return redirect('/../tools/viewTools/') #If the tool does not belong to the user. PLACEHOLDER else: return HttpResponseRedirect('/login') #If the user is not logged in. PLACEHOLDER
def addTool(request): """ This is the function that the addToolForm calls when a form is submitted. The new tool is added to the Tool database. Takes user back to the tool list. tool: new tool to be created from the information obtained from POST """ if request.user.is_authenticated(): #If user is logged in form = ToolForm(request.POST, request.FILES) tool_list = Tool.objects.all() if form.is_valid(): tool = Tool.objects.create( name=form.cleaned_data['name'], owner_id=request.user.id, owner_string=Owner.objects.get(pk=request.user.id).fname + ' ' + Owner.objects.get(pk=request.user.id).lname, borrower_id=0, borrower_string='None', availability=1, shed_id = form.cleaned_data['shed_id'], shed_string = Shed.objects.get(pk=form.cleaned_data['shed_id']).name, zipcode = Shed.objects.get(pk=form.cleaned_data['shed_id']).zipcode, description=form.cleaned_data['description'], creation_date = date.today(), return_date_limit = form.cleaned_data['return_date_limit'], return_date = '2001-01-01', usage_count = 0, image = form.cleaned_data['image'], ) #return render(request, 'tools/addTool.html', context) context = RequestContext(request, { 'tool_list': tool_list, }) messages.success(request, 'The Tool has been added.') return render(request, 'tools/viewTools.html', context) else: messages.error(request, 'There is invalid information. Please double-check all inputs.') shed_list = Shed.objects.filter(zipcode=request.user.zipcode) form = ToolForm() context = ({'title': 'Add Tool', 'form': form, 'shed_list': shed_list, }) return render(request, 'tools/addToolForm.html', context) else: #If not logged in, redirect to login page return HttpResponseRedirect('/login')
def make_form(tool, request=None): """ Instantiate one form, based on the request and the tool :param tool: :param request: the current post request :return: an instanciated and initialized form """ tool_inputs_details = tool.fetch_tool_json() tool_field_white_list, created = ToolFieldWhiteList.objects.get_or_create( tool=tool, context="w") formname = str(slugify(tool.name).title().replace('-', '')) + 'Form' prefix = formname.lower() # If not post, it means that we want to create a form with # default values formdata = None if request is not None and request.POST: formdata = request.POST return ToolForm(data=formdata, prefix=prefix, tool_params=tool_inputs_details.get('inputs'), tool_id=tool.id_galaxy, tool_name=tool.name, whitelist=tool_field_white_list.saved_params, fields_ids_mapping={}, n=0)