O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Grok 1.0 Web Development

Book Description

Create flexible, agile web applications using the power of Grok—a Python web framework

  • Develop efficient and powerful web applications and web sites from start to finish using Grok, which is based on Zope 3

  • Integrate your applications or web sites with relational databases easily

  • Extend your applications using the power of the Zope Toolkit

  • Easy-to-follow and packed with practical, working code with clear explanations

  • In Detail

    Python is a very popular language for web application development. It's easy to learn and powerful enough to program any kind of system. There are many options for Python web development frameworks, from lightweight packages to everything-and-the-kitchen sink solutions. However, Grok offers a solid foundation for developing powerful, extensible, and secure web applications without too much complexity. This is because it is based on a very advanced object-oriented web framework: Zope 3. Grok enables web developers to tap into the power of Zope 3, thus giving a range of useful features for our applications with very little code.

    This book offers Python developers a comprehensive understanding of Grok and a look at its main features by way of developing and evolving a sample application from concept to deployment. You will learn how to use Grok to create web applications and how Grok offers you an agile and enjoyable development experience.

    This book will introduce you to the Grok web development framework and show you how to create an application from start to finish. You will learn how to create an application using mostly page templates and then add features to it while reworking the code as it grows. You will learn how to use models to describe your application's domain and how to connect those models to views for presenting information to your users. You will also learn how to automatically generate forms from the models and how to store the data and make it searchable. Due to the public nature of the Web, security is an important part of any web application and you will learn how Grok's integrated security can be used to protect your application and grant access to specific users and roles when needed. Grok offers a powerful object database for storage, but can also interact nicely with any database directly or using an object-relational mapper. You will learn how to do both and when it's advisable to use each. You will also learn how to extend a third-party Grok application, how to debug it, and how to deploy it. By the end of the book, you will be able to design and develop a complete web application using Grok.

    Table of Contents

    1. Grok 1.0 Web Development
      1. Grok 1.0 Web Development
      2. Credits
      3. Foreword
      4. About the Author
      5. Acknowledgement
      6. About the Reviewers
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      8. 1. Getting to Know Grok
        1. Why Grok?
        2. Distinguishing Grok concepts
          1. The Zope Component Architecture
          2. Object database
          3. Object publishing and traversal
        3. Other Grok features
          1. Integrated security model
          2. Easy use of existing Python libraries and modules
          3. Static resources
          4. Relational database access
          5. Python Web Server Gateway Interface compatibility
        4. How Grok compares to other web frameworks
          1. PHP
          2. Java frameworks
          3. Ruby on Rails
          4. Django
          5. Pylons
        5. Summary
      9. 2. Getting Started with Grok
        1. Getting a C compiler
        2. Installing Python
          1. Installing Python on Unix/Linux
          2. Installing Python on Mac OS X
          3. Installing Python on Windows
        3. EasyInstall and the Python Package Index (PyPI)
        4. Grok and the Python environment
        5. Virtualenv
          1. Installing Grok using grokproject
          2. Creating our first project
          3. Running the default application
        6. What's inside a Grok project?
          1. Overview of the application structure
          2. Creating our first template
        7. The Grok admin UI
          1. Applications
          2. Server control
          3. Documentation
        8. Summary
      10. 3. Views
        1. Grok views
        2. Zope Page Templates (ZPT)
          1. The Template Attribute Language (TAL)
          2. Expression types
          3. Inserting text
          4. Repeating tags
          5. Conditional elements
          6. Variables
          7. Special variables
          8. Modifying HTML tag attributes
          9. Inserting structure
          10. Multiple statements in one tag
          11. Macros and slots
          12. Going beyond ZPT basics
        3. The to-do list application
        4. Handling form data
        5. Adding static resources
        6. Putting in some JavaScript flourish
        7. Summary
      11. 4. Models
        1. Models and applications
        2. Storing model data
          1. The ZODB
          2. Changing the title dynamically
          3. The rules of persistence
        3. Display logic and application logic
          1. Separating display and application logic
        4. Using containers and multiple models
          1. Adding containers to our application
          2. Adding behavior to the models
          3. The TodoList model
          4. Reorganizing the views
          5. Introducing class annotations
          6. Setting view names explicitly
          7. Redirecting views
        5. The Grok introspector
        6. Summary
      12. 5. Forms
        1. A quick demonstration of automatic forms
        2. Interfaces
          1. Example of an interface
          2. Interfaces in Grok
        3. Schemas
          1. Available schema attributes and field types
        4. Form fields and widgets
        5. Form components
          1. Adding a project container at the root of the site
          2. Form actions
          3. Trying out the application
        6. Filtering fields
        7. Using grok.EditForm
        8. Modifying individual form fields
        9. Form validation
        10. Customizing the form template
        11. Summary
      13. 6. The Catalog: An Object-Oriented Search Engine
        1. Catalogs and indexes
        2. Adding a simple search function to the application
          1. Defining a simple index
          2. Creating the search view
          3. Creating a template to display the search results
        3. A brief diversion: Structuring our application for search
        4. Back to search: Using multiple indexes
        5. Indexing different kinds of objects
        6. The catalog admin UI
        7. Types of indexes
        8. Summary
      14. 7. Security
        1. Authentication and authorization
        2. Principals, permissions, and roles
        3. Security policies
          1. Default Grok security policy definition
          2. Modifying the security policy
            1. Modifying the default view permissions
            2. Adding a new user
            3. Protecting a view
        4. Setting up a custom security policy
          1. Creating permissions
          2. Roles
        5. Adding authentication
          1. The Pluggable Authentication Utility
          2. Registering PAU with our application
          3. Adding a credentials plugin
            1. The login form
            2. The logout view
          4. The UserAuthenticator plugin
            1. Adding users
            2. Basic user management
        6. Summary
      15. 8. Application Presentation and Page Layout
        1. Viewlets and viewlet managers
          1. Viewlets
          2. Viewlet managers
        2. Understanding how the pieces fit together
          1. View methods and attributes
          2. Viewlet methods and attributes
          3. Viewlet manager methods and attributes
        3. To-do list manager layout
          1. Defining viewlet managers in application code
          2. Registering viewlets
          3. Modifying existing views to use the master template
          4. Inserting forms into viewlets
          5. Using viewlets to insert different static resources
        4. Layers and skins
          1. Skins in Grok
        5. Adding themes to our application
          1. Overriding a viewlet
          2. Using the new skins
        6. Summary
      16. 9. Grok and the ZODB
        1. How the ZODB works
          1. Transparency
          2. ACID properties
          3. Other ZODB features
          4. ZEO
        2. Using the ZODB
          1. The rules of persistence
          2. Traversal
          3. How Grok uses the ZODB
        3. Adding Blob support to our application
          1. Adding messages to projects
            1. The megrok.form package
            2. Adding an external package to our project
            3. How blob support is configured in Grok
            4. The messaging class and schema
            5. The message viewlet
            6. The message list template
            7. Downloading an attachment
            8. Testing the Blob support
        4. Taking advantage of transactions
          1. Using zope.app.undo
          2. Testing the undo functionality
        5. ZODB maintenance
          1. File storage
          2. Automatic packing
          3. Backing up
          4. Using the ZODB without Grok
        6. Summary
      17. 10. Grok and Relational Databases
        1. Object Relational Mappers
          1. SQLAlchemy
          2. Including SQLAlchemy in our Grok project
            1. Using SQLAlchemy
        2. Using a relational database for authentication
        3. Handling database transactions securely
        4. Creating database-backed models and containers
          1. The megrok.rdb package
          2. Making an application database backed
        5. When to use the ZODB versus a relational database
        6. Summary
      18. 11. Key Concepts Behind Grok
        1. The Zope Component Architecture
          1. Interfaces
          2. Adapters
            1. Real-world adapters
            2. Defining and using an adapter
            3. Adapters that we have used in our application
          3. Utilities
            1. Global utilities
            2. Local utilities
          4. Events
        2. Extending a Grok application from the outside
          1. Preparing the original application
          2. The to-do plus package
            1. Creating the new package
          3. Adding a skin chooser form to the application
            1. Adding a new skin
            2. The skin chooser form
            3. Using events to show the selected skin
          4. Sending e-mail notifications on content creation
            1. Object annotations
            2. Creating the form
            3. Sending the e-mail
          5. Displaying additional navigation links
        3. Summary
      19. 12. Grokkers, Martian, and Agile Configuration
        1. Agile configuration
        2. The Martian library
        3. Understanding grokkers
          1. The grokked class
          2. The directives
          3. The class grokker
          4. The only ZCML you'll ever need
        4. Creating our own grokker for zope.sendmail configuration
          1. Creating the package
          2. Writing our grokker
          3. Using mailgrokker
        5. Summary
      20. 13. Testing and Debugging
        1. Testing
          1. Unit testing
          2. Functional tests
        2. Testing in Grok
          1. Doctests
          2. Default test setup for Grok projects
            1. Test configuration
            2. Test files
            3. Running the tests
          3. The test runner
            1. Test layers
            2. Invoking the test runner
          4. Looking at the test code
            1. ftesting.zcml
            2. tests.py
            3. app.txt
          5. Adding our own tests
            1. The Browser class
            2. Our first to-do application tests
            3. Adding unit tests
            4. Extending the test suite
        3. Debugging
          1. Debugging in Grok
            1. The default Ajax debugger
            2. Post-mortem debugging with the Python debugger
        4. Summary
      21. 14. Deployment
        1. Moving an application to production mode
        2. Running the paster server in the background
        3. Running Grok behind the Apache web server
          1. Using mod_rewrite to serve our application from Apache
          2. Installing and setting up Grok under mod_wsgi
            1. WSGI: The Python Web Server Gateway Interface
            2. Grok and WSGI
            3. Why use Apache and mod_wsgi for Grok?
            4. Setting up a clean Linux server
            5. Installing and configuring mod_wsgi
            6. Configuring a Grok site under mod_wsgi
            7. Getting the to-do application ready for mod_wsgi
            8. Configuring an Apache site to use mod_wsgi
              1. The PYTHON_EGG_CACHE directory
            9. Running the application
          3. Adding a ZEO server
            1. Buildout recipe for the ZEO configuration
            2. Configuring the ZEO client
            3. Launching the ZEO server
            4. Augmenting the number of processes
        4. More scalability: Adding caching and load balancing
          1. Caching proxies
          2. Load balancers
          3. A high-traffic architecture for Grok
        5. Summary