CherryPy - Demo Application


Advertisements


In this chapter, we will focus on how an application is created in CherryPy framework.

Consider Photoblog application for the demo application of CherryPy. A Photoblog application is a normal blog but the principal text will be photos in place of text. The main catch of Photoblog application is that the developer can focus more on design and implementation.

Basic Structure – Design of Entities

The entities design the basic structure of an application. The following are the entities for the Photoblog application −

  • Film
  • Photo
  • Album

The following is a basic class diagram for the entity relationship −

Basic Structure

Design Structure

As discussed in the previous chapter, the design structure of the project would be as shown in the following screenshot −

Design Structure

Consider the given application, which has sub-directories for Photoblog application. The sub-directories are Photo, Album, and Film which would include controllers.py, models.py and server.py.

Functionally, the Photoblog application will provide APIs to manipulate those entities via the traditional CRUD interface — Create, Retrieve, Update, and Delete.

Connection to the Database

A storage module includes a set of operations; connection with the database being one of the operations.

As it is a complete application, the connection with database is mandatory for API and to maintain the functionality of Create, Retrieve, Update and Delete.

import dejavu

arena = dejavu.Arena()
from model import Album, Film, Photo
def connect():

conf = {'Connect': "host=localhost dbname=Photoblog user=test password=test"}
arena.add_store("main", "postgres", conf)
arena.register_all(globals())

The arena in the above code will be our interface between the underlying storage manager and the business logic layer.

The connect function adds a storage manager to the arena object for a PostgreSQL RDBMS.

Once, the connection is obtained, we can create forms as per business requirements and complete the working of application.

The most important thing before creation of any application is entity mapping and designing the structure of the application.



Advertisements
E-Books Store