Documentation

Python Django Integration

Use the following code for your Python Django websites:

Without caching:

import re
import urllib

def file_get_contents(url):
    contents = urllib.urlopen(url).read()
    return contents

class KitGUI:
   
# replace with your own KitGUI CMS Account Key
    accountKey = "00000000000000000000000000000000"
    
    @staticmethod
    def getContent(request, contentID, contentType):
        if re.match(r'^[a-zA-Z]+[a-zA-Z0-9\-_]*$', contentID) is None:
            raise Exception('KitGUI content ID must begin with a letter ([A-Za-z]) and '
                            'may be followed by any number of letters, digits ([0-9]), '
                            'hyphens ("-"), underscores ("_")')
        url = "http://s3.amazonaws.com/kitgui/clients/%s/ids/%s.txt" % (KitGUI.accountKey, contentID)
        raw = file_get_contents(url)
        if request.COOKIES.get("kitgui", None) == "1":
            return ("<div style=\"display:inline-block\" class=\"kitgui-id-%s kitgui-content-type-"
                    "%s\">%s</div>" % (contentID, contentType, raw))
        else:
            return raw

 

With caching:

Requires Django Cache Framework, you need to configure it correctly (https://docs.djangoproject.com/en/dev/topics/cache/)

import re
import urllib
from django.core.cache import cache


def file_get_contents(url):
    contents = urllib.urlopen(url).read()
    return contents


class KitGUI:
    # replace with your own KitGUI CMS Account Key
    accountKey = "00000000000000000000000000000000"
    
    @staticmethod
    def getContent(request, contentID, contentType):
        if re.match(r'^[a-zA-Z]+[a-zA-Z0-9\-_]*$', contentID) is None:
            raise Exception('KitGUI content ID must begin with a letter ([A-Za-z]) and '
                            'may be followed by any number of letters, digits ([0-9]), '
                            'hyphens ("-"), underscores ("_")')
        editMode = request.COOKIES.get("kitgui", None) == "1"
        raw = None
        if not editMode:
            raw = cache.get(contentID)
        if editMode or raw is None:
            url = "http://s3.amazonaws.com/kitgui/clients/%s/ids/%s.txt" % (KitGUI.accountKey, contentID)
            raw = file_get_contents(url)
            cache.set(contentID, raw, 3600) # cache for an hour
        if editMode:
            return ("<div style=\"display:inline-block\" class=\"kitgui-id-%s kitgui-content-type-"
                    "%s\">%s</div>" % (contentID, contentType, raw))
        else:
            return raw


The following is an example of using the above class in a Django view to pull in content ID "your-content-id" that is of content type "HTML".

In your view function,
def the_view_function(request):
    kitgui_content = KitGUI.getContent(request, "your-content-id", "HTML")
    render_to_response("the_view.template", {"kitgui_content": kitgui_content})


In your template file "the_view.template",
    <body>
        {{kitgui_content|safe}}
    </body>

You can dynamically set this content ID as it does not have to exist. It will only exist once saved when in edit mode. This means using dynamic URLs is very interesting if you set the content ID by the getting a parameter from the URL such as "/articles/[article-name-or-id]".

Posted by Jacob Fan (Guan Ping Fan on odesk)