diff --git a/setup.py b/setup.py index 59ed891..abf7a69 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,10 @@ 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.5', 'Topic :: Software Development :: Libraries :: Python Modules', ], ) diff --git a/webshell/__init__.py b/webshell/__init__.py index 6cd8f69..2f30efe 100644 --- a/webshell/__init__.py +++ b/webshell/__init__.py @@ -1,7 +1,7 @@ -VERSION = (0, 0, 5) +VERSION = (0, 1) def get_version(): - return '.'.join([str(v) for v in VERSION]) + return '.'.join(map(str, VERSION)) __version__ = get_version() diff --git a/webshell/admin.py b/webshell/admin.py index ee68f11..e643f26 100644 --- a/webshell/admin.py +++ b/webshell/admin.py @@ -5,6 +5,7 @@ class ScriptAdmin(admin.ModelAdmin): form = ScriptForm + list_display = ('name',) change_form_template = 'webshell/change_form.html' -admin.site.register(Script, ScriptAdmin) \ No newline at end of file +admin.site.register(Script, ScriptAdmin) diff --git a/webshell/forms.py b/webshell/forms.py index 4bdfccb..7bf9e71 100644 --- a/webshell/forms.py +++ b/webshell/forms.py @@ -5,7 +5,7 @@ class ScriptForm(forms.ModelForm): class Meta: model = Script - fields = ['name', 'source'] + fields = '__all__' class Media: css = { @@ -18,4 +18,4 @@ class Media: 'js/codemirror.js', 'js/python.js', 'js/highlight.min.js', - ) \ No newline at end of file + ) diff --git a/webshell/models.py b/webshell/models.py index 59efc72..e04c832 100644 --- a/webshell/models.py +++ b/webshell/models.py @@ -1,9 +1,14 @@ from django.db import models +from django.utils.translation import ugettext_lazy as _ class Script(models.Model): - name = models.CharField(max_length=100) - source = models.TextField() + name = models.CharField(_('Name'), max_length=100) + source = models.TextField(_('Source')) + + class Meta: + verbose_name = _('Script') + verbose_name_plural = _('Scripts') def __unicode__(self): - return self.name \ No newline at end of file + return self.name diff --git a/webshell/urls.py b/webshell/urls.py index bcc4780..64979f3 100644 --- a/webshell/urls.py +++ b/webshell/urls.py @@ -1,5 +1,6 @@ from django.conf.urls import patterns, url +from .views import execute_script_view -urlpatterns = patterns('webshell.views', - url(r'^execute/$', 'execute_script_view', name='execute-script'), -) \ No newline at end of file +urlpatterns = patterns('', + url(r'^execute/$', execute_script_view, name='execute-script'), +) diff --git a/webshell/views.py b/webshell/views.py index d50b947..2a3745c 100644 --- a/webshell/views.py +++ b/webshell/views.py @@ -1,14 +1,32 @@ -import commands +import sys +import traceback +from contextlib import contextmanager +try: + from StringIO import StringIO +except ImportError: + from io import StringIO from django.http import HttpResponse from django.views.decorators.http import require_POST from django.contrib.auth.decorators import permission_required +@contextmanager +def catch_stdout(buff): + stdout = sys.stdout + sys.stdout = buff + yield + sys.stdout = stdout + + @require_POST @permission_required('is_superuser') def execute_script_view(request): - source = request.POST.get('source', '').replace('"', r'\"') - result = commands.getoutput('python -c "%s"' % source) + buff = StringIO() + try: + with catch_stdout(buff): + exec(request.POST.get('source', '')) + except: + traceback.print_exc(file=buff) - return HttpResponse(result) \ No newline at end of file + return HttpResponse(buff.getvalue())