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_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)