# 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
            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

    def render(self, request, **kwargs):
        recipe = kwargs.get("recipe")
        if not recipe:
            redirect_uri = "{0}://{1}{2}".format("https" if request.is_secure() else "http",
                                                 RequestSite(request).domain,
                                                 reverse("dropbox_oauth2_redirect"))
            csrf_key = "dropbox-auth-csrf-token"
            session_dict = {}
            authorize_url = DropboxOAuth2Flow(settings.DROPBOX_APP_KEY,
                                              settings.DROPBOX_APP_SECRET,
                                              redirect_uri,
                                              session_dict, csrf_key).start()
            request.session[csrf_key] = str(session_dict[csrf_key])
            dropbox_access_token = request.session.get("dropbox_access_token")
            folder_name = ""
        else:

# 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

            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

    def render(self, request, **kwargs):
        recipe = kwargs.get("recipe")
        if not recipe:
            redirect_uri = "{0}://{1}{2}".format("https" if request.is_secure() else "http",
                                                 RequestSite(request).domain,
                                                 reverse("dropbox_oauth2_redirect"))
            csrf_key = "dropbox-auth-csrf-token"
            session_dict = {}
            authorize_url = DropboxOAuth2Flow(settings.DROPBOX_APP_KEY,
                                              settings.DROPBOX_APP_SECRET,
                                              redirect_uri,
                                              session_dict, csrf_key).start()
            request.session[csrf_key] = str(session_dict[csrf_key])
            dropbox_access_token = request.session.get("dropbox_access_token")
            folder_name = ""
        else:

            trigger_params = json.loads(recipe.trigger_params)