Esempio n. 1
0
def test_notify_item_admin_form(rf, admin_user):
    event_class = ATestEvent
    script_item = SendEmail({
        "send_identifier": {"constant": "hello"},
        "recipient": {"constant": "*****@*****.**"},
        "language": {"constant": "en"},
    })
    send_data = {
            "b_recipient_c": "*****@*****.**",
            "b_language_c": "en",
            "b_message_c" : "Message",
            "b_send_identifier_c": "hello",
        }
    form = ScriptItemEditForm(
        event_class=event_class,
        script_item=script_item,
        data=send_data
    )
    initial = form.get_initial()
    assert initial["b_send_identifier_c"] == "hello"
    assert form.is_valid()
    form.save()
    assert script_item.data["recipient"] == {"constant": "*****@*****.**"}
    send_data["b_recipient_c"] = admin_user.pk
    send_data['init_data'] = json.dumps({"eventIdentifier":"order_received","itemType":"action","data":{"identifier":"add_notification"}})
    view = script_item_editor
    request = apply_request_middleware(rf.post("/", data=send_data), user=admin_user)
    response = view(request)
    assert response.status_code == 200 #  Assert no errors have occurred
Esempio n. 2
0
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": "*****@*****.**"
    }
Esempio n. 3
0
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": "*****@*****.**"}
Esempio n. 4
0
def test_notify_item_admin_form(rf, admin_user):
    event_class = ATestEvent
    script_item = SendEmail({
        "send_identifier": {
            "constant": "hello"
        },
        "recipient": {
            "constant": "*****@*****.**"
        },
        "language": {
            "constant": "en"
        },
    })
    send_data = {
        "b_recipient_c": "*****@*****.**",
        "b_language_c": "en",
        "b_message_c": "Message",
        "b_send_identifier_c": "hello",
    }
    form = ScriptItemEditForm(event_class=event_class,
                              script_item=script_item,
                              data=send_data)
    initial = form.get_initial()
    assert initial["b_send_identifier_c"] == "hello"
    assert form.is_valid()
    form.save()
    assert script_item.data["recipient"] == {
        "constant": "*****@*****.**"
    }
    send_data["b_recipient_c"] = admin_user.pk
    send_data['init_data'] = json.dumps({
        "eventIdentifier": "order_received",
        "itemType": "action",
        "data": {
            "identifier": "add_notification"
        }
    })
    view = script_item_editor
    request = apply_request_middleware(rf.post("/", data=send_data),
                                       user=admin_user)
    response = view(request)
    assert response.status_code == 200  #  Assert no errors have occurred
Esempio n. 5
0
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)
Esempio n. 6
0
def test_notify_item_admin_form(rf, admin_user):
    event_class = ATestEvent
    script_item = SendEmail({
        "send_identifier": {
            "constant": "hello"
        },
        "recipient": {
            "constant": "*****@*****.**"
        },
        "language": {
            "constant": "en"
        },
    })
    send_data = {
        "b_recipient_c": "*****@*****.**",
        "b_language_c": "en",
        "b_message_c": "Message",
        "b_send_identifier_c": "hello",
        "t_en_subject": "Welcome!",
        "t_ja_subject": "Konnichiwa!",
        "t_ja_body": "Bye",
        "t_en_content_type": "html",
    }
    form = ScriptItemEditForm(event_class=event_class,
                              script_item=script_item,
                              data=send_data)
    initial = form.get_initial()
    assert initial["b_send_identifier_c"] == "hello"
    assert not form.is_valid()  # Missing template body for default language
    with pytest.raises(forms.ValidationError):
        form.save()

    send_data.update({"t_en_body": "ok now this should pass"})
    form = ScriptItemEditForm(event_class=event_class,
                              script_item=script_item,
                              data=send_data)
    initial = form.get_initial()
    assert initial["b_send_identifier_c"] == "hello"
    assert form.is_valid()
    form.save()

    assert script_item.data["template_data"]["en"]["subject"] == "Welcome!"
    assert script_item.data["template_data"]["ja"]["subject"] == "Konnichiwa!"
    assert script_item.data["recipient"] == {
        "constant": "*****@*****.**"
    }
    send_data["b_recipient_c"] = admin_user.pk
    send_data["init_data"] = json.dumps({
        "eventIdentifier": "order_received",
        "itemType": "action",
        "data": {
            "identifier": "add_notification"
        }
    })
    view = script_item_editor
    request = apply_request_middleware(rf.post("/", data=send_data),
                                       user=admin_user)
    response = view(request)
    assert response.status_code == 200  #  Assert no errors have occurred
Esempio n. 7
0
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)