Package couchdbkit :: Package ext :: Package django
[hide private]
[frames] | no frames]

Source Code for Package couchdbkit.ext.django

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (c) 2008-2009 Benoit Chesneau <benoitc@e-engura.com>  
  4  # 
  5  # Permission to use, copy, modify, and distribute this software for any 
  6  # purpose with or without fee is hereby granted, provided that the above 
  7  # copyright notice and this permission notice appear in all copies. 
  8  # 
  9  # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
 10  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
 11  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
 12  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 13  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 14  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 15  # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 16   
 17  """ 
 18  Extension to use couchdbkit in Django 1.x. It allows you to use couchdbkit 
 19  easily in your django projects. 
 20   
 21  Just add to your settings the `COUCHDB_DATABASES` that defines  
 22   
 23          COUCHDB_DATABASES = ( 
 24              ('djangoapp.greeting', 'http://127.0.0.1:5984/greeting'), 
 25          ) 
 26   
 27  This one define the db greeting on url `http://127.0.0.1:5984/greeting` 
 28  for the application `greeting`of djangoapp project. 
 29   
 30   
 31  Then add extension to your INSTALLED_APPS before all applications using 
 32  couchdbkit documents : 
 33   
 34      INSTALLED_APPS = ( 
 35          .... 
 36          'couchdbkit.ext.django', 
 37          .... 
 38      ) 
 39   
 40  Add your documents objects in models.py :  
 41   
 42      from couchdbkit.ext.django.schema import * 
 43      class Greeting(Document): 
 44          author = StringProperty() 
 45          content = StringProperty(required=True) 
 46          date = DateTimeProperty(default=datetimee.utcnow) 
 47   
 48  and use it in your views.py : 
 49           
 50      class GreetingForm(DocumentForm): 
 51           
 52          class Meta: 
 53              document = Greeting 
 54   
 55      def home(request): 
 56           
 57          greet = None 
 58           
 59          if request.POST: 
 60              form = GreetingForm(request.POST) 
 61              if form.is_valid(): 
 62                  greet = form.save()   
 63          else: 
 64              form = GreetingForm() 
 65               
 66          greetings = Greeting.view('greeting/all') 
 67           
 68          return render("home.html", { 
 69              "form": form, 
 70              "greet": greet, 
 71              "greetings": greetings 
 72          }, context_instance=RequestContext(request) 
 73   
 74  You could notice in this example the `DocumentForm` object.  
 75  This object works like the ModelForm object but for couchdb 
 76  documents. Very easy. 
 77   
 78  Views/shows/lists are created in _design folder of your application. 
 79  exemple : 
 80   
 81      /yourapp 
 82      /yourapp/_design 
 83      /yourapp/_design/views 
 84      /yourapp/_design/views/viewname 
 85      /yourapp/_design/views/viewname/map.js 
 86      .... 
 87   
 88  To create databases and sync views, just run the usual `syncdb` command. 
 89  It won't destroy your datas, just synchronize views. 
 90  """ 
 91   
 92  from django.db.models import signals 
 93   
94 -def syncdb(app, created_models, verbosity=2, **kwargs):
95 """ function used by syncdb signal """ 96 from couchdbkit.ext.django.loading import couchdbkit_handler 97 couchdbkit_handler.sync(app, verbosity=verbosity)
98 99 signals.post_syncdb.connect(syncdb) 100