Exemple #1
0
    def generate_from_views(cls, views):

        # id -> instance
        instances = dict()

        def get_instance(name):
            existing = instances.get(name, None)
            if not existing:
                doc_id = u"_design/%s" % (name, )
                existing = cls(doc_id=doc_id)
                instances[name] = existing
            return existing

        for view in views:
            view = IViewFactory(view)
            instance = get_instance(view.design_doc_id)
            entry = dict()
            if hasattr(view, 'map'):
                entry['map'] = view.get_code('map')
                if view.use_reduce:
                    entry['reduce'] = view.get_code('reduce')
                instance.views[view.name] = entry

            if hasattr(view, 'filter'):
                instance.filters[view.name] = unicode(view.filter.source)
        return instances.values()
Exemple #2
0
    def generate_from_views(cls, views):

        # id -> instance
        instances = dict()

        def get_instance(view):
            name = view.design_doc_id
            if name is None:
                raise ValueError("%r.design_doc_id is None" % (view,))
            existing = instances.get(name, None)
            if not existing:
                doc_id = u"_design/%s" % (name,)
                existing = cls(doc_id=doc_id, language=view.language)
                instances[name] = existing
            elif existing.language != view.language:
                raise ValueError(
                    "Language mismatch! Design document %s "
                    "has language: %s, the view %s has language: "
                    " %s" % (doc_id, existing.language, name, view.language)
                )
            return existing

        for view in views:
            view = IViewFactory(view)
            if view.name is None:
                raise ValueError("%r.name is None!" % (view,))
            instance = get_instance(view)
            entry = dict()
            if hasattr(view, "map"):
                entry["map"] = view.get_code("map")
                if view.use_reduce:
                    entry["reduce"] = view.get_code("reduce")
                instance.views[view.name] = entry

            if hasattr(view, "filter"):
                instance.filters[view.name] = view.get_code("filter")

            if view.disable_reduce_limit:
                instance.disable_reduce_limit = True

        return instances.values()