69dc333e2727 — Peter Sanchez 13 years ago
Basic app done.
6 files changed, 99 insertions(+), 0 deletions(-)

A => README
A => cartfreakapi/__init__.py
A => cartfreakapi/models.py
A => cartfreakapi/tests.py
A => cartfreakapi/views.py
A => setup.py
A => README +5 -0
@@ 0,0 1,5 @@ 
+Django App that accepts communication 
+from the CartFreak Shopping cart API 
+hits.
+
+

          
A => cartfreakapi/__init__.py +5 -0
@@ 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

          
A => cartfreakapi/models.py +3 -0
@@ 0,0 1,3 @@ 
+from django.db import models
+
+# Create your models here.

          
A => cartfreakapi/tests.py +23 -0
@@ 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
+"""}
+

          
A => cartfreakapi/views.py +37 -0
@@ 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

          
A => setup.py +26 -0
@@ 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',
+    ],
+)