Source

inbounding / inbounding / dispatch / api / handlers.py

import re
import json

from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import get_object_or_404

from piston.handler import BaseHandler
from piston.utils import rc

from dispatch.models import Refer, Action


def template_engine(result):
    '''
       This is used to select the templates based on the Action.template value.
       We override the exising database Action.code value. The html templates 
       are stored in the templates.py file.
    '''
    template_id = result.template
    if template_id == 1:
        #Use MailChimp html template
        profile = result.user.get_profile()
        customer_uuid = profile.uuid
        mailchimp = '''
            <!-- ContextKick custom form --> 
            <div id="embed_signup">
                <h3>Subscribe to our Newsletter</h3>
                <form src="#" id="subscribe-form" name="mc-embedded-subscribe-form" 
                     class="mailchimp-submit validate form-stacked">
                  <input type="hidden" value="%s">
                  <div class="mc-field-group clearfix">
                    <label for="mce-EMAIL">Email Address </label>
                    <div class="input">
                        <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
            	</div>
                  </div>
                  <div class="clear">
                      <input type="submit" value="Subscribe" name="subscribe" id="submit" class="btn button">
                  </div>
                 </form>
            </div>
            ''' % customer_uuid

        result.code = mailchimp
    elif template_id == 2:
        '''
        Facebook Template
        '''

        '''j = json.loads(result.tpl_values)
        j.get('url')'''


class ReferHandler(BaseHandler):
    allowed_methods = ('GET')
    model = Refer
    
    def read(self, request):
        '''
           Search for an Action by the source (customer) domain and the
           referral domain.
        '''
        try:
            result = Action.objects.select_related().\
                              filter(url=request.GET.get('u'), \
                                     refer__name=request.GET.get('ref'))
            if not result:
                result = "No valid Refer & Action combination was found."
            else:
                template_engine(result[0])
                
            return result[0]
        except Action:
            return "An exception occured when checking the Referrer."

class ActionHandler(BaseHandler):
    '''

    '''
    allowed_methods = ('GET')
    model = Action
    fields = ("id", "url", "code", "type")
    
    def read(self, request):
        mylist = []
        return mylist

    def create(self, request, recipe_id=None):
        pass

    def update(self, request):
        pass
    
    def delete(self, request, recipe_id=None):
        pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.