# Path: action/forms.py
# class SendEmailForm(forms.Form):
#     to_email = forms.CharField()
#     subject = forms.CharField()
#     message = forms.CharField()
# 
# class SendHTTPRequestForm(forms.Form):
#     http_method = forms.ChoiceField(choices=(
#         ("get", "GET"),
#         ("post", "POST"),
#         ("head", "HEAD"),
#         ("put", "PUT"),
#     ))
#     http_data = forms.CharField()
#     http_url = forms.URLField()


import requests
import json
from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings
from django.template import Template, Context
from .forms import SendEmailForm, SendHTTPRequestForm
            return (True, form.cleaned_data)
        return (False, form.errors)

    def action(self, recipe, **kwargs):
        raise NotImplementedError


class SendHTTPRequest(Action):
    template_name = "actions/send_http_request.html"
    form_class = SendHTTPRequestForm

    def action(self, recipe, **kwargs):
        http_fn = getattr(requests, kwargs["action"]["http_method"])
        nkwargs = {}
        context = Context(kwargs)
        if kwargs["action"]["http_method"] == "get":
            nkwargs["params"] = Template(kwargs["action"].get("http_data", "")).render(context)
        else:
            nkwargs["data"] = Template(kwargs["action"].get("http_data", "")).render(context)
        try:
            response = http_fn(kwargs["action"]["http_url"], **nkwargs)
        except requests.exceptions.RequestException:
            return False
        if not response.ok:
            return False
        return True


class SendEmail(Action):
    template_name = "actions/send_email.html"

# Path: action/forms.py
# class SendEmailForm(forms.Form):
#     to_email = forms.CharField()
#     subject = forms.CharField()
#     message = forms.CharField()
# 
# class SendHTTPRequestForm(forms.Form):
#     http_method = forms.ChoiceField(choices=(
#         ("get", "GET"),
#         ("post", "POST"),
#         ("head", "HEAD"),
#         ("put", "PUT"),
#     ))
#     http_data = forms.CharField()
#     http_url = forms.URLField()


# Path: action/actions.py
import requests
import json
from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings
from django.template import Template, Context
from .forms import SendEmailForm, SendHTTPRequestForm

            return (True, form.cleaned_data)
        return (False, form.errors)

    def action(self, recipe, **kwargs):
        raise NotImplementedError


class SendHTTPRequest(Action):
    template_name = "actions/send_http_request.html"
    form_class = SendHTTPRequestForm

    def action(self, recipe, **kwargs):
        http_fn = getattr(requests, kwargs["action"]["http_method"])
        nkwargs = {}
        context = Context(kwargs)
        if kwargs["action"]["http_method"] == "get":
            nkwargs["params"] = Template(kwargs["action"].get("http_data", "")).render(context)
        else:
            nkwargs["data"] = Template(kwargs["action"].get("http_data", "")).render(context)
        try:
            response = http_fn(kwargs["action"]["http_url"], **nkwargs)
        except requests.exceptions.RequestException:
            return False
        if not response.ok:
            return False
        return True


class SendEmail(Action):
    template_name = "actions/send_email.html"

    form_class = SendEmailForm