Exemplo n.º 1
0
def hardwareEdit(request, id=None):
	user = request.user
	profile = user.get_profile()
	if user.email != "" and profile.mail_confirmed:
		if id==None:
			#Create new hardware
			if request.method == 'POST': # If the form has been submitted...
				form = HardwareForm(request.POST) # A form bound to the POST data
				if form.is_valid(): # All validation rules pass
					h = Hardware()
					h.name = form.cleaned_data['name']
					h.description = form.cleaned_data['description']
					h.condition = form.cleaned_data['condition']
					h.category = form.cleaned_data['category']
					h.state = get_object_or_404(State, id=form.cleaned_data['state'])
					h.owner = user
					if form.cleaned_data['ownlocation']:
						location = Location()
						location.city = form.cleaned_data['city']
						location.street = form.cleaned_data['street']
						location.postcode = form.cleaned_data['postcode']
						g = geocoders.Google()
						if location.city!= "" or location.street!="" or location.street!="":
							searchstring = location.street + ", " + location.postcode + " " + form.cleaned_data['city']
							try:
								places = g.geocode(urllib.quote(searchstring), exactly_one=False)
								location.latitude = places[0][1][0]
								location.longitude = places[0][1][1]
								location.save()
							except geocoders.google.GQueryError, e:
								messages.add_message(request, messages.ERROR, u"Es konnte kein Ort gefunden werden, der deiner Eingabe entspricht. Hast du dich vielleicht vertippt?")
								context = {'form':form, 'edit':False}
								return render_to_response('hardware/hardwareform.html', context, RequestContext(request))
							except:
								messages.add_message(request, messages.ERROR, u"Es gab einen Fehler beim überprüfen des Ortes. Hast du dich vielleicht vertippt?")
								context = {'form':form, 'edit':False}
								return render_to_response('hardware/hardwareform.html', context, RequestContext(request))
						else:
							location.latitude = None
							location.longitude = None
							location.save()
						h.location = location
					else:
						h.location = profile.location
					if h.state.temporary and form.cleaned_data['lendlength'] != None:
						h.lendlength = form.cleaned_data['lendlength']
						h.lendlengthtype = form.cleaned_data['lendlengthtype']

					h.save()
					messages.add_message(request, messages.SUCCESS, u"Deine Hardware wurde erfolgreich angelegt. Bitte füge noch ein paar Bilder hinzu.")
					context = {'form':form, 'hardware':h}
					return render_to_response('hardware/imageform.html', context, RequestContext(request))
Exemplo n.º 2
0
							location.latitude = None
							location.longitude = None
							location.save()
						h.location = location
					else:
						h.location = profile.location
					if h.state.temporary and form.cleaned_data['lendlength'] != None:
						h.lendlength = form.cleaned_data['lendlength']
						h.lendlengthtype = form.cleaned_data['lendlengthtype']

					h.save()
					messages.add_message(request, messages.SUCCESS, u"Deine Hardware wurde erfolgreich angelegt. Bitte füge noch ein paar Bilder hinzu.")
					context = {'form':form, 'hardware':h}
					return render_to_response('hardware/imageform.html', context, RequestContext(request))
			else:
				form = HardwareForm()

			context = {'form':form, 'edit':False}
			return render_to_response('hardware/hardwareform.html', context, RequestContext(request))
		else:
			#edit existing hardware
			hardware = get_object_or_404(Hardware, id=id)
			if user == hardware.owner:
				if request.method == 'POST':
					form = HardwareForm(request.POST) # A form bound to the POST data
					if form.is_valid():
						hardware.name = form.cleaned_data['name']
						hardware.description = form.cleaned_data['description']
						hardware.condition = form.cleaned_data['condition']
						hardware.category = form.cleaned_data['category']
						hardware.state = get_object_or_404(State, id=form.cleaned_data['state'])