def policy_modify(request, policy_id=None): if request.method == "POST": policy = PolicyModel() policy_form = PolicyModelForm(request.POST) if policy_form.is_valid(): json = simplejson.dumps("Policy successfully updated.") else: errors = [] for field in policy_form: errors.append({ 'field' : field.html_name, 'error' : field.errors }) failed = { 'failure' : errors, 'id': policy_id } json = simplejson.dumps(failed) return HttpResponse(json)
def policy_create(request): if request.method == "POST": policy_form = PolicyModelForm(request.POST) if policy_form.is_valid(): policy = policy_form.save() context = { 'policy': {'policy': policy, 'policyForm': policy_form }} tpl = "celerymanagementapp/policy_instance.html" html = render_to_response(tpl, context, context_instance=RequestContext(request)) success = { 'success': 'Policy successfully created.', 'html': html.content, 'pk': policy.pk } json = simplejson.dumps(success) else: errors = [] for field in policy_form: errors.append({ 'field' : field.html_name, 'error' : field.errors }) failed = { 'failure' : errors } json = simplejson.dumps(failed) return HttpResponse(json)
def policy_modify(request, policy_id=None): if request.method == "POST": try: policy = PolicyModel.objects.get(pk=policy_id) except ObjectDoesNotExist: m = 'No Policy with the given ID ({0}) was found.'.format(policy_id) failed = { 'failure' : m, 'id': policy_id } json = simplejson.dumps(failed) else: policy_form = PolicyModelForm(request.POST, instance=policy) if policy_form.is_valid(): policy_form.save() json = simplejson.dumps("Policy successfully updated.") else: errors = [] for field in policy_form: errors.append({ 'field' : field.html_name, 'error' : field.errors }) failed = { 'failure' : errors, 'id': policy_id } json = simplejson.dumps(failed) return HttpResponse(json)
def policy_form_test(request): from django.template import Template html = '''\ <html> <head><title>Policy Form</title></head> <body> <form action="/celerymanagementapp/policy/test_form/" method="post"> {% csrf_token %} <table> <tr> <td>Name:</td> <td>{{form.name}}</td> <td>{{form.name.errors}}</td> </tr> <tr> <td>Source:</td> <td>{{form.source}}</td> <td><pre>{{form.source.errors}}</pre></td> </tr> </table> <input type="submit" value="Submit" /> </form> </body> ''' if request.method == 'POST': # If the form has been submitted... form = PolicyModelForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass return HttpResponse('success!') else: form = PolicyModelForm() # An unbound form t = Template(html) c = RequestContext(request, {'form': form}) return HttpResponse(t.render(c))