<<

web2py Application Development Cookbo

Over 110 recipes to master this full-stack Python

Mariano Reingart

Bruno Cezar Rocha

Jonathan Lundell

Pablo Martin Mulone

Michele Comitini

Richard Gordon

Massimo Di Pierro

open source [ ] community experience distilled PUBLISHING*"

BIRMINGHAM-MUMBAI Table of Contents

Preface 1

Chapter 1: Deploying web2py 7 Introduction 7

Installing web2py on Windows (from source code) 8 Installing web2py in Ubuntu 10 Setting up a production deployment on Ubuntu 12 Running web2py with Apache, mod_proxy, and mod_rewrite 16 Running web2py with 23 Running web2py with 26 Running web2py with and uWSGI 31 Running web2py on shared hosts using CGI 34 Running web2py on shared hosts with mod_proxy 35 Running web2py from a user-defined folder 36 Installing web2py as a service in Ubuntu 39 Running web2py with IIS as a proxy 39 Running web2py with ISAPI 45

Chapter 2: Building Your First Application 49 Introduction 50 Improving the scaffolding application 50 Building a simple contacts application 53 Building a Reddit clone 61 Building a Facebook clone 68 Using crud.archive 76 Converting an existing static site into a web2py application 78 Creating semi-static pages (flatpages) 79 Adding your custom logo 84 Table of Contents

Creating menus and submenus 87 Customizing menus with icons 88 Creating a navigation bar 89 Using cookies to set the language 92 Designing modular applications 94 96 Speeding up downloads Chapter 3: Database Abstraction Layer 99 Introduction 99 Creating a new model 100 Creating a model from a CSV file 102 Batch upload of your data 104 Moving your data from one database to another 106 Creating a model from existing MySQL and PostgreSQL databases 107 Efficiently searching by tag 110 Accessing your database from multiple applications 112 Hierarchical category tree 114 Creating records on demand 116 OR, LIKE, BELONGS, and more on 117 Replacing slow virtual fields with DB views 121 Chapter 4: Advanced Forms 125 Introduction 126 Adding confirmation on submit 127 Searching data dynamically 128 Embedding multiple forms in one page 130 Detecting and blocking concurrent updates 133 Creating a form wizard 134 De-normalizing data temporarily 136 Removing form labels 138 Using fileuploader.js 139 Uploading files using a LOADed component 142 Making image thumbnails from uploaded images 144 Monitoring upload progress 146 Auto tooltips in forms 148 Color picker widget 150 Shortening text fields 151 Creating multi-table forms 153 Creating a multi-table form with references 154 Creating a multi-table update form 156 Star rating widget 158

Hjl Table of Contents

Chapter 5: Adding Effects 161 Introduction 161 Using .multiselect.js 162 Creating a select_or_add widget 163 Using an autocompletion plugin 169 Creating a drop-down date selector 171 Improving the built-in ajax function 173 Using a slider to represent a number 174 Using jqGrid and web2py 175 Improving data tables with WebGrid 180 Ajaxing your search functions 183 Creating sparklines 187 Chapter 6: Using Third-party Libraries 191 Introduction 191 Customizing logging 191 Aggregating feeds 195 Displaying Tweets 197 Plotting with matplotlib 200 Extending PluginWiki with an RSS widget 203 Chapter 7: Web Services 207 Introduction 207 Consuming a web2py JSON service with JQuery 208 Consuming a JSON-RPC service 210 JSON-RPC from JavaScript 211 Making amf3 RPC calls from Flex using pyamf 220 PayPal integration in Web2py 222 PayPal web payments standard 235 Getting Flickr photos 243 Sending e-mails with Boto through Amazon Web Services (AWS) 245 Making GIS amps using mapscript 246 Google groups and Google code feeds reader 248 Creating SOAP web services 248

Chapter 8: Authentication and Authorization 253 Introduction 253 Customizing Auth 254 Using CAPTCHA on login failure 255 Using pyGravatar to get avatars for user profile pages 256 Multi-user and teacher modes 262 Authenticating with Facebook using OAuth 2.0 263

COD- Table of Contents

Chapter 9: Routing Recipes 267 Introduction 267 Making cleaner URLs with routes.py 268 Creating a simple router 270 Adding a URL prefix 272 Associating applications with domains 272 Omitting the application name 273 Removing application name and controllers from URLs 274 Replacing underscores with hyphens in URLs 275 Mapping favicons.ico and robots.txt 275 Using URLs to specify the language 276

Chapter 10: Reporting Recipes 279 Introduction 279 Creating PDF reports 279 Creating PDF listings 282 Creating pdf labels, badges, and invoices 284

Chapter 11: Other Tips and Tricks 295 Introduction 295 Using PDB and the embedded web2py debugger 296 with and PyDev 302 Updating web2py using a shell script 304 Creating a simple page statistics plugin 307 Rounding corners without images or JavaScript 308 Setting a cache.disk quota 310 Checking if web2py is running using 311 Building a Mercurial plugin 312 Building a pingback plugin 315 Changing views for mobile browsers 323 Background processing with a database queue 324 How to effectively use template blocks 327 Making standalone applications with web2py and wxPython 330

Index 335

-DD