English 中文(简体)
CherryPy - Demo Application
  • 时间:2024-11-03

CherryPy - Demo Apppcation


Previous Page Next Page  

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

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

Basic Structure – Design of Entities

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

    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 apppcation, which has sub-directories for Photoblog apppcation. The sub-directories are Photo, Album, and Film which would include controllers.py, models.py and server.py.

Functionally, the Photoblog apppcation 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 apppcation, the connection with database is mandatory for API and to maintain the functionapty 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 apppcation.

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

Advertisements