def edit(request, template_name='tickets/edit.html'): agent = get_object_or_404(Agent) if request.method == 'POST': form = AgentForm(request.POST, instance=agent) contacts = ContactFormSet(request.POST, instance=agent) success = False errors = [] if form.is_valid(): form.save() if contacts.is_valid(): contacts.save() success = True else: errors = [(key, unicode(value[0])) for key, value in contacts.errors.items()] else: errors = [(key, unicode(value[0])) for key, value in form.errors.items()] if success and not request.is_ajax(): return HttpResponseRedirect(urlresolvers.reverse('tickets_home')) json_result = simplejson.dumps(({ 'success' : success, 'errors' : errors })) return HttpResponse(json_result, mimetype='application/javascript') else: form = AgentForm(instance=agent, label_suffix=':') contacts = ContactFormSet(instance=agent) return render_to_response(template_name, context_instance=RequestContext(request, { 'form' : form, 'contacts' : contacts, 'agent' : agent, 'page_title' : "About My Self - Edit" }))
def test_reverse_fields(self): form_fields = AgentForm().fields.keys() original_fields = AgentForm.Meta.fields form_fields.reverse() self.assertEquals(form_fields, original_fields)