Web to py enterprise web framework - p 11 docx

10 236 0
Web to py enterprise web framework - p 11 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

MORE ON ADMIN 85 [EDIT] You have used the [EDIT] page already in this chapter. Here we want to point out a few more functionalities of the [EDIT] page. • If you click on any file name, you can see the content of the file with syntax highlighting. • If you click on edit, you can edit the file via a web interface. • If you click on delete, you can delete the file (permanently). • If you click on test, web2py will run tests. Tests are written by the developerusing Python doctests, and each function should have its own tests. • View files have an htmledit link that allows editing the view using a web-based WYSIWYG editor. • You can add language files, scan the app to discover all strings, and edit string translations via the web interface. • If the static files are organized in folders and subfolders, the folder hierarchy can be toggled by clicking on a folder name. The image below shows the output of the test page for the welcome appli- cation. The image below show the languages tab for the welcome application. 86 OVERVIEW The image below shows how to edit a language file, in this case the "it" (Italian) language for the welcome application. MORE ON ADMIN 87 shell If you click on the "shell" link under the controllers tab in [EDIT], web2py will open a web based Python shell and will execute the models for the current application. This allows you to interactively talk to your application. crontab Also under the controllers tab in [EDIT] there is a "crontab" link. By clicking on this link you will be able to edit the web2py crontab file. This follows the same syntax as the unix crontab but does not rely on unix. In fact, it only requires web2py and it works on Windows too. It allows you to register actions that need to be executed in background as scheduled times. For more information about this we refer to the next chapter. [errors] When programming web2py, you will inevitably make mistakes and intro- duce bugs. web2py helps in two ways: 1) it allows you to create tests for every function that can be run in the browser from the [EDIT] page; and 2) when an error manifests itself, a ticket is issued to the visitor and the error is logged. Purposely introduce an error in the images application as shown below: 1 def index(): 2 images = db().select(db.image.ALL,orderby=db.image.title) 88 OVERVIEW 3 1/0 4 return dict(images=images) When you access the index action, you get the following ticket: Only the administrator can access the ticket: The ticket shows the traceback, and the content of the file that caused the problem. If the error occurs in a view, web2py shows the view converted MORE ON ADMIN 89 from HTML into Python code. This allows to easily identify the logical structure of the file. Notice that everywhere admin shows syntax-highlighted code (for exam- ple, in error reports, web2py keywords are shown in orange). If you click on a web2py keyword, you are redirected to a documentation page about the keyword. If you fix the 1/0 bug in the index action and introduce one in the index view: 1 {{extend 'layout.html'}} 2 3 <h1>Current Images</h1> 4 <ul> 5 {{for image in images:}} 6 {{1/0}} 7 {{=LI(A(image.title, _href=URL(r=request, f="show", args=image.id))) }} 8 {{pass}} 9 </ul> you get the following ticket: Note that web2py has converted the view from HTML into a Python file, and thus, the error described in the ticket refers to the generated Python code and NOT to the original view file. you get the following ticket: 90 OVERVIEW This may seem confusing at first, but in practice it makes debugging easier, because the Python indentation highlights the logical structure of the code that you embedded in the views. The code is shown at the bottom of the same page. All tickets are listed under admin in the [errors] page for each application: MORE ON APPADMIN 91 [mercurial] If you are running from source and you have the Mercurial version control libraries installed: 1 easy_install mercurial then the administrative interface shows one more menu item called "mercu- rial". It automatically creates a local Mercurial repository for the application. Pressing the "commit" button in the page will commit the current application. This feature is experimental and will be improved in the future. 3.11 More on appadmin appadmin is not intended to be exposed to the public. It is designed to help you by providing an easy access to the database. It consists of only two files: a controller "appadmin.py" and a view "appadmin.html" which are used by all actions in the controller. The appadmin controller is relatively small and readable; it provides an example on designing a database interface. appadmin shows which databases are available and which tables exist in each database. You can insert records and list all records for each table individually. appadmin paginates output 100 records at a time. Once a set of records is selected, the header of the pages changes, allowing you to update or delete the selected records. To update the records, enter an SQL assignment in the Query string field: 1 title = 'test' where string values must be enclosed in single quotes. Multiple fields can be separated by commas. To delete a record, click the corresponding checkbox and confirm that you are sure. appadmin can also perform joins if the SQL FILTER contains a SQL condition that involves two or more tables. For example, try: 1 db.image.id == db.comment.image_id web2py passes this along to the DAL, and it understands that the query links two tables; hence, both tables are selected with an INNER JOIN. Here is the output: 92 OVERVIEW If you click on the number of an id field, you get an edit page for the record with the corresponding id. If you click on the number of a reference field, you get an edit page for the referenced record. You cannot update or delete rows selected by a join because they involve records from multiple tables and this would be ambiguous. CHAPTER 4 THE CORE 4.1 Command Line Options It is possible to skip the GUI and start web2py directly from the command line by typing something like: 1 python web2py.py -a 'your password' -i 127.0.0.1 -p 8000 When web2py starts, it creates a file called "parameters 8000.py" where it stores the hashed password. If you use "<ask>" as the password, web2py prompts you for it. For additional security, you can start web2py with: 1 python web2py.py -a '<recycle>' -i 127.0.0.1 -p 8000 In this case web2py reuses the previously stored hashed password. If no password is provided, or if the "parameters 8000.py" file is deleted, the web-based administrative interface is disabled. WEB2PY: Enterprise Web Framework / 2nd Ed By Massimo Di Pierro Copyright © 2009 93 94 THE CORE web2py normally runs with CPython (the C implementation of the Python interpreter created by Guido van Rossum), but it can also runwith Jython(the Javaimplementation ofthe interpreter). The latter possibility allows the use of web2py in the context of a J2EE infrastructure. To use Jython, simply replace "python web2py.py " with "jython web2py.py". Details about installing Jython, zxJDBC modules required to access the databases can be found in Chapter 12. The "web2py.py" script can take many command-line arguments specify- ing the maximum number of threads, enabling of SSL, etc. For a complete list type: 1 >>> python web2py.py -h 2 Usage: python web2py.py 3 4 web2py Web Framework startup script. ATTENTION: unless a password 5 is specified (-a 'passwd'), web2py will attempt to run a GUI. 6 In this case command line options are ignored. 7 8 Options: 9 version show program's version number and exit 10 -h, help show this help message and exit 11 -i IP, ip=IP ip address of the server (127.0.0.1) 12 -p PORT, port=PORT port of server (8000) 13 -a PASSWORD, password=PASSWORD 14 password to be used for administration 15 use -a "<recycle>" to reuse the last 16 password 17 -u UPGRADE, upgrade=UPGRADE 18 -u yes: upgrade applications and exit 19 -c SSL_CERTIFICATE, ssl_certificate=SSL_CERTIFICATE 20 file that contains ssl certificate 21 -k SSL_PRIVATE_KEY, ssl_private_key=SSL_PRIVATE_KEY 22 file that contains ssl private key 23 -d PID_FILENAME, pid_filename=PID_FILENAME 24 file to store the pid of the server 25 -l LOG_FILENAME, log_filename=LOG_FILENAME 26 file to log connections 27 -n NUMTHREADS, numthreads=NUMTHREADS 28 number of threads 29 -s SERVER_NAME, server_name=SERVER_NAME 30 server name for the web server 31 -q REQUEST_QUEUE_SIZE, request_queue_size=REQUEST_QUEUE_SIZE 32 max number of queued requests when server unavailable 33 -o TIMEOUT, timeout=TIMEOUT 34 timeout for individual request (10 seconds) 35 -z SHUTDOWN_TIMEOUT, shutdown_timeout=SHUTDOWN_TIMEOUT 36 timeout on shutdown of server (5 seconds) 37 -f FOLDER, folder=FOLDER 38 folder from which to run web2py 39 -v, verbose increase test verbosity 40 -Q, quiet disable all output . >>> python web2 py. py -h 2 Usage: python web2 py. py 3 4 web2 py Web Framework startup script. ATTENTION: unless a password 5 is specified (-a 'passwd'), web2 py will attempt to run a GUI. 6. start web2 py directly from the command line by typing something like: 1 python web2 py. py -a 'your password' -i 127.0.0.1 -p 8000 When web2 py starts, it creates a file called "parameters. web2 py. py -a '<recycle>' -i 127.0.0.1 -p 8000 In this case web2 py reuses the previously stored hashed password. If no password is provided, or if the "parameters 8000 .py& quot;

Ngày đăng: 06/07/2014, 19:20

Tài liệu cùng người dùng

Tài liệu liên quan