Per la spiegazione del funzionamento di Django ovviamente rimando al sito ufficiale, in questo articolo vengono riportati solo i passaggi principali.
Vediamo innanzitutto il contenuto del file urls.py, che descrive lo schema delle URL di questa applicazione (molto semplice):
# Copyright (C) 2010 Lorenzo Sfarra (lorenzosfarra@ubuntu.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from django.conf.urls.defaults import *
from django.conf import settings
MEDIA_ROOT = settings.MEDIA_ROOT
urlpatterns = patterns('',
(r'^cmd/(?P.*)$', 'terminaljs.terminal.views.cmd'),
(r'^media/(?P.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
(r'^$', 'terminaljs.terminal.views.index'),
)
Definiamo quindi che una URL con cmd/ iniziale richiama la view cmd con un parametro "command", il comando da eseguire. Definiamo inoltre che le URL con "media/" iniziale devono essere processate staticamente, ed infine definiamo la URL predefinita che richiama la view index.
Passiamo quindi a vedere il codice delle view, definito nel file views.py:
# Copyright (C) 2010 Lorenzo Sfarra (lorenzosfarra@ubuntu.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from django.shortcuts import render_to_response
from django.http import HttpResponse
from commands import getoutput
import os.path
# global variable
CWD = "/home/lorenzo"
def index(request):
return render_to_response("index.html")
def is_trusted_command(command):
#TODO: check if it's a trusted command
return True
def cmd(request, command):
global CWD
if command.startswith("cd "):
new_path = command.split(" ")[1]
if not new_path.startswith("/"):
# relative path
new_path = os.path.join(CWD, new_path)
if os.path.isdir(new_path):
CWD = new_path
output = ""
else:
output = "cd: " + new_path + ": No such file or directory"
elif is_trusted_command(command):
output = getoutput("cd %s; %s" %(CWD,command))
else:
output = "Untrusted command."
return HttpResponse(output)
Da sottolineare, così come fatto nella prima parte, la funzione is_trusted_command che deve essere implementata anche server-side.
Ovviamente il tutto è puramente dimostrativo, per chi fosse interessato a migliorarlo è presente tutto il codice su Google Code.
8 commenti:
Posta un commento