def script_item_editor(request): # This is a regular non-CBV view because the way it processes the data it received # would be more awkward to do in a CBV. request.POST = dict(request.POST.items()) # Make it mutable init_data_json = request.POST.pop("init_data") init_data = json.loads(init_data_json) item_class = {"action": Action, "condition": Condition}[init_data["itemType"]] form = ScriptItemEditForm( script_item=item_class.unserialize(init_data["data"], validate=False), event_class=Event.class_for_identifier(init_data["eventIdentifier"]), data=(request.POST if request.POST else None), files=(request.FILES if request.FILES else None) ) form.initial = form.get_initial() context = { "form": form, "script_item": form.script_item, "event_class": form.event_class, "init_data": init_data_json, } if form.data and form.is_valid(): try: form.save() except ValidationError as verr: form.add_error(None, verr) else: context["post_message"] = {"new_data": form.script_item.data} # Unbind so we'll use the initial data form.is_bound = False form.data = {} form.initial = form.get_initial() return render(request, "notify/admin/script_item_editor.jinja", context)
def script_item_editor(request): # This is a regular non-CBV view because the way it processes the data it received # would be more awkward to do in a CBV. request.POST = dict(request.POST.items()) # Make it mutable init_data_json = request.POST.pop("init_data") init_data = json.loads(init_data_json) item_class = { "action": Action, "condition": Condition }[init_data["itemType"]] form = ScriptItemEditForm( script_item=item_class.unserialize(init_data["data"], validate=False), event_class=Event.class_for_identifier(init_data["eventIdentifier"]), data=(request.POST if request.POST else None), files=(request.FILES if request.FILES else None)) form.initial = form.get_initial() context = { "form": form, "script_item": form.script_item, "event_class": form.event_class, "init_data": init_data_json, } if form.data and form.is_valid(): try: form.save() except ValidationError as verr: form.add_error(None, verr) else: context["post_message"] = {"new_data": form.script_item.data} # Unbind so we'll use the initial data form.is_bound = False form.data = {} form.initial = form.get_initial() return render(request, "notify/admin/script_item_editor.jinja", context)
def event_class(self): return Event.class_for_identifier(self.event_identifier)