def upload(cls): """Uploads all the local views from :attr:`VIEW_PATHS` directory to CouchBase server This method **over-writes** all the server-side views with the same named ones coming from :attr:`VIEW_PATHS` folder. """ cls._check_folder() os.chdir(cls.VIEWS_PATH) buckets = dict() # iterate local folders for bucket_name in os.listdir(cls.VIEWS_PATH): if not os.path.isdir(bucket_name): continue # get bucket object if bucket_name not in buckets: try: bucket = Connection.bucket(bucket_name) except BucketUnavailableException as why: print "[WARNING] %s" % str(why) continue else: buckets[bucket_name] = bucket else: bucket = buckets[bucket_name] # go through design docs for ddoc_name in os.listdir(bucket_name): views_path = '%s/%s/views' % (bucket_name, ddoc_name) spatial_path = '%s/%s/spatial' % (bucket_name, ddoc_name) if not (os.path.exists(views_path) and os.path.isdir(views_path)) and \ not (os.path.exists(spatial_path) and os.path.isdir(spatial_path)): continue # initialize design doc new_ddoc = { 'views': {}, 'spatial': {}, } # map and reduces if os.path.exists(views_path) and os.path.isdir(views_path): for filename in os.listdir(views_path): if not os.path.isfile('%s/%s' % (views_path, filename)) or \ not filename.endswith(('.map.js', '.reduce.js')): continue view_name, view_type, js = filename.rsplit('.', 2) if view_name not in new_ddoc['views']: new_ddoc['views'][view_name] = {} with open('%s/%s' % (views_path, filename), 'r') as f: new_ddoc['views'][view_name][view_type] = f.read() # spatial views if os.path.exists(spatial_path) and os.path.isdir(spatial_path): for filename in os.listdir(spatial_path): if not os.path.isfile('%s/%s' % (spatial_path, filename)) or \ not filename.endswith('.spatial.js'): continue view_name = filename.rsplit('.', 2)[0] with open('%s/%s' % (spatial_path, filename), 'r') as f: new_ddoc['spatial'][view_name] = f.read() bucket['_design/%s' % ddoc_name] = new_ddoc print 'Uploaded design document: %s' % ddoc_name pass
def bucket(self): """Returns the couchbase Bucket object for this instance, object property. :returns: See: :class:`couchbase.client.Bucket`. :rtype: :class:`couchbase.client.Bucket` """ return Connection.bucket(self.__bucket_name__)
def close_connection(self): Connection.close()
#! /usr/bin/env python import datetime from couchbasekit import Document, Connection from couchbasekit.fields import EmailField, ChoiceField from example.samples.publisher import Publisher from example.samples.book import Book Connection.auth('couchbasekit_samples', 'couchbasekit') class Gender(ChoiceField): CHOICES = { 'M': 'Male', 'F': 'Female', } class Author(Document): __bucket_name__ = 'couchbasekit_samples' __key_field__ = 'slug' # optional doc_type = 'author' structure = { 'slug': unicode, 'first_name': unicode, 'last_name': unicode, 'gender': Gender, 'email': EmailField, 'publisher': Publisher, # kind of foreign key 'books': [Book], # 1-to-many, or many-to-many? some-to-some.. :) 'has_book': bool, 'age': int, 'birthday': datetime.date,
#! /usr/bin/env python import datetime from couchbasekit import Document, Connection Connection.auth('couchbasekit_samples', 'couchbasekit') class Book(Document): __bucket_name__ = 'couchbasekit_samples' doc_type = 'book' structure = { 'title': unicode, 'published_at': datetime.date, 'pictures': list, 'tags': [unicode], 'category': { u'History': bool, u'Sci-Fiction': bool, u'Cooking': { u'Turkish': bool, u'Italian': bool, u'Fast Food': bool, u'Dessert': bool, }, }, }