Killer Web Development

by Marco Laspe

3.2 Creating the pitch app

In your terminal go to you web2py folder and create a new app called pitch:

1.
2.
$ cd /your/web2py/folder # on Windows use backslashes
$ ./web2py.py -S pitch # web2py.exe on Windows and web2py.app on OS X

This will create a new app pitch (unless there is allready one) and put you in a python shell (we come back to the shell later). For the moment press Ctrl + D to exit the web2py python shell.

Putting things under version control

In the last chapter we installad the version controll system Mercurial; it's time to put it into action:

Again in the terminal go to the directory of the pitch app; create a repository; then create a .hgignore-file; add files to the repostitory and make your initial commit:

1.
2.
$ cd applications/pitch
$ gedit .hgignore

add the following content to .hgignore and close gedit

1.
2.
3.
4.
5.
6.
7.
syntax: glob
cache/**
databases/**
errors/**
sessions/**
*
˜
*.pyc

go back to the terminal

1.
2.
3.
$ hg init # hg is the element of mercury in the periodic table
$ hg add . # this adds all files to the repository
$ hg commit -m"Initial commit"

Pushing your code to BitBucket

Now we will push our code to bitbucket.org, a website specialized on hosting Mercurial repositories.

code web hosting application bitbucket

This has two advantages:

  1. You have a complete backup of your code.
  2. If you will get new team members, it will be much easier to collaborate.

First get an account on http://bitbucket.org, after that login and go to "Repositories" and "create repository".

code web hosting application bitbucket

Fill out the fields and click on "Create repository". Again in the terminal:

1.
2.
3.
$ hg push https://your_username@bitbucket.org/your_username/pitch
...
$ Password: [your password]

Fill in your password and your code should be pushed to your bitbucket account.

What is the buzz about version control all about?

Probably it's not 100\% clear, why version control is so helpful. Let me give you an example. We will now delete the whole controllers directory and then bring it back. In the pitch directory:

1.
2.
3.
4.
5.
6.
$ ls controllers/
$ rm -rf controllers/
$ ls controllers/ # They are gone
$ hg status #let's see what Mercurial is saying
$ hg revert --all # bring them back
$ ls controllers/

This way Mercurial let's you code with confidence. If you break something, you can always go back. One good habit with version controll is to commit often.

Creating the models

Ok, now we have to create the user model and the post model. Good things first: web2py allready created a user model for us; for the tukker app we will create a customized one.

For the post model open the file models/db.py and at the end of the file fill in the following content:

1.
2.
3.
db.define_table('post',
Field('content', 'text'),
Field('user_id', db.auth_user))

What did we do? We defined a table with two fields; one text field and a reference field to the user model; the id field is automaticly created by web2py, there is no need to do this explicitly.

Now let's play around with what we created. Go to http://127.0.0.1:8000/pitch/default/user/register and register a user. After we registered our user we can login at http://127.0.0.1:8000/pitch/default/user/login or view the profile of our user at http://127.0.0.1:8000/pitch/default/user/profile.

Now we need an interface to create post with our user, but first let's commit our changes:

1.
2.
3.
$ hg status 
M models/db.py
$ hg commit -m"added user and post models"

Creating a post form

To create a message form we must open the file controllers/default.py:

1.
$ gedit controllers/default.py

create a function post at the end of controller/default.py:

1.
2.
3.
4.
5.
6.
7.
...
return dict(form=crud())

def entry_post():
"""returns a form where the can entry a post"""
form = crud.create(db.post)
return dict(form=form)

now go to http://127.0.0.1:8000/pitch/default/entry_post and entry one or two posts, logout and again entry a post.

web2py form entry posts

What have done? We created our first Python function. We will explain function later in more detail; just keep in mind that the first line of a function begins with def and ends with a colon :. The rest of the function must be indented.

Wikipedia says

Function

In computer science, a subroutine (also called procedure, function, routine, method, or subprogram) is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.

In the second line of the function we create the form to entry a post. We use the CRUD mechanism of web2py. It is a standardized way to create and manipulate database entries - web2py supports other ways, but we will go with CRUD.

Wikipedia says

CRUD

In computer programming, create, read, update and delete (CRUD) are the four basic functions of persistent storage. Sometimes CRUD is expanded with the words retrieve instead of read or destroy instead of delete. It is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information; often using computer-based forms and reports.

The last line transfers the form to the view.

Now let's logout and again go to http://127.0.0.1:8000/pitch/default/entry_post - you can still add posts. We will now add some user restrictions; add the following line to controllers/default.py, but first commit your changes in the terminal:

1.
$ hg commit -m"created basic entry form for posts"

go back to controllers/default.py:

1.
2.
3.
@auth.requires_login()
def entry_post():
...

Refresh the page; you should see a login form, after you login with your email and username you can post messages again.

As a final step we will show users and messages on the home page of our pitch app. But first, commit your changes:

1.
$ hg commit -m"added authentication for post entry form"

Go to the index function in the default controller and change it like this:

1.
2.
3.
4.
5.
def index():
""" Shows all users and posts"""
users = db(db.auth_user.id > 0).select()
posts = db(db.post.id > 0).select()
return dict(users=users, posts=posts)

At http://127.0.0.1:8000/pitch/default/index you can view all users and posts.

web2py web pitch app homepage

Again commit the changes:

1.
$ hg commit -m"Show users an posts on the homepage"

Don't worry we will create a nicer homepage for the final tukker app.


Books often read by web2py and Python experts:

Comments

  1. There is a tilde("~") in your .hgignore listing that is invisible on my browser (Win7/Firefox 10.0). See here: http://i.imgur.com/TjF8s.jpg (This didn't cause any errors, but kept Mercurial from adding my files. Being new to hg, this was pretty confusing.)

  2. Hi! I can't create the app (I'm using OSX). I have my web2py app on the folloring location: /Users/mateocavasotto/Documents/web2py So, get to the this location through: "cd/Users/mateocavasotto/Documents/web2py" then i try to create the app writing "./web2py.app -S pitch" but I get "invalid command name "./web2py.app"" I never used a console before, perhaps it's really stupid, but I'm stucked. I would really appreacite any help. Thanks for the whole book.

  3. hello again! i still cannot find a suitable place to put .hg folder, .hgrc and .hgignore. everywhere says in the working directory / home. but where are these place???? o'm so lost :( please help!

Leave a Reply

Required fields are marked *.