def edit_building(request, areacode=None, locationid=None, buildingid=None): logger = logging.getLogger('webapp') logger.info('run edit_building run') if(areacode and locationid and buildingid): area = Area.objects.get(id=int(areacode)) l = Location.objects.get(id=int(locationid)) b = Building.objects.get(id=int(buildingid)) if request.method == 'POST': #update record with submitted values form = BuildingForm(request.POST, instance=b) if form.is_valid(): b.name = form.cleaned_data['name'] b.address = form.cleaned_data['address'] b.zipcode = form.cleaned_data['zipcode'] b.phone = form.cleaned_data['phone'] b.cellphone = form.cleaned_data['cellphone'] b.adminEmail = form.cleaned_data['adminEmail'] b.save() return HttpResponseRedirect('/area/' + areacode + '/location/' + locationid + '/buildings') return render(request, 'area/building_detail.html', {'form': form, 'action':'/area/' + areacode + '/location/' + locationid + '/building/' + buildingid + '/', 'http_method':'POST', 'area': area, 'location': l}) else: #load record to allow edition form = BuildingForm(instance=b) return render(request, 'area/building_detail.html', {'form': form, 'action':'/area/' + areacode + '/location/' + locationid + '/building/' + buildingid + '/', 'http_method':'POST', 'area': area, 'location': l}) else: return HttpResponseRedirect('/area/' + areacode + '/location/' + locationid + '/buildings') if areacode and locationid else HttpResponseRedirect('/areas/')
def handle_building(request, areacode=None, locationid=None): logger = logging.getLogger('webapp') logger.info('run handle_building run') area = Area.objects.get(id=int(areacode)) if request.method == 'POST': form = BuildingForm(request.POST) if form.is_valid(): b = Building() b.name = form.cleaned_data['name'] b.address = form.cleaned_data['address'] b.zipcode = form.cleaned_data['zipcode'] b.phone = form.cleaned_data['phone'] b.cellphone = form.cleaned_data['cellphone'] b.adminEmail = form.cleaned_data['adminEmail'] location = Location.objects.get(id=int(locationid)) location.building_set.add(b) return HttpResponseRedirect('/area/' + areacode + '/location/' + locationid + '/buildings') else: form = BuildingForm() return render(request, 'area/building_detail.html', {'form': form, 'action':'/area/' + areacode + '/location/' + locationid + '/building/', 'http_method':'POST', 'area': area})