def my_ajax_view(request): csrf_middleware = CsrfViewMiddleware() response_data = {} if request.method == 'GET': # Get form definition form = ProjectForm() elif request.raw_post_data: request.POST = json.loads(request.raw_post_data) # Process request for CSRF csrf_middleware.process_view(request, None, None, None) form_data = request.POST.get('data', {}) form = ProjectForm(form_data) if form.is_valid(): form.save() field_configuration = { 'include': ['title','duration'], } remote_form = RemoteForm(form, **field_configuration) # Errors in response_data['non_field_errors'] and response_data['errors'] response_data.update(remote_form.as_dict()) response = HttpResponse( json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json" ) # Process response for CSRF csrf_middleware.process_response(request, response) return response
def edit_project(): form = ProjectForm(request.form) if form.validate(): name = request.form["name"] id = request.form["id"] db.query(Project).filter(Project.id == id).update({"name": name}) db.commit() return redirect("admin")
def post(self, request, *args, **kwargs): form = ProjectForm(request.POST) # check whether it's valid: if form.is_valid(): form.instance.id = Project.objects.get(title=kwargs['project_title']).id form.save() return redirect(reverse('project:overview', kwargs={'project_title': request.POST.get('title')})) else: return HttpResponseBadRequest
def post(self, request): # create a form instance and populate it with data from the request: form = ProjectForm(request.POST, auto_id=True) # check whether it's valid: if form.is_valid(): form.save() form.instance.users = [request.user] group = Group.objects.get(name=OWNER_ROLE) user_project_role = UserProjectRole(role=group, user=request.user, project=form.instance) user_project_role.save() return redirect(reverse('user_projects')) else: return HttpResponseBadRequest
def project_new(request): if request.method == "POST": # If the form has been submitted form = ProjectForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass project = form.save() project.set_owner(request.user) # Process the data in form.cleaned_data return HttpResponseRedirect( reverse('project', args=(project.uuid,)) ) # Redirect after POST else: form = ProjectForm() # An unbound form return render_to_response("core/project_new.html", {'form': form}, context_instance=RequestContext(request))
def test_project_settings_form(self): user = User.objects.get(username='******') logged_in = self.client.login(username='******', password='******') self.assertTrue(logged_in) response = self.client.get(reverse('project:overview', kwargs={'project_title': 'Test'})) self.assertEquals(response.status_code, 200) self.assertTrue('project' in response.context) project = response.context['project'] # Form would be invalid because of uniqueness constraint # We know that it exists because we fetched it, so we can change its title project.title = 'New title' form = ProjectForm(data={'title': project.title, 'description': project.description, 'users': [user.id]}) self.assertTrue(form.is_valid(), form.errors)
def project_edit(request, uuid): project = get_object_or_404(Project, uuid=uuid) if not request.user.has_perm('core.change_project', project): return HttpResponseForbidden( custom_error_page(request, '403.html', { user: request.user, 'msg': "edit this project" })) if request.method == "POST": # If the form has been submitted... form = ProjectForm(data=request.POST, instance=project) # A form bound to the POST data if form.is_valid(): # All validation rules pass form.save() # Process the data in form.cleaned_data # ... return HttpResponseRedirect( reverse('core.views.project', args=(uuid, ))) # Redirect after POST else: form = ProjectForm(instance=project) # An unbound form return render_to_response("core/project_edit.html", { 'form': form, 'project': project }, context_instance=RequestContext(request))
def project_edit(request, uuid): project = get_object_or_404(Project, uuid=uuid) if not request.user.has_perm('core.change_project', project): return HttpResponseForbidden( custom_error_page(request, '403.html', {user: request.user, 'msg': "edit this project"}) ) if request.method == "POST": # If the form has been submitted # A form bound to the POST data form = ProjectForm(data=request.POST, instance=project) if form.is_valid(): # All validation rules pass form.save() # Process the data in form.cleaned_data return HttpResponseRedirect( reverse('core.views.project', args=(uuid,))) else: form = ProjectForm(instance=project) # An unbound form return render_to_response("core/project_edit.html", {'form': form, 'project': project}, context_instance=RequestContext(request))
def admin(): first = request.args.get("first-run") is not None newproject = NewProjectForm() projects = [ProjectForm(project=proj) for proj in Project.query.all()] unspecified = Donation.query.filter(Donation.project == None).all() donations = Donation.query.order_by( Donation.created.desc()).limit(50).all() return render_template( "admin.html", first=first, projects=projects, newproject=newproject, donations=donations, currency=currency, one_times=lambda p: sum( [d.amount for d in p.donations if d.type == DonationType.one_time]), recurring=lambda p: sum([ d.amount for d in p.donations if d.type == DonationType.monthly and d.active ]), recurring_ever=lambda p: sum([ d.amount * d.payments for d in p.donations if d.type == DonationType.monthly ]), unspecified_one_times=sum([ d.amount for d in unspecified if d.type == DonationType.one_time ]), unspecified_recurring=sum([ d.amount for d in unspecified if d.type == DonationType.monthly and d.active ]), unspecified_recurring_ever=sum([ d.amount * d.payments for d in unspecified if d.type == DonationType.monthly ]), total_one_time=sum([ d.amount for d in Donation.query.filter( Donation.type == DonationType.one_time) ]), total_recurring=sum([ d.amount for d in Donation.query.filter( Donation.type == DonationType.monthly, Donation.active == True) ]), total_recurring_ever=sum([ d.amount * d.payments for d in Donation.query.filter( Donation.type == DonationType.monthly) ]), uptodate=check_update(), )
def test_form_invalid(self): # Missing values form = ProjectForm(data={'title': 'Test'}) self.assertFalse(form.is_valid(), 'Project form should be invalid: %s' % form.errors) form = ProjectForm(data={'description': 'Test description'}) self.assertFalse(form.is_valid(), 'Project form should be invalid: %s' % form.errors) # Uniqueness constraint form = ProjectForm(data={'title': 'Test', 'description': 'Test description'}) self.assertFalse(form.is_valid(), 'Project form should be valid: %s' % form.errors)
def project_new(request): if request.method == "POST": # If the form has been submitted form = ProjectForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass project = form.save() project.set_owner(request.user) # Process the data in form.cleaned_data return HttpResponseRedirect( reverse('project', args=(project.uuid, ))) # Redirect after POST else: form = ProjectForm() # An unbound form return render_to_response("core/project_new.html", {'form': form}, context_instance=RequestContext(request))
def addProject(request): ''' The view manages Customer's project and its settings ''' if request.method == 'POST': form = ProjectForm(request.POST) if form.is_valid(): form = form.save(commit=False) form.owner = request.user form.save() return redirect("home") else: print(form.errors) print(form.non_field_errors) project_form = ProjectForm # project_form = ProjectForm(initial={'owner':request.user}) return render(request, 'project.html', {'form': project_form})
def test_form_valid(self): user = User.objects.get(username='******') form = ProjectForm(data={'title': 'Project', 'description': 'Project description', 'users': [user.id]}) self.assertTrue(form.is_valid(), 'Project form should be valid: %s' % form.errors)