1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Maintain registry of documents used in your django project
19 and manage db sessions
20 """
21
22 import sys
23 import os
24
25 import urllib
26 import urlparse
27
28 from couchdbkit import Server, contain, ResourceConflict
29 from couchdbkit import push
30 from couchdbkit.resource import CouchdbResource, PreconditionFailed
31 from django.conf import settings
32 from django.db.models import signals, get_app
33 from django.core.exceptions import ImproperlyConfigured
34 from django.utils.datastructures import SortedDict
35 from restkit import BasicAuth
36
37 COUCHDB_DATABASES = getattr(settings, "COUCHDB_DATABASES", [])
38 COUCHDB_TIMEOUT = getattr(settings, "COUCHDB_TIMEOUT", 300)
39
41 """ The couchdbkit handler for django """
42
43
44 __shared_state__ = dict(
45 _databases = {},
46 app_schema = SortedDict()
47 )
48
50 """ initialize couchdbkit handler with COUCHDB_DATABASES
51 settings """
52
53 self.__dict__ = self.__shared_state__
54
55
56 for app_name, uri in databases:
57
58 try:
59 if isinstance(uri, tuple):
60
61
62
63 server_uri, dbname = uri
64 else:
65 server_uri, dbname = uri.rsplit("/", 1)
66 except ValueError:
67 raise ValueError("couchdb uri [%s:%s] invalid" % (
68 app_name, uri))
69
70
71 res = CouchdbResource(server_uri, timeout=COUCHDB_TIMEOUT)
72
73 server = Server(server_uri, resource_instance=res)
74 app_label = app_name.split('.')[-1]
75 self._databases[app_label] = (server, dbname)
76
77 - def sync(self, app, verbosity=2):
78 """ used to sync views of all applications and eventually create
79 database.
80 """
81 app_name = app.__name__.rsplit('.', 1)[0]
82 app_label = app_name.split('.')[-1]
83 if app_label in self._databases:
84 if verbosity >=1:
85 print "sync `%s` in CouchDB" % app_name
86 db = self.get_db(app_label)
87
88 app_path = os.path.abspath(os.path.join(sys.modules[app.__name__].__file__, ".."))
89 design_path = "%s/%s" % (app_path, "_design")
90 if not os.path.isdir(design_path):
91 if settings.DEBUG:
92 print >>sys.stderr, "%s don't exists, no ddoc synchronized" % design_path
93 return
94
95 push(os.path.join(app_path, "_design"), db, force=True,
96 docid="_design/%s" % app_label)
97
98 - def get_db(self, app_label, register=False):
99 """ retrieve db session for a django application """
100 if register:
101 return
102
103 db = self._databases[app_label]
104 if isinstance(db, tuple):
105 server, dbname = db
106 db = server.get_or_create_db(dbname)
107 self._databases[app_label] = db
108 return db
109
111 """ register a Document object"""
112 for s in schema:
113 schema_name = schema[0].__name__.lower()
114 schema_dict = self.app_schema.setdefault(app_label, SortedDict())
115 if schema_name in schema_dict:
116 fname1 = os.path.abspath(sys.modules[s.__module__].__file__)
117 fname2 = os.path.abspath(sys.modules[schema_dict[schema_name].__module__].__file__)
118 if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
119 continue
120 schema_dict[schema_name] = s
121
123 """ retriev Document object from its name and app name """
124 return self.app_schema.get(app_label, SortedDict()).get(schema_name.lower())
125
126 couchdbkit_handler = CouchdbkitHandler(COUCHDB_DATABASES)
127 register_schema = couchdbkit_handler.register_schema
128 get_schema = couchdbkit_handler.get_schema
129 get_db = couchdbkit_handler.get_db
130