Peter Sanchez avatar Peter Sanchez committed ae31f40

initial import....

Comments (0)

Files changed (11)

+syntax:glob
+.coverage
+.tox
+settings_local.py
+.*.swp
+**.pyc
+MANIFEST
+
+
+syntax:regexp
+^htmlcov$
+^env$

Empty file added.

awesomebox/__init__.py

+__version__ = '0.1'
+
+

awesomebox/base.py

+from collections import namedtuple
+from django.conf import settings
+from django.utils.importlib import import_module
+from django.core.urlresolvers import reverse_lazy
+
+
+Result = namedtuple('Result', ['success', 'message'])
+
+
+class BaseAwesomebox(object):
+    init_str = '!'
+    cmd_sep = ' '
+    success_url = reverse_lazy('awesomebox_main')
+    template_name = 'awesomebox/awesomebox.html'
+
+    def __init__(self, cmd):
+        self.cmd = cmd
+
+    def run_command(self):
+        cmd = self.cmd
+        if self.init_str is not None and not cmd.startswith(self.init_str):
+            return Result(False, 'Invalid command initialization.')
+
+        real_cmd, _, data = cmd[len(self.init_str):].partition(self.cmd_sep)
+        func = getattr(self, 'cmd_{}'.format(real_cmd, None)
+        if func is None or not callable(func):
+            return Result(False, 'Invalid command.')
+
+        result = func(data)
+        if not isinstance(result, Result):
+            return Result(
+                False,
+                ('Invalid result type given. Expected '
+                 'awesomebox.base.Result instance.'),
+            )
+        return result
+
+    def cmd_echo(self, data):
+        ''' Example function. Returns back the data passed in.
+        '''
+        return Result(True, data)
+
+
+def get_awesomebox_class():
+    ''' Return the class to use for Awesomebox dispatching.
+    '''
+    class NullAwesomeBox(BaseAwesomebox):
+        pass
+
+    cls_path = getattr(settings, 'AWESOMEBOX_CLASS', None)
+    cls = NullAwesomeBox if cls_path is None else import_module(cls_path)
+    if not issubclass(cls, BaseAwesomebox):
+        raise TypeError(
+            'Invalid Awesomebox Class. Must subclass '
+            'awesomebox.base.BaseAwesomebox'
+        )
+    return cls

awesomebox/forms.py

+from django import forms
+
+
+class AwesomeboxForm(forms.Form):
+    cmd = forms.CharField(widget=forms.Textarea())

awesomebox/models.py

+from django.db import models
+
+# Create your models here.

awesomebox/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this 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.assertEqual(1 + 1, 2)

awesomebox/urls.py

+from django.conf.urls import patterns, url
+from .views import AwesomeboxView
+
+
+urlpatterns = patterns('awesomebox.views',
+    url(r'^/?$',
+        AwesomeboxView.as_view(),
+        name='awesomebox_main'),
+)

awesomebox/views.py

+import json
+from django.conf import settings
+from django.views.base import View
+from django.http import HttpResponse
+from django.shortcuts import render, redirect
+from .forms import AwesomeboxForm
+from .base import get_awesomebox_class
+
+AwesomeboxCls = get_awesomebox_class()
+
+
+class AwesomeboxView(View):
+    template_name = AwesomeboxCls.template_name
+    form_class = AwesomeboxForm
+    success_url = AwesomeboxCls.success_url
+
+    def get(self, request, *args, **kwargs):
+        form = self.form_class()
+        return render(request,  self.template_name, {'form': form})
+
+    def post(self, request, *args, **kwargs):
+        form = self.form_class(request.POST)
+        if form.is_valid():
+            cmd = form.cleaned_data.get('cmd')
+            abox = AwesomeboxCls(cmd)
+            result = abox.run_command()
+            if request.is_ajax():
+                result = {
+                    'success': result.success,
+                    'message': result.message,
+                }
+                return HttpResponse(
+                    json.dumps(result),
+                    mimetype='application/json',
+                )
+            # TODO Add message here for user session
+            return redirect(self.success_url)
+
+        return render(request,  self.template_name, {'form': form})
+import os
+from distutils.core import setup
+
+project_name = 'awesomebox'
+long_description = open('README.rst').read()
+
+# Idea from django-registration setup.py
+packages, data_files = [], []
+root_dir = os.path.dirname(__file__)
+if root_dir:
+    os.chdir(root_dir)
+
+for dirpath, dirnames, filenames in os.walk(project_name):
+    # Ignore dirnames that start with '.'
+    for i, dirname in enumerate(dirnames):
+        if dirname.startswith('.'):
+            del dirnames[i]
+    if '__init__.py' in filenames:
+        pkg = dirpath.replace(os.path.sep, '.')
+        if os.path.altsep:
+            pkg = pkg.replace(os.path.altsep, '.')
+        packages.append(pkg)
+    elif filenames:
+        prefix = dirpath[(len(project_name) + 1):]
+        for f in filenames:
+            data_files.append(os.path.join(prefix, f))
+
+setup(
+    name='django-awesomebox',
+    version=__import__(project_name).__version__,
+    package_dir={project_name: project_name},
+    packages=packages,
+    package_data={project_name: data_files},
+    description=('Django "awesomebox" to easily dispatch '
+                 'commands from an admin dashboard.'),
+    author='Peter Sanchez',
+    author_email='petersanchez@gmail.com',
+    license='BSD License',
+    url='http://bitbucket.org/petersanchez/django-awesomebox/',
+    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 :: Web Environment',
+    ],
+)
+[tox]
+downloadcache = {toxworkdir}/cache/
+envlist = py3.3-django1.5,py2.7-django1.5,py2.6-django1.5,py2.7-django1.4,py2.6-django1.4
+
+[testenv]
+commands = {envpython} runtests.py
+
+[testenv:py3.3-django1.5]
+basepython = python3.3
+deps = django==1.5.1
+
+[testenv:py2.7-django1.5]
+basepython = python2.7
+deps = django==1.5.1
+
+[testenv:py2.7-django1.4]
+basepython = python2.7
+deps = django==1.4.5
+
+[testenv:py2.6-django1.5]
+basepython = python2.6
+deps = django==1.5.1
+
+[testenv:py2.6-django1.4]
+basepython = python2.6
+deps = django==1.4.5
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.