def sync_design_docs(db, design_dir, design_name, temp=None): """ pushes design documents and brings new index up to date if temp for example it can be used to push new changes to the myapp design doc to _design/myapp-tmp and then trigger an index of _design/myapp-tmp """ design_name_ = '%s-%s' % (design_name, temp) if temp else design_name docid = "_design/%s" % design_name_ push(design_dir, db, force=True, docid=docid) log.info("synced '%s' in couchdb", design_name) if temp: # found in the innards of couchdbkit view_names = db[docid].get('views', {}).keys() if len(view_names) > 0: log.info('Triggering view rebuild') view = '%s/%s' % (design_name_, view_names[0]) while True: try: list(db.view(view, limit=0)) except RequestFailed as e: if 'timeout' not in e.message: raise else: break
def reset_db(self): super(PrototypeCouch, self).reset_db() for db in self.dbs.values(): url = self._get_couch_url(db) print('Dropping couch', url) response = requests.delete(url, auth=self.auth) if response.status_code not in (200, 404): raise Exception("Failed to delete couch database: {}\n{}".format(url, response.text)) for db in self.dbs.values(): url = self._get_couch_url(db) print('Creating couch', url) response = requests.put(url, auth=self.auth) if not response.status_code == 201: raise Exception("Failed to create couch database: {}\n{}".format(url, response.text)) # create views # This is hacky and I hate it design_dir = os.path.join( os.path.dirname(__file__), '..', 'prototype', 'mobile_endpoint', 'backends', 'couch', '_designs',) for app_name in os.listdir(design_dir): folder = os.path.join(design_dir, app_name) push(folder, Database(self._get_couch_url(self.dbs[app_name], credentials=True)), force=True, docid='_design/{}'.format(app_name)) # The couch backend uses sql for some things, like OwnershipCleanlinessFlags print('Running db upgrade') self._run_manage_py('db', 'upgrade')
def sync(self, server, dbname, view, module, verbosity=2): """ Used to sync views of all applications and eventually create database. @param server: couchdb server object @param dbname: name of the database @param view: 'view' name @param module: module, provided here to calculate each view's _design/ path. """ print "Sync `%s` in CouchDB server." % view db = server.get_or_create_db(dbname) app_module_path = module.__name__ app_name = app_module_path.split(".")[-1] app_dir = dirname(module.__file__) design_path = join(app_dir, "_design") if not isdir(design_path): print >> sys.stderr, \ "%s doesn't exists, doc wasn't synchronized" % design_path else: push(design_path, db, force=True, docid="_design/%s" % app_name) print "Sync of `%s` done." % view
def sync_design_docs(app, **kwargs): """Function used by syncdb signal""" app_name = app.__name__.rsplit('.', 1)[0] app_label = app_name.split('.')[-1] if app_label == "couchapps": dir = os.path.abspath(os.path.dirname(__file__)) for d in [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]: push(os.path.join(dir, d), get_db(), force=True, docid="_design/%s" % d) print "synced couchapp %s in couchdb" % d
def sync(self, app, verbosity=2, temp=None): """ used to sync views of all applications and eventually create database. When temp is specified, it is appended to the app's name on the docid. It can then be updated in the background and copied over the existing design docs to reduce blocking time of view updates """ app_name = app.__name__.rsplit('.', 1)[0] app_labels = set() schema_list = list(self.app_schema.values()) for schema_dict in schema_list: for schema in list(schema_dict.values()): app_module = schema.__module__.rsplit(".", 1)[0] if app_module == app_name and not schema._meta.app_label in app_labels: app_labels.add(schema._meta.app_label) for app_label in app_labels: if not app_label in self._databases: continue if verbosity >= 1: print("sync `%s` in CouchDB" % app_name) db = self.get_db(app_label) app_path = os.path.abspath( os.path.join(sys.modules[app.__name__].__file__, "..")) design_path = "%s/%s" % (app_path, "_design") if not os.path.isdir(design_path): if settings.DEBUG: print("%s don't exists, no ddoc synchronized" % design_path, file=sys.stderr) return if temp: design_name = '%s-%s' % (app_label, temp) else: design_name = app_label docid = "_design/%s" % design_name push(os.path.join(app_path, "_design"), db, force=True, docid=docid) if temp: ddoc = db[docid] view_names = list(ddoc.get('views', {}).keys()) if len(view_names) > 0: if verbosity >= 1: print('Triggering view rebuild') view = '%s/%s' % (design_name, view_names[0]) list(db.view(view, limit=0))
def sync_design_docs(db, design_dir, design_name, temp=None): design_name_ = "%s-%s" % (design_name, temp) if temp else design_name docid = "_design/%s" % design_name_ push(design_dir, db, force=True, docid=docid) print "synced '%s' in couchdb" % design_name if temp: # found in the innards of couchdbkit try: view_names = db[docid].get("views", {}).keys() if len(view_names) > 0: print "Triggering view rebuild" view = "%s/%s" % (design_name, view_names[0]) list(db.view(view, limit=0)) except Exception, ex: print "\tError trying to sync couchapp %s, but ignoring %s" % (docid, ex)
def sync(self, app, verbosity=2, temp=None): """ used to sync views of all applications and eventually create database. When temp is specified, it is appended to the app's name on the docid. It can then be updated in the background and copied over the existing design docs to reduce blocking time of view updates """ app_name = app.__name__.rsplit('.', 1)[0] app_labels = set() schema_list = self.app_schema.values() for schema_dict in schema_list: for schema in schema_dict.values(): app_module = schema.__module__.rsplit(".", 1)[0] if app_module == app_name and not schema._meta.app_label in app_labels: app_labels.add(schema._meta.app_label) for app_label in app_labels: if not app_label in self._databases: continue if verbosity >=1: print("sync `%s` in CouchDB" % app_name) db = self.get_db(app_label) app_path = os.path.abspath(os.path.join(sys.modules[app.__name__].__file__, "..")) design_path = "%s/%s" % (app_path, "_design") if not os.path.isdir(design_path): if settings.DEBUG: sys.stderr.write("%s don't exists, no ddoc synchronized" % design_path) return if temp: design_name = '%s-%s' % (app_label, temp) else: design_name = app_label docid = "_design/%s" % design_name push(os.path.join(app_path, "_design"), db, force=True, docid=docid) if temp: ddoc = db[docid] view_names = ddoc.get('views', {}).keys() if len(view_names) > 0: if verbosity >= 1: print('Triggering view rebuild') view = '%s/%s' % (design_name, view_names[0]) list(db.view(view, limit=0))
def sync_design_docs(db, design_dir, design_name, temp=None): """ pushes design documents and brings new index up to date if temp for example it can be used to push new changes to the myapp design doc to _design/myapp-tmp and then trigger an index of _design/myapp-tmp """ design_name_ = '%s-%s' % (design_name, temp) if temp else design_name docid = "_design/%s" % design_name_ push(design_dir, db, force=True, docid=docid) log.info("synced '%s' in couchdb", design_name) if temp: index_design_docs(db, docid, design_name_)
def sync_design_docs(db, design_dir, design_name, temp=None): design_name_ = '%s-%s' % (design_name, temp) if temp else design_name docid = "_design/%s" % design_name_ push(design_dir, db, force=True, docid=docid) print "synced '%s' in couchdb" % design_name if temp: # found in the innards of couchdbkit try: view_names = db[docid].get('views', {}).keys() if len(view_names) > 0: print 'Triggering view rebuild' view = '%s/%s' % (design_name, view_names[0]) list(db.view(view, limit=0)) except Exception, ex: print "\tError trying to sync couchapp %s, but ignoring %s" % ( docid, ex)
def sync_design_docs(db, temp=None): dir = os.path.abspath(os.path.dirname(__file__)) for d in get_couchapps(): design_name = '%s-%s' % (d, temp) if temp else d docid = "_design/%s" % design_name push(os.path.join(dir, d), db, force=True, docid=docid) print "synced couchapp %s in couchdb" % d if temp: # found in the innards of couchdbkit try: view_names = db[docid].get('views', {}).keys() if len(view_names) > 0: print 'Triggering view rebuild' view = '%s/%s' % (design_name, view_names[0]) list(db.view(view, limit=0)) except Exception, ex: print "\tError trying to sync couchapp %s, but ignoring %s" % (docid, ex)
def sync(self, app, verbosity=2): """ used to sync views of all applications and eventually create database. """ app_name = app.__name__.rsplit('.', 1)[0] app_label = app_name.split('.')[-1] if app_label in self._databases: if verbosity >=1: print "sync `%s` in CouchDB" % app_name db = self.get_db(app_label) app_path = os.path.abspath(os.path.join(sys.modules[app.__name__].__file__, "..")) design_path = "%s/%s" % (app_path, "_design") if not os.path.isdir(design_path): if settings.DEBUG: print >>sys.stderr, "%s don't exists, no ddoc synchronized" % design_path return push(os.path.join(app_path, "_design"), db, force=True, docid="_design/%s" % app_label)
def sync(self, app, verbosity=2, temp=None): """ used to sync views of all applications and eventually create database. When temp is specified, it is appended to the app's name on the docid. It can then be updated in the background and copied over the existing design docs to reduce blocking time of view updates """ app_name = app.__name__.rsplit('.', 1)[0] app_label = app_name.split('.')[-1] if app_label in self._databases: if verbosity >=1: print "sync `%s` in CouchDB" % app_name db = self.get_db(app_label) app_path = os.path.abspath(os.path.join(sys.modules[app.__name__].__file__, "..")) design_path = "%s/%s" % (app_path, "_design") if not os.path.isdir(design_path): if settings.DEBUG: print >>sys.stderr, "%s don't exists, no ddoc synchronized" % design_path return if temp: design_name = '%s-%s' % (app_label, temp) else: design_name = app_label docid = "_design/%s" % design_name push(os.path.join(app_path, "_design"), db, force=True, docid=docid) if temp: ddoc = db[docid] view_names = ddoc['views'].keys() if len(view_names) > 0: if verbosity >= 1: print 'Triggering view rebuild' view = '%s/%s' % (design_name, view_names[0]) list(db.view(view, limit=0))
def sync(self, app_name, server, dbname, verbosity=2): """ Used to sync views of all applications and eventually create database. """ print "Sync `%s` in CouchDB server." % app_name db = server.get_or_create_db(dbname) app_label = app_name.split('.')[-1] app_path = os.path.abspath(os.path.join("./", app_label.replace(".", "/"))) design_path = "%s/%s" % (app_path, "_design") if not os.path.isdir(design_path): print >> sys.stderr, \ "%s don't exists, no ddoc synchronized" % design_path else: push(os.path.join(app_path, "_design"), db, force=True, docid="_design/%s" % app_label) print "Sync of `%s` done." % app_name
def sync_design_docs(db): dir = os.path.abspath(os.path.dirname(__file__)) for d in [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]: push(os.path.join(dir, d), db, force=True, docid="_design/%s" % d) print "synced couchapp %s in couchdb" % d
login_manager = LoginManager() login_manager.init_app(app) # server object server = Server() # create databases db = server.get_or_create_db("post") userdb = server.get_or_create_db("user") emaildb = server.get_or_create_db("email") # from models.post import Post # from models.user import User # # # associate Models to the db # Post.set_db(db) # User.set_db(userdb) #this is views we need on the server for searching and retrieving doc's #push the to server push('messageboard/designdocs/Post', db) import models from views import login, post, user, signup, home
def sync_design(db, path): """Synchronizes the design documents with the database passed in.""" push(path, db, force=true)
from flask_login import LoginManager #init the flask app app = Flask(__name__) app.secret_key = "troleolol" login_manager = LoginManager() login_manager.init_app(app) # server object server = Server() # create databases db = server.get_or_create_db("post") userdb = server.get_or_create_db("user") emaildb = server.get_or_create_db("email") # from models.post import Post # from models.user import User # # # associate Models to the db # Post.set_db(db) # User.set_db(userdb) #this is views we need on the server for searching and retrieving doc's #push the to server push('messageboard/designdocs/Post', db) import models from views import login, post, user, signup, home
def init_views(): design_dir = os.path.join(os.path.dirname(__file__), '_designs') for app_name in os.listdir(design_dir): folder = os.path.join(design_dir, app_name) push(os.path.join(design_dir, app_name), get_app_db(app_name), force=True, docid='_design/{}'.format(app_name))