# Path: trigger/forms.py
# class FeedContainsForm(forms.Form):
#     feed_url = forms.URLField()
#     phrase = forms.CharField()
# 
# class DropboxFileUploadForm(forms.Form):
#     _access_token = forms.CharField(widget=forms.HiddenInput())
#     folder_name = forms.CharField()
# 
# Path: recipe/models.py
# class Recipe(models.Model):
#     user = models.ForeignKey(User)
#     trigger = models.ForeignKey(Trigger)
#     action = models.ForeignKey(Action)
#     last_checked = models.DateTimeField(auto_now=True)
#     trigger_params = models.TextField(default="{}")
#     action_params = models.TextField(default="{}")
# 
#     def __str__(self):
#         return "{0}: {1} -> {2}".format(self.user.username,
#                                         self.trigger.name,
#                                         self.action.name)


import requests
import feedparser
import time
import datetime
import requests
import json
from dateutil import parser
from django.shortcuts import render
from django.conf import settings
from django.contrib.sites.requests import RequestSite
from django.core.urlresolvers import reverse
from .forms import FeedContainsForm, DropboxFileUploadForm
from recipe.models import Recipe
from dropbox.client import DropboxOAuth2Flow, DropboxClient

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

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

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

    def validate(self, request):
        if not self.form_class:
            return (True, {})
        form = self.form_class(request.POST)
        if form.is_valid():
            return (True, form.cleaned_data)
        return (False, form.errors)

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


class DropboxFileUpload(Trigger):
    template_name = "triggers/dropbox_file_upload.html"

# Path: trigger/forms.py
# class FeedContainsForm(forms.Form):
#     feed_url = forms.URLField()
#     phrase = forms.CharField()
# 
# class DropboxFileUploadForm(forms.Form):
#     _access_token = forms.CharField(widget=forms.HiddenInput())
#     folder_name = forms.CharField()
# 
# Path: recipe/models.py
# class Recipe(models.Model):
#     user = models.ForeignKey(User)
#     trigger = models.ForeignKey(Trigger)
#     action = models.ForeignKey(Action)
#     last_checked = models.DateTimeField(auto_now=True)
#     trigger_params = models.TextField(default="{}")
#     action_params = models.TextField(default="{}")
# 
#     def __str__(self):
#         return "{0}: {1} -> {2}".format(self.user.username,
#                                         self.trigger.name,
#                                         self.action.name)


# Path: trigger/triggers.py
import requests
import feedparser
import time
import datetime
import requests
import json
from dateutil import parser
from django.shortcuts import render
from django.conf import settings
from django.contrib.sites.requests import RequestSite
from django.core.urlresolvers import reverse
from .forms import FeedContainsForm, DropboxFileUploadForm
from recipe.models import Recipe
from dropbox.client import DropboxOAuth2Flow, DropboxClient


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

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

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

    def validate(self, request):
        if not self.form_class:
            return (True, {})
        form = self.form_class(request.POST)
        if form.is_valid():
            return (True, form.cleaned_data)
        return (False, form.errors)

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


class DropboxFileUpload(Trigger):
    template_name = "triggers/dropbox_file_upload.html"

    form_class = DropboxFileUploadForm