def add_owner(): form = OwnerForm() if form.validate_on_submit(): name = form.name.data pup_id = form.id.data new_owner = Owner(name, pup_id) db.session.add(new_owner) db.session.commit() return redirect(url_for('list_pup')) return render_template('owners.html', form=form)
def add_owner(): form = OwnerForm() if form.validate_on_submit(): owner = Owner(form.owner_name.data,form.puppy_id.data) db.session.add(owner) db.session.commit() flash(f"Owner {owner.name} has been registered") return redirect(url_for('list_pup')) return render_template('add_owner.html',form=form)
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) data = serializer.data for d in data: d["authors"] = ", ".join([a["name"] for a in d["authors"]]) out_data = {} out_data["res_types"] = ResourceTypeForm() out_data["owners"] = OwnerForm() out_data["data"] = data return self.get_paginated_response(out_data) serializer = self.get_serializer(queryset, many=True) # print serializer.data data = serializer.data for d in data: d["authors"] = ", ".join([a["name"] for a in d["authors"]]) return Response({"results": serializer.data})
def post(self, request, property_id, *args, **kwargs): if request.is_ajax(): property = Property.objects.get(id=property_id) property_form = PropertyForm(request.POST, instance=property) owner_form = OwnerForm(request.POST, instance=property.owner) project_form = ProjectForm(request.POST, instance=property.project) permission_form = PermissionForm(request.POST) tower_form = TowerFormset(request.POST, files=request.FILES, instance=property.project) tower_helper = TowerHelper() builder_form = DeveloperForm( request.POST, instance=Property.objects.get(id=property_id).developer ) builder_project_formset = DeveloperProjectFormset( request.POST, instance=Property.objects.get(id=property_id).developer ) developer_project_helper = DeveloperProjectHelper() other_details_form = OtherDetailsForm(request.POST, instance=property) if 'property-details' in request.POST: if property_form.is_valid(): property_form.save() form_html = render_crispy_form(property_form) return JsonResponse({'success': 'true', 'form_html': form_html}) else: form_html = render_crispy_form(property_form) return JsonResponse({'success': 'false', 'form_html': form_html}) if 'owner-details' in request.POST: if owner_form.is_valid(): owner_form.save(property_id) form_html = render_crispy_form(owner_form) return JsonResponse({'success': 'true', 'form_html': form_html}) else: form_html = render_crispy_form(owner_form) return JsonResponse({'success': 'false', 'form_html': form_html}) if 'project-details' in request.POST: if project_form.is_valid() and permission_form.is_valid() and \ tower_form.is_valid(): project = project_form.save(property_id) permission_form.save(project=project) tower_form.save() project_form_html = render_crispy_form(project_form) permission_form_html = render_crispy_form(permission_form) tower_form_html = render_crispy_form(tower_form, tower_helper) form_html = (project_form_html + permission_form_html + tower_form_html) return JsonResponse({'success': 'true', 'form_html': form_html}) else: project_form_html = render_crispy_form(project_form) permission_form_html = render_crispy_form(permission_form) tower_form_html = render_crispy_form(tower_form, tower_helper) form_html = (project_form_html + permission_form_html + tower_form_html) return JsonResponse({'success': 'false', 'form_html': form_html}) if 'builder-details' in request.POST: if builder_form.is_valid() and \ builder_project_formset.is_valid(): builder_form.save() builder_project_formset.save() builder_form_html = render_crispy_form(builder_form) builder_project_formset_html = render_crispy_form( builder_project_formset, developer_project_helper) form_html = builder_form_html + builder_project_formset_html return JsonResponse({'success': 'true', 'form_html': form_html}) else: form_html = render_crispy_form(builder_form) return JsonResponse({'success': 'false', 'form_html': form_html}) if 'add-tower' in request.POST: post_data = request.POST.copy() post_data['tower_set-TOTAL_FORMS'] = int( request.POST['tower_set-TOTAL_FORMS']) + 1 project = Property.objects.get(id=property_id).project tower_form = TowerFormset(post_data, instance=project) tower_helper = TowerHelper() tower_form_html = render_crispy_form(tower_form, tower_helper) print tower_form.errors form_html = tower_form_html return JsonResponse({'succcess': 'true', 'form_html': form_html}) if 'add-project' in request.POST: developer = Developer.objects.all().first() builder_form = DeveloperForm( initial={ 'number_of_projects': request.POST['number_of_projects'] } ) builder_form_html = render_crispy_form(builder_form) post_data = request.POST.copy() post_data['developerproject_set-TOTAL_FORMS'] = int( request.POST['developerproject_set-TOTAL_FORMS']) + 1 builder_project_formset = DeveloperProjectFormset( post_data, instance=developer ) developer_project_helper = DeveloperProjectHelper() builder_project_formset_html = render_crispy_form( builder_project_formset, developer_project_helper) form_html = builder_form_html + builder_project_formset_html return JsonResponse({'success': 'true', 'form_html': form_html}) if 'other-details' in request.POST: if other_details_form.is_valid(): other_details_form.save() form_html = render_crispy_form(other_details_form) return JsonResponse({'success': 'true', 'form_html': form_html}) else: form_html = render_crispy_form(other_details_form) return JsonResponse({'success': 'true', 'form_html': form_html})