# HG changeset patch # User Peter Sanchez # Date 1282096624 25200 # Tue Aug 17 18:57:04 2010 -0700 # Node ID 69dc333e27272224906e46decaeff849d6ea95e1 # Parent 6bf362ea9c7361ec82f567fe6c2d2333341458bd Basic app done. diff --git a/README b/README new file mode 100644 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +Django App that accepts communication +from the CartFreak Shopping cart API +hits. + + diff --git a/cartfreakapi/__init__.py b/cartfreakapi/__init__.py new file mode 100644 --- /dev/null +++ b/cartfreakapi/__init__.py @@ -0,0 +1,5 @@ +class CartFreakError(Exception): + ''' General error. Used to raise an + error from a callback function. + ''' + pass \ No newline at end of file diff --git a/cartfreakapi/models.py b/cartfreakapi/models.py new file mode 100644 --- /dev/null +++ b/cartfreakapi/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/cartfreakapi/tests.py b/cartfreakapi/tests.py new file mode 100644 --- /dev/null +++ b/cartfreakapi/tests.py @@ -0,0 +1,23 @@ +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.failUnlessEqual(1 + 1, 2) + +__test__ = {"doctest": """ +Another way to test that 1 + 1 is equal to 2. + +>>> 1 + 1 == 2 +True +"""} + diff --git a/cartfreakapi/views.py b/cartfreakapi/views.py new file mode 100644 --- /dev/null +++ b/cartfreakapi/views.py @@ -0,0 +1,37 @@ +import hashlib +from django.conf import settings +from django.http import HttpResponse +from django.core.exceptions import ImproperlyConfigured +from cartfreakapi import CartFreakError + + +ERROR_STR = 'ERROR %s' + + +def handle_api(request, callback=None, key_name='CARTFREAKAPI_KEY'): + if not hasattr(settings, key_name): + raise ImproperlyConfigured( + u'No CartFreak API setting configured for %s' % key_name + ) + + required = ('command', 'hash') + cf_key = getattr(settings, key_name) + for req in required: + if req not in request.POST: + err_msg = 'No %s varaible was sent' % req + return HttpResponse(ERROR_STR % err_msg) + + in_hash = request.POST.get('hash') + command = request.POST.get('command') + _hash = hashlib.sha1(cf_key + command).hexdigest() + + if _hash != in_hash: + return HttpResponse(ERROR_STR % 'Invalid hash sent') + + if callback is not None and callable(callback): + try: + callback(request.POST.copy()) + except CartFreakError, err: + return HttpResponse(ERROR_STR % str(err)) + + return HttpResponse('OK') \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from distutils.core import setup + +long_description = open('README').read() + +setup( + name='cartfreakapi', + version="0.1", + package_dir={'cartfreakapi': 'cartfreakapi'}, + packages=['cartfreakapi',], + description='Django CartFreak API Handler', + author='Peter Sanchez', + author_email='petersanchez@gmail.com', + license='BSD License', + url='http://bitbucket.org/petersanchez/django-cartfreakapi/', + long_description=long_description, + platforms=["any"], + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD License', + 'Natural Language :: English', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Environment :: Any Environment', + ], +)