# 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
            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)
            dropbox_access_token = trigger_params["_access_token"]
            authorize_url = None
            folder_name = trigger_params["folder_name"]
        context = {"form": self.form,
                   "redirect_url": authorize_url,
                   "dropbox_access_token": dropbox_access_token,
                   "folder_name": folder_name}
        return render(request, self.template_name, context)
        
    def trigger(self, recipe, **kwargs):
        url = ("https://api.dropbox.com/1/metadata/auto/"
               "{0}?access_token={1}").format(kwargs["trigger"]["folder_name"],
                                              kwargs["trigger"]["_access_token"])
        if kwargs["trigger"].get("hash"):
            url += "&hash={0}".format(kwargs["trigger"]["hash"])
        response = requests.get(url)
        if not response.ok:
            return (False, ())
        if response.status_code == 304:
            return (False, ())
        kwargs["trigger"]["response"] = response.json()

# 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

            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)
            dropbox_access_token = trigger_params["_access_token"]
            authorize_url = None
            folder_name = trigger_params["folder_name"]
        context = {"form": self.form,
                   "redirect_url": authorize_url,
                   "dropbox_access_token": dropbox_access_token,
                   "folder_name": folder_name}
        return render(request, self.template_name, context)
        
    def trigger(self, recipe, **kwargs):
        url = ("https://api.dropbox.com/1/metadata/auto/"
               "{0}?access_token={1}").format(kwargs["trigger"]["folder_name"],
                                              kwargs["trigger"]["_access_token"])
        if kwargs["trigger"].get("hash"):
            url += "&hash={0}".format(kwargs["trigger"]["hash"])
        response = requests.get(url)
        if not response.ok:
            return (False, ())
        if response.status_code == 304:
            return (False, ())
        kwargs["trigger"]["response"] = response.json()

        instance = Recipe.objects.get(id=recipe["id"])