def test_notify_item_admin_form(): event_class = ATestEvent script_item = SendEmail({ "send_identifier": { "constant": "hello" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "en" }, }) form = ScriptItemEditForm(event_class=event_class, script_item=script_item, data={ "b_recipient_c": "*****@*****.**", "b_language_c": "en", "b_send_identifier_c": "hello", }) initial = form.get_initial() assert initial["b_send_identifier_c"] == "hello" assert form.is_valid() form.save() assert script_item.data["recipient"] == { "constant": "*****@*****.**" }
def test_notify_item_admin_form(): event_class = TestEvent script_item = SendEmail({ "send_identifier": {"constant": "hello"}, "recipient": {"constant": "*****@*****.**"}, "language": {"constant": "en"}, }) form = ScriptItemEditForm( event_class=event_class, script_item=script_item, data={ "b_recipient_c": "*****@*****.**", "b_language_c": "en", "b_send_identifier_c": "hello", } ) initial = form.get_initial() assert initial["b_send_identifier_c"] == "hello" assert form.is_valid() form.save() assert script_item.data["recipient"] == {"constant": "*****@*****.**"}
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)