Sei sulla pagina 1di 12

Web Engineering

Laboratory 3
Web2py database access
Aims
There are four main aims for this Laboratory
1. To develop skills in creating database tables inside the web2py model.
2. To explain how to manually add data to the database.
3. To show how queries are performed using the web2py DAL
3. To explain how to create HTML forms based on the database schema (SQL form)
Required software for this laboratory
Firefox Browser v17 or later
Firebug Debugger v1.11.4 or later
WebStorm IDE v6.0 or later or Wing IDE 101 version 4.1.13 or later or the built in editor of
web2py
web2py framework 2.5.1 or later
USB software image download including Portable Python.
We will be using the webstorm IDE to edit in this lab.

IMPORTANT MESSAGE ABOUT USB DRIVES
At the end of each prac, you must stop your USB from the system tray. Otherwise,
data may be lost from it.
In Windows XP its like this:

In Windows 7 it is similar.


Part 1: Creating a movie database schema in web2py
By the end of this part of the practical you should be able to:
add python statements to the web2py model to create a movie database.
We will use the Webstorm editor on the welcome application.

1. Start web2py server
On your USB drive start the web2py server by clicking on the bat file. Type and admin
password and start the server.
Open the welcome application inside Firefox by cutting and pasting the URL
http://127.0.0.1:8000/welcome/default/index
2. Start the Webstorm IDE
If you have a previous project open close it.
Use open directory and navigate to the USB drive. Fine the web2py/applications folder and
open the welcome folder.
Now using the IDE find the models folder and open db.py


3. Add a movie table to the db database
At the bottom of the db.py file add statements to define the movie database:
db.define_table('movie',
Field('title','string',length=IS_LENGTH(256)),
Field('release_year','integer',requires=IS_INT_IN_RANGE(1800, 2200)),
Field('genre','string',length=IS_LENGTH(1000)),
Field('country','string',length=IS_LENGTH(128)),
Field('cast_names','text',length=IS_LENGTH(6000)),
Field('classification','string',length=IS_LENGTH(8)),
Field('director','string',length=IS_LENGTH(64)),
Field('duration','integer',requires=IS_INT_IN_RANGE(1, 400)),
Field('imdb_number','integer',requires=IS_INT_IN_RANGE(1, 10000000)),
Field('imdb_rating','double',requires=IS_FLOAT_IN_RANGE(0, 10)),
Field('plot_outlinee','text',length=IS_LENGTH(60000)),
Field('studio','string',length=IS_LENGTH(128))
)
Note that all fields have a type. Note that we have specified lengths for all string and text fields. This
aids portability between databases by not relying on an arbitrary default length for these. The requires
attribute defines the range of values that are appropriate for integers and doubles. This together with
the type attribute will be passed down to the HTML forms for use in validating user input. Users will
not be permitted to input values of the wrong type.
You test if the code is correct by reloading the welcome index page. If the page loads normally your
table has been initialized. If you get a ticket you have made a syntax error and need to check the ticket
to see where.
When the welcome page is loaded the database table should be initialized. You can also go to the
web2py database admin page to see that the table is present:

Note that if you make a syntax error in the table definition you will get a web2py ticket and the table
will not appear.

4. Load the movie table with data from a spreadsheet CSV file imdb2.cvs
A sample set of movie data is provided in the zip file for this practical. Its called imdb2.csv
because the source of the data was the IMDB movie database: http://www.imdb.com. The
windows program embd http://www.emdb.tk was used to download from the IMDB site and
convert the information to csv format. Note that emdb has issues in exporting to CSV so the
file had to be cleaned up manually.
Load csv files into a spreadsheet (Excel) to see the data:

Dont save the file again when you exit excel to avoid corrupting the file with a new format.
Place the imdb2.csv file into the same folder as the welcome application inside web2py.
Load the csv file into the database.
We will write a simple python controller function that uses the python package csv to parse
the file.
def myimport():
import csv
ifile = open('imdb2.csv', "rb")
reader = csv.reader(ifile)
for row in reader:
db.movie.insert(title=row[0],
release_year=row[1],
genre=row[2],
country=row[3],
cast_names=row[4],
classification=row[5],
director=row[6],
duration=row[7],
imdb_number=row[8],
imdb_rating=row[9],
plot_outlinee=row[10],
studio=row[11]
)

ifile.close()
return dict(row=row[0])
Add a function to drop all the data from the movie table. This allows you to remove incorrect
data that might get added as you are debugging your programs.
def dropmovie():
db.movie.drop()
return dict()
To test these functions out, setup three tabs in firefox:
One for the dropmovie function:
http://127.0.0.1:8000/welcome/default/dropmovie
Loading this page will remove all the data from the movie table
One for the myimport function:
http://127.0.0.1:8000/welcome/default/myimport
If the import works you should get a page that has the Sound of Music which is the
last movie in the file to load.
One for the database admin page for the movie table so you can see easily whats in
the table:
http://127.0.0.1:8000/welcome/appadmin/select/db?query=db.movie.id%3E0
This one will show the contents of the movie table. When loaded correctly should
look like this:

If you get a ticket at any time you must fix the problem before proceeding. If you are unsure
where the upload is drop the table contents and then reload it.

Part 2 Querying the database
By the end of this part of the practical you should be able to
add python statements to the web2py controller that queries the movie database.
We will use the WebStorm editor on the welcome application.

1. Select all the rows in the database table movie
Query is an object that represents a SQL "WHERE" clause. To apply a query it is passed to
the db() object as a parameter. This generates a set object. Set objects have methods which
when called cause the database to be accessed or updated.
Enter this code into the default controller

Run it from a separate browser tab:

The rest of the dump is not shown but all the data in the database is sent to the browser.

2. Show movie titles and ratings ordered by rating from high to low

Here is the output:


3. Select a subset of the movie titles with ratings greater than 8.0 ordered by rating
The db() part of the query allows you to select a subset of the database to display (similar to
the SQL "WHERE" clause):




Part 3 Creating HTML forms based on the database tables
By the end of this part of the practical you should be able to
Create an HTML for based on the movie database.
We will use the WebStorm editor on the welcome application.

1. Define a function that both creates, validates and processes a form using the
database as template
This section illustrates form automation in web2py. Please read the comments in the code to
see how it works:

def autovalidatingform():
"""
Use SQLFORM to generate the form with dbio=False so the
database is not updated in this function.
Web2py validators automate the checking of data entered by
Users.
"""
# Use the database table as a template for the form
myform=SQLFORM(db.movie)
# Apply the accepts function to the data returned from myform
# This function returns True if the data in the form is
# correct according to the validators defined (as requires) in
# the model
if myform.accepts(request.vars, session, dbio=False):
response.flash='form accepted'
# if the form has errors then myform.accepts will be False and
# myform.errors will be True
elif myform.errors:
response.flash='form has errors'
# the next case is reached if
# both myform.accepts and myform.errors are False
# this is the case where no data has yet been submitted to the
# form this happens when the form is first displayed
else:
response.flash='please fill in the form'
return dict(form=myform)
The result is a form that checks your input based on the requires statements in the model

Try entering incorrect data to see that the form is checking: such as a release year before
1800.

This concludes your practical

Potrebbero piacerti anche