Skip to content

Commit

Permalink
Version 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Mar 28, 2016
1 parent 146cdcc commit 5352107
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
)
4 changes: 2 additions & 2 deletions webshell/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion webshell/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
admin.site.register(Script, ScriptAdmin)
4 changes: 2 additions & 2 deletions webshell/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class ScriptForm(forms.ModelForm):
class Meta:
model = Script
fields = ['name', 'source']
fields = '__all__'

class Media:
css = {
Expand All @@ -18,4 +18,4 @@ class Media:
'js/codemirror.js',
'js/python.js',
'js/highlight.min.js',
)
)
11 changes: 8 additions & 3 deletions webshell/models.py
Original file line number Diff line number Diff line change
@@ -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
return self.name
7 changes: 4 additions & 3 deletions webshell/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
)
urlpatterns = patterns('',
url(r'^execute/$', execute_script_view, name='execute-script'),
)
26 changes: 22 additions & 4 deletions webshell/views.py
Original file line number Diff line number Diff line change
@@ -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)
return HttpResponse(buff.getvalue())

0 comments on commit 5352107

Please sign in to comment.