def __init__(self, db_url, name=None):
     url = "{}/{}".format(db_url, name)
     get_or_create(db_url, name)
     super(TendersStorage, self).__init__(url=url)
     ViewDefinition.sync_many(self, [tenders_all,
                                     tenders_date_modified,
                                     tenders_date_modified_for_package])
Example #2
0
def sync_db(db):
    ViewDefinition.sync_many(db, [
        all_doc_tags,
        
        Article.all_months, Article.all_tags, Article.by_month,
        Article.by_tag, Article.by_time, Article.by_slug,
        
        Comment.by_time, Comment.comment_count, Comment.by_anytime,
        
        Documentation.by_path, Documentation.ids_for_version,
        Documentation.doc_key,
        
        Human.by_displayname, Human.by_email, Human.by_email_token,
        Human.by_openid, Human.by_password_token,
        
        Paste.by_author, Paste.by_tag, Paste.all_tags, Paste.by_time,
        Paste.by_old_id, Paste.by_tag_time, Paste.by_session_id,
        
        Rating.all_raters,
        
        Snippet.by_date, Snippet.by_author, Snippet.by_slug, Snippet.by_title,
        Snippet.by_author_id, Snippet.by_tag, Snippet.all_tags,
        Snippet.author_totals,
        
        Traceback.by_uuid, Traceback.by_time, Traceback.by_session_id,
    ])
Example #3
0
def get_db(server, db_name):
    try:
        db = server[db_name]
    except couchdb.ResourceNotFound:
        db = server.create(db_name)
    ViewDefinition.sync_many(db, db_views, remove_missing=True)
    return db
Example #4
0
 def sync(self, app):
     """
     This syncs the database for the given app. It will first make sure the
     database exists, then synchronize all the views and run all the
     callbacks with the connected database.
     
     It will run any callbacks registered with `on_sync`, and when the
     views are being synchronized, if a method called `update_design_doc`
     exists on the manager, it will be called before every design document
     is updated.
     
     :param app: The application to synchronize with.
     """
     server_url = app.config['COUCHDB_SERVER']
     db_name = app.config['COUCHDB_DATABASE']
     server = couchdb.Server(server_url)
     if 'COUCHDB_USERNAME' in app.config and 'COUCHDB_PASSWORD' in app.config:
         server.resource.credentials = (app.config['COUCHDB_USERNAME'],
                                        app.config['COUCHDB_PASSWORD'])
     if db_name not in server:
         db = server.create(db_name)
     else:
         db = server[db_name]
     OldViewDefinition.sync_many(db,
                                 tuple(self.all_viewdefs()),
                                 callback=getattr(self, 'update_design_doc',
                                                  None))
     for callback in self.sync_callbacks:
         callback(db)
def create_couchdb_views(app, created_models, verbosity, **kwargs):
    ViewDefinition.sync_many(db, [item_type_date, by_date])
    ContentType.objects.get_or_create(
        name='CouchDB Item',
        app_label='couch_lifestream',
        model='couchdbitem'
    )
Example #6
0
    def sync(self):
        # Synchronizing all couchdb views of the document class
        views = []

        # Spare a lit bit of space for Javascript views
        import re
        def _minify(language, code):
            if language == "javascript":
                return re.compile(r" ?(\W) ?").sub(lambda match: match.group(1),
                                                   code)
            else:
                return code

        for attr in self.doc_class.__dict__:
            value = getattr(self.doc_class, attr)
            if isinstance(value, ViewDefinition):
                if value.map_fun:
                    value.map_fun = _minify(value.language, value.map_fun)
                if value.reduce_fun:
                    value.reduce_fun = _minify(value.language, value.reduce_fun)
                views.append(value)

            elif isinstance(value, FilterFunction):
                value[value.language] = _minify(value.language, value[value.language])
                
        ViewDefinition.sync_many(self.database, views)
        sync_docs(self.database, [self.doc_class])
Example #7
0
 def sync(self, app):
     """
     This syncs the database for the given app. It will first make sure the
     database exists, then synchronize all the views and run all the
     callbacks with the connected database.
     
     It will run any callbacks registered with `on_sync`, and when the
     views are being synchronized, if a method called `update_design_doc`
     exists on the manager, it will be called before every design document
     is updated.
     
     :param app: The application to synchronize with.
     """
     server_url = app.config['COUCHDB_SERVER']
     db_name = app.config['COUCHDB_DATABASE']
     server = couchdb.Server(server_url)
     if 'COUCHDB_USERNAME' in app.config and 'COUCHDB_PASSWORD' in app.config:
       server.resource.credentials = (app.config['COUCHDB_USERNAME'], app.config['COUCHDB_PASSWORD'])
     if db_name not in server:
         db = server.create(db_name)
     else:
         db = server[db_name]
     OldViewDefinition.sync_many(
         db, tuple(self.all_viewdefs()),
         callback=getattr(self, 'update_design_doc', None)
     )
     for callback in self.sync_callbacks:
         callback(db)
Example #8
0
    def __init__(self, host_url, db_name):
        server = couchdb.Server(host_url)
        if server.uuids():
            self.uid = server.uuids()[0]
        self.db = get_or_create_db(server, db_name)

        ViewDefinition.sync_many(self.db, [releases_ocid, releases_id])
        LOGGER.info("Starting storage: {}".format(self.db.info()))
Example #9
0
def sync_design_databases(server: Server, db_names: dict):
    # specific databases views
    for db_key, views in VIEWS.items():
        views.append(conflicts_view)  # should be in every db
        db_name = db_names[db_key]
        ViewDefinition.sync_many(server[db_name],
                                 views,
                                 callback=add_index_options)
def upload_views(db_name, map_funcs):
    """
    Wrapper to syncronize a view

    @author BrendonCrawford
    """
    ViewDefinition.sync_many(SERVER[db_name], map_funcs, True)
    print "Uploaded views: <%s>" % db_name
    return True
Example #11
0
def upload_views(db_name, map_funcs):
    """
    Wrapper to syncronize a view

    @author BrendonCrawford
    """
    ViewDefinition.sync_many(SERVER[db_name], map_funcs, True)
    print "Uploaded views: <%s>" % db_name
    return True
Example #12
0
    def setUp(self):
        self.server = Server('http://localhost:5984')
        self.db_name = 'test_%s' % uuid4()
        self.server.create(self.db_name)

        db = self.server[self.db_name]
        ViewDefinition.sync_many(db, couchdb_views)
        self.db = CouchDBDatabase(db, lambda: six.text_type(uuid4()))
        super(TestCouchDBBackend, self).setUp()
Example #13
0
 def _sync_views(self):
     ViewDefinition.sync_many(self.adb, VIEWS)
     _id = '_design/report'
     original = self.adb.get(_id)
     original['views']['lib'] = {
         'jsonpatch': jsonpatch,
         'tenders': tenders_lib,
         'bids': bids_lib
     }
     self.adb.save(original)
Example #14
0
 def _sync_views(self):
     ViewDefinition.sync_many(self.adb, VIEWS)
     _id = '_design/report'
     original = self.adb.get(_id)
     original['views']['lib'] = {
         'jsonpatch': jsonpatch,
         'tenders': tenders_lib,
         'bids': bids_lib
     }
     self.adb.save(original)
Example #15
0
def create_db(name):
    dbm = get_db_manager('http://localhost:5984/', name)
    views = []
    for v in view_js.keys():
        funcs = view_js[v]
        map = (funcs['map'] if 'map' in funcs else None)
        reduce = (funcs['reduce'] if 'reduce' in funcs else None)
        views.append(ViewDefinition(v, v, map, reduce))

    ViewDefinition.sync_many(dbm.database, views)
    return dbm
Example #16
0
def action_syncviews():
	"""
	Synchronize views to CouchDB.
	"""
	from webui import couchdb_views
	from couchdb.design import ViewDefinition
	import webui.db
	try:
		ViewDefinition.sync_many(webui.db.database,
			couchdb_views.view_definitions)
	except AttributeError:
		print 'Error: CouchDB must not be running'
		sys.exit(1)
 def sync(self, app=None):
     """
     This syncs the database for the given app. It will first make sure the
     database exists, then synchronize all the views and run all the
     callbacks with the connected database.
     
     It will run any callbacks registered with `on_sync`, and when the
     views are being synchronized, if a method called `update_design_doc`
     exists on the manager, it will be called before every design document
     is updated.
     
     :param app: The application to synchronize with.
     """
     db = self.db
     CouchDBViewDefinition.sync_many(
         db, tuple(self.all_viewdefs()),
         callback=getattr(self, 'update_design_doc', None)
     )
     for callback in self.sync_callbacks:
         callback(db)
Example #18
0
 def sync_log_views(self, log_db):
     """
     Import ViewDefinitions for the log database and sync them.        
     """
     print 'Importing view maker functions'
     
     # Import all view generator functions from the couchdb.views module
     from votersdaily_web.api.couchdb import log_views
     view_makers = [
         v for k, v in log_views.__dict__.items() if k.find('make_views') == 0]
     
     view_definitions = []
     
     for maker in view_makers:
         print 'Executing %s()' % maker.__name__
         view_definitions.extend(maker(log_db))
     
     print 'Syncing a total of %i views to CouchDB' % len(view_definitions)
     ViewDefinition.sync_many(log_db, view_definitions, remove_missing=True)
     
     print 'Finished'
Example #19
0
	def OnLogin(self, event):
		self.user = User()
		with dialog( dict(dialog = LoginDialog, user = self.user)) as val:
			"""
			do validation here
			"""
			if self.user.username == FAKE_USER and self.user.password == FAKE_PASSWORD:
				try:
					s = Server(self.URL)
					blog = s.create(BLOG)
					dlg = wx.MessageDialog(self, "Database {0} does not exist. Do you want to create it?".format(BLOG), "Database not found", style = wx.YES_NO)
					if dlg.ShowModal() == wx.ID_YES:
						from couchdb.design import ViewDefinition
						ViewDefinition.sync_many( blog, [Design.all, Design.by_date, Design.by_author, Design.tags, Design.attachments])
						p = Post()
						p.author = self.user.username
						p.subject = "Welcome Post"
						p.content = "First Post.  See that a <b>screenshot</b>  of your computer is included as attachment."
						p.date = datetime.now()
						p.tags = ["GENERAL", "WELCOME"]
						p.store(blog)
						sfile = REGEXP.sub("","screenshot{0}".format(datetime.now()))
						sfile = "{0}.png".format(sfile)
						screenshot = Screenshot(filename = sfile)
						doc = blog[p.id]
						f = open(sfile,"rb")
						blog.put_attachment(doc,f, sfile)
						f.close()

					else:
						del s[BLOG]
						dlg.Destroy()
						self.Close()
					dlg.Destroy()
				except:
					pass
			else:
				self.user.username = None
		if not self.user.username:
			self.Close()
Example #20
0
def setup_app(command, conf, vars):
    """Place any commands to setup troppotardi here"""
    config = load_environment(conf.global_conf, conf.local_conf)
    
    print "Syncing the couchdb database..."
    db = Database(config['couchdb_uri'])
    ViewDefinition.sync_many(db, [
            Image.pending_by_time, Image.by_day, Image.deleted_by_time,
            User.by_time, User.by_username,
            Email.by_time,
            ])

    if not list(User.by_username(db)):
        print "Creating first user - username and password \"admin\""
        admin = User()
        admin.username = "******"
        admin.password = "******"
        admin.email = "*****@*****.**"
        admin.role = "Admin"
        admin.store(db, revised_by=False)

    print "Done."
Example #21
0
    def sync_view(self, f, remove_missing=False):
        def get_file(filename):
            viewlist = []
            data = open(filename).read()
            data = json.loads(data)

            if data['_id'].startswith('_design/'):
                design = data['_id'][8:] # removing _design/
            else:
                design = data['_id']
            language = data['language']
            for k,v in data['views'].iteritems():
                name = k
                map_func = v['map']
                if v.has_key('reduce'):
                    reduce_func = v['reduce']
                else:
                    reduce_func = None
                view = ViewDefinition(
                    design, name, map_func, reduce_func, language)
                viewlist.append(view)

            return viewlist

        def get_directory(path):
            viewlist = []
            for f in os.listdir(path):
                current = os.path.join(path, f)
                viewlist = list(set.union(set(viewlist), set(get_file(current))))
            return viewlist

        if not self.is_connected:
            raise Exception('Error: Not connected to a database!')

        if os.path.isfile(f):
            viewlist = get_file(f)
        elif os.path.isdir(f):
            viewlist = get_directory(f)

        return ViewDefinition.sync_many(
            self.db, viewlist, remove_missing=remove_missing)
    def sync_designs(self, design_path, design_name=None):
        design_name = design_name or self.db_name
        logging.info("syncing design docs from %s/%s" %
                     (design_path, design_name))
        try:
            designs = load_design_doc(design_path + "/" + design_name,
                                      strip=True)
            view_defs = []
            logging.debug("Found designs: %s" % designs.keys())
            for name in designs:
                design = designs[name]
                views = design['views']
                for v_name in views:
                    view = views[v_name]
                    vd = ViewDefinition(name, v_name, view['map'],
                                        view.get('reduce'))
                    logging.debug("Found view def", vd)
                    view_defs.append(vd)
            return ViewDefinition.sync_many(self.db, view_defs)

        except OSError as ose:
            logging.error("Error syncing: %s" % ose)
        return None
 def sync_views(self):
     ViewDefinition.sync_many(self.server[self.db_name], docs)
Example #24
0
                del self[table]
            except:
                pass


__ACTIV = Activ(COUCHDB_URL)
EVALUATION = __ACTIV.evaluation

################################################
# CouchDB Permanent Views
################################################
#
# Retorna todas as avaliações de uma comunidade incluindo registry_id como parte da chave
#
# Retorno:
# todos os campos de EVALUATION
#
# Uso: model.BLOG.view('evaluation/all_data',startkey=["comunidade1"],endkey=["comunidade1", {}])
#
evaluation_all_data = ViewDefinition(
    "evaluation",
    "all_data",
    """function(doc) { 
                                     emit([doc._id.split("/")[0], doc._id], doc); 
                                   }
                                """,
)


ViewDefinition.sync_many(EVALUATION, [evaluation_all_data])
Example #25
0
 def _sync_views(self):
     ViewDefinition.sync_many(self.adb, views)
Example #26
0
# Retorna todos os documentos por url
#
# Retorno:
# todos os campos do documento
#
# Uso: database.BOOKMARKS.view('bookmarks/by_url',startkey=["http://www.nce.ufrj.br"],endkey=["http://www.nce.ufrj.br", {}])
#
bookmarks_by_url = ViewDefinition(
    "bookmarks",
    "by_url",
    """function(doc) { 
                                     emit([doc.url,doc.data_alt,doc._id], doc); 
                                   }
                                """,
)


ViewDefinition.sync_many(
    BOOKMARKS,
    [
        bookmarks_by_registry_id,
        bookmarks_count_by_registry_id,
        bookmarks_by_registry_id_and_tag,
        bookmarks_count_by_registry_id_and_tag,
        bookmarks_by_owner,
        bookmarks_by_url,
        bookmarks_by_registry_id_and_url,
        bookmarks_count_by_url,
    ],
)
Example #27
0
def sync_design(db):
    views = [j for i, j in globals().items() if "view" in i]
    ViewDefinition.sync_many(db, views)
Example #28
0
 def sync_views(self):
     ViewDefinition.sync_many(self.server[self.db_name], docs)
Example #29
0
def sync_couchdb_views(db):
    ViewDefinition.sync_many(db, __design_docs__)
Example #30
0
                                }
                            }  
                            
                           ''')

activity_pendent = ViewDefinition('activity', 'pendent', \
                          '''                          
                             function(doc) {
                               if (doc.type=="activity" && (!(doc.status == "finalizado"))) {
                                    for (e in doc.encarregados){
                                        emit([doc.encarregados[e], doc.registry_id, doc.data_cri,  1], doc);
                                    }
                                }
                                
                                if (doc.type=="activity" && (!(doc.status == "finalizado"))) {
                                    for (e in doc.encarregados){
                                        emit([doc.encarregados[e], doc.registry_id, doc.data_cri, 0], doc.group_id);
                                    }
                                }
                            }
                            ''',)

ViewDefinition.sync_many(core.database.ACTIVDB, [ 
                                                  activity_by_group, \
                                                  activity_Nfinalized, \
                                                  activity_pendent, \
                                                  activity_finalized_and_groups, \
                                                  activity_list_by_registry \
                                                ])

def sync_design(db):
    views = [j for i, j in globals().items() if "_view" in i]
    ViewDefinition.sync_many(db, views, callback=add_index_options)
Example #32
0
__ACTIV = Activ(COUCHDB_URL)
AGENDA = __ACTIV.agenda


# Retorna todas as entradas na agenda por registry_id e data/hora
#
# Retorno:
# todos os campos do documento
#
# Uso: database.AGENDA.view('agenda/by_date',startkey=[registry_id, ano, mes, dia, hora, min], endkey=[registry_id, ano, mes, dia, hora, min, {}], limit=3)
#
agenda_by_date = ViewDefinition('agenda','by_date', \
                               '''function(doc) { 
                                     for (var anomes in doc.events){
                                        ano = anomes.slice(0,4);
                                        mes = anomes.slice(4);
                                        for (var dia in doc.events[anomes]) {
                                           for (var event in doc.events[anomes][dia]) {
                                              hora = doc.events[anomes][dia][event]["hora"].slice(0,2);
                                              min = doc.events[anomes][dia][event]["hora"].slice(3);
                                              emit([doc._id, ano, mes, dia, hora, min], doc.events[anomes][dia][event]); 
                                           }
                                        }
                                     }
                                   }
                                ''')



ViewDefinition.sync_many(AGENDA, [agenda_by_date])
                                     
Example #33
0
 def __init__(self, db_url, name=None):
     url = "{}/{}".format(db_url, name)
     get_or_create(db_url, name)
     super(ContractsStorage, self).__init__(url=url)
     ViewDefinition.sync_many(self, [get_contracts_by_tender_id])
Example #34
0
def sync_design(db):
    views = [j for i, j in globals().items() if "view" in i]
    ViewDefinition.sync_many(db, views)
Example #35
0
def view_sync():
  View.sync_many(db,views,remove_missing=True)
Example #36
0
    def __init__(self, host_url, db_name):
        server = couchdb.Server(host_url)
        self.db = get_or_create_db(server, db_name)

        ViewDefinition.sync_many(self.db, [OCID, ID])
        LOGGER.info("Starting storage: {}".format(self.db.info()))
Example #37
0
#     participacao:
# }

communities_partial_data_of_all = ViewDefinition('communities','partial_data_of_all', \
                          u'''
                          function(doc) { 
                             if (doc.type=="community") 
                                 emit(doc._id, 
                                      {description: doc.description, owner: doc.owner, privacidade: doc.privacidade, participacao: doc.participacao}); 
                           }
                           ''')



ViewDefinition.sync_many(REGISTRY, [
                                    registry_exists, \
                                    registry_type \
                                    ])

ViewDefinition.sync_many(REGISTRY, [
                                    users_exists, \
                                    users_origin, \
                                    users_by_email, \
                                    users_by_cpf, \
                                    users_ismemberof, \

                                    users_isowner, \
                                    users_isfriend, \
                                    users_friends, \
                                    users_communities, \
                                    users_partial_data, \
                                    ])
Example #38
0
import couchdb
from couchdb.design import ViewDefinition

from peritus.couchdb.conf import settings

server = couchdb.Server(settings.URI)

for db in settings.AUTOCREATE_DATABASES:
    if not db in server:
        server.create(db)

for db, views in settings.AUTOCREATE_VIEWS.items():
    ViewDefinition.sync_many(db, views)
def create_couchdb_views(app, created_models, verbosity, **kwargs):
    ViewDefinition.sync_many(db, [item_type_date, by_date])
    ContentType.objects.get_or_create(name='CouchDB Item',
                                      app_label='couch_lifestream',
                                      model='couchdbitem')
Example #40
0
# Permite encontrar um comentário de uma página
#
# Retorno:
# Comentário
#
# Uso:
# database.FILES.view('files/comment',key=[doc_id, user, data_cri])

files_comment = ViewDefinition(
    "files",
    "comment",
    """
                          function(doc) { 
                            for (c in doc.comentarios)
                               emit([doc._id, doc.comentarios[c]['owner'], doc.comentarios[c]['data_cri']], doc.comentarios[c]['comment']);
                          }
                           """,
)

ViewDefinition.sync_many(
    FILES,
    [
        files_filename_exists,
        files_all_data,
        files_partial_data,
        files_folder_data,
        files_count_rootfiles,
        files_comment,
    ],
)
Example #41
0
                                       if (doc.type=="topic") {
                                           data = doc.ultimo_reply ? doc.ultimo_reply : doc.data_cri
                                           emit( [doc.registry_id, data], doc );
                                       }
                                    }
                               ''')

# Retorna lista de topicos do forum com uma tag, ordenadas por data do último comentário
#
# Esta view é baseada na objects/by_registry_id_and_tag, incluindo a data do último reply na chave,
# de forma a permitir a ordenação cronológica dos tópicos
# 
# Uso: database.ACTIVDB.view('forum/topic_list', startkey=["topic", registry_id],endkey=["topic", registry_id, {}])
#
forum_topic_list_by_tag = ViewDefinition('forum', 'topic_list_by_tag', \
                               '''function(doc) {
                                       if (doc.type=="topic") {
                                           for (tag in doc.tags){
                                               data = doc.ultimo_reply ? doc.ultimo_reply : doc.data_cri
                                               emit( [doc.registry_id, doc.tags[tag], data], doc );
                                            }
                                       }
                                    }
                               ''')

ViewDefinition.sync_many(core.database.ACTIVDB, [ 
                                                  forum_topic_list, \
                                                  forum_topic_list_by_tag \
                                                ])

def sync_design_chronograph(db):
    views = [start_date_chronograph]
    ViewDefinition.sync_many(db,
                             views,
                             remove_missing=True,
                             callback=add_index_options)
Example #43
0
                                for (var i = 0; i < diacritics.length; i++) {
                                    s = s.replace(diacritics[i][0], diacritics[i][1]);
                                }
                                return s;
                            }

                             emit([doc.registry_id,doc.tag.toLowerCase().removeDiacritics()],
                                      {"tag_id":doc._id,
                                       "texto":doc.texto,
                                       "objeto":doc.objeto,
                                       "registry_id":doc.registry_id,
                                       "tipo":doc.tipo,
                                       "data_cri":doc.data_cri});
                             if (doc.registry_id != doc.owner) {
                                  emit([doc.owner,doc.tag.toLowerCase().removeDiacritics()], 
                                          {"tag_id":doc._id,
                                           "texto":doc.texto,
                                           "objeto":doc.objeto,
                                           "registry_id":doc.registry_id,
                                           "tipo":doc.tipo,
                                           "data_cri":doc.data_cri}); 
                             }
                          }
                          ''')



ViewDefinition.sync_many(TAGS, [search_searchtags, \
                                search_tagcloud, \
                                search_usertag])
Example #44
0
                                     emit(doc.item_id, 1); 
                                   }
                              ''',
                              u'''
                              function(keys, values) {
                                 return sum(values);
                              }
                              ''')

# Retorna todos os documentos por item_id
#
# Retorno:
# todos os campos do documento
#
# Uso: database.GLOSSARY.view('glossary/by_item_id',startkey=["termo_x"],endkey=["termo_x", {}])
#
glossary_by_item_id = ViewDefinition('glossary','by_item_id', \
                               '''function(doc) { 
                                     emit([doc.item_id, doc.registry_id], doc); 
                                   }
                                ''')


ViewDefinition.sync_many(GLOSSARY, [glossary_by_registry_id, \
                                    glossary_by_registry_id_and_item_id, \
                                    glossary_by_registry_id_and_tag, \
                                    glossary_count_by_registry_id, \
                                    glossary_count_by_registry_id_and_tag, \
                                    glossary_count_by_item_id, \
                                    glossary_by_item_id])
Example #45
0
def sync_design(db):
    views = [j for i, j in globals().items() if "_view" in i]
    ViewDefinition.sync_many(db, views, callback=add_index_options)
Example #46
0
                                           }
                                       }
                                   }
                                """
    % (VERBOS_LEITURA, VERBOS_ESCRITA),
    """
                                function(keys, values) {
                                    return sum(values);
                                }
                                """,
)

ViewDefinition.sync_many(
    LOG,
    [
        total_by_day,
        total_by_user,
        log_by_object,
        log_list,
        log_list_news,
        log_stats_by_user,
        log_stats_by_all_users,
        log_stats_by_object,
        log_stats_by_all_objects,
        log_objects_by_all_types_bar_chart,
        log_users_by_all_types_bar_chart,
        log_users_by_types_bar_chart,
        log_objects_by_types_bar_chart,
    ],
)