Esempio n. 1
0
def editmachine(request, machine_id):
	machine = get_object_or_404(Machine,pk=machine_id)
	machine_user_id = machine.created_by_id
	logged_in_user_id = request.user.id

	if machine_user_id != logged_in_user_id:
		raise Http404
	machine.updated_at = datetime.datetime.now()
	if request.POST:
		form = MachineForm(request.POST, instance=machine)
	
		if form.is_valid():
			p = form.save(commit=False)
			p.updated_by = request.user
			
		try:		
			p.save()
		except IntegrityError:
			form.errors["machine_name"] = ["You cannot register more machines with same name"]			

			#In case of edit this extra row was needed for the validation comparing with the validation of the create form. I do not know why yet.
			return render(request,'approba/editmachine.html',{'form': form},)
		else:		
			return HttpResponseRedirect(reverse('approba:machinelist'))
	else:
		form = MachineForm(instance=machine)	
		return render(request,'approba/editmachine.html',{'form': form},)
Esempio n. 2
0
def newmachine(request):
	"""Add new machine"""
	if request.method == 'POST':
		form = MachineForm(request.POST)
		
		if form.is_valid():
			p = form.save(commit=False)
			p.created_by = request.user
			p.updated_by = p.created_by

			try:
				p.save()
			except IntegrityError:
				form.errors["machine_name"] = ["You cannot register more machines with same name"]
			else:
				return HttpResponseRedirect(reverse('approba:machinelist'))
	else:
		form = MachineForm()
	return render(request,'approba/newmachine.html',{'form': form},)