# 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

class Action(object):
    form_class = None
    template_name = None

    def __init__(self, *args, **kwargs):
        self.form = self.form_class()

    def render(self, request, **kwargs):
        context = {"form": self.form}
        if kwargs.get("recipe"):
            recipe = kwargs["recipe"]
            recipe.action_params = json.loads(recipe.action_params)
            context["recipe"] = recipe
        return render(request, self.template_name, context)

    def validate(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            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


# 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


class Action(object):
    form_class = None
    template_name = None

    def __init__(self, *args, **kwargs):
        self.form = self.form_class()

    def render(self, request, **kwargs):
        context = {"form": self.form}
        if kwargs.get("recipe"):
            recipe = kwargs["recipe"]
            recipe.action_params = json.loads(recipe.action_params)
            context["recipe"] = recipe
        return render(request, self.template_name, context)

    def validate(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            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):