Exemplo n.º 1
0
def add_search_to_context(context):
    stash = {}
    context['stash'] = stash
    params = SearchParams(stash)
    if params.search_in_progress:
        search = Search(params)
        search.add_to_context(context)
Exemplo n.º 2
0
 def write_search(self, params):
     params = dict(params)
     stash = {}
     context = dict(stash=stash)
     searchparams = SearchParams(stash, params)
     search = Search(searchparams, url_fn=self.url)
     if self.collid is not None:
         search.restrict_to_collection(self.collid)
     search.add_to_context(context)
     context['showfull'] = int(getparam('showfull', '1', stash, params))
     self.render('search', (), params,
                 "export/html/search.html", context)
Exemplo n.º 3
0
    def _get_export_records(self, stash, record_id, issearch, coll, incsubs):
        """Returns a search queryset, and a description of the export."""

        # record_id is set to export a single record.
        if record_id is not None:
            search = SearchCollection.doc_type('record').field.id == record_id
            return search, "record with id %s" % record_id, None

        # if issearch is set, export the search results.
        if issearch is not None:
            params = SearchParams(stash)
            search_obj = Search(params)
            if not search_obj.validate():
                redirect(url("search", **stash))
            desc = "%d records found by search.\n" % search_obj.match_count
            return search_obj.query, desc + search_obj.query_desc, None

        # FIXME - allow selected records here.

        # If we've got no collection set, but have a lock, set the collection
        # to the locked one.
        lockedto = get_lockto_coll()
        if lockedto is not None and coll is None:
            coll = lockedto.id

        # coll is set to export a whole collection.
        if coll is not None:
            coll_obj = get_or_404(Collection, coll)
            search = coll_obj.items_from_search(incsubs=incsubs)

            desc = "all %d records from collection %s" % (
                len(search),
                coll_obj.title
            )
            if incsubs:
                desc += " (including subcollections)"
            return search, desc, coll_obj

        # If none of the above are set, export everything.
        return SearchCollection.doc_type('record').all()[:], "all records", None
Exemplo n.º 4
0
    def search(self, **params):
        stash = {}
        context = dict(stash=stash)
        params = SearchParams(stash)
        search = Search(params)
        if not search.validate():
            # If the search doesn't validate, always go to the search entry
            # page.
            params.action = 'entry'
        search.add_to_context(context)
        context['showfull'] = int(getparam('showfull', '0', stash))

        if params.action == 'search':
            return render("search.html", context)
        elif params.action == 'select':
            return render("search_select.html", context)

        elif params.action.startswith('createcoll'):
            if cherrypy.request.method != "POST":
                return render("search_createcoll.html", context)
            subact = params.action[11:]

            parents = []
            for num in getorderparam('parent_order'):
                parent = getparam('parent%d' % num)
                if parent is None or parent == '':
                    continue
                if subact == ('del_parent_%d' % num):
                    continue
                parents.append(unicode(parent))
            if subact == 'add_parent':
                parents.append(u'')
            context['parents'] = tuple(enumerate(parents))
            context['allowable_parents'] = set(
                c.id for c in Collection.objects
            )
            context['collections'] = Collection.objects

            if subact == 'do':
                newtitle = getparam('create_colltitle', '')
                if len(newtitle) == 0:
                    context['error'] = "Cannot create collection with no title"
                    return render("search_createcoll.html", context)

                coll = Collection.find_by_title(newtitle)
                if len(coll) != 0:
                    context['error'] = "Collection with title %s already exists" % newtitle
                    return render("search_createcoll.html", context)

                coll = Collection(None, None, newtitle)
                Collection.objects.set(coll)
                # Have to set the collection before setting the parents, to get
                # an ID for the parents to refer back to.
                coll.set_parents(filter(lambda x: x != '', parents))
                Collection.objects.set(coll)
                Record.objects.flush()
                Collection.objects.flush()

                for record in search.query:
                    record = record.object
                    record.collections = record.collections + [coll.id]
                    Record.objects.set(record)
                Record.objects.flush()
                Collection.objects.flush()

                redirect(url("coll-view", id=coll.id))
            else:
                return render("search_createcoll.html", context)

        elif params.action == 'addtocoll':
            context['all_collections'] = Collection.objects
            if cherrypy.request.method != "POST":
                return render("search_addtocoll.html", context)

            newid = getparam('addto_collid', '')
            if len(newid) == 0:
                context['error'] = "Pick a collection to add to"
                return render("search_addtocoll.html", context)

            coll = Collection.objects.get(newid)
            for record in search.query:
                record = record.object
                record.collections = record.collections + [coll.id]
                Record.objects.set(record)
            Record.objects.flush()
            Collection.objects.flush()

            redirect(url("coll-view", id=coll.id))

            return render("search_addtocoll.html", context)

        elif params.action == 'removefromcoll':
            context['all_collections'] = Collection.objects
            if cherrypy.request.method != "POST":
                return render("search_removefromcoll.html", context)

            newid = getparam('removefrom_collid', '')
            if len(newid) == 0:
                context['error'] = "Pick a collection to remove from"
                return render("search_removefromcoll.html", context)

            coll = Collection.objects.get(newid)
            for record in search.query:
                record = record.object
                record.collections = tuple(filter(lambda x: x != coll.id,
                                                  record.collections))
                Record.objects.set(record)
            Record.objects.flush()
            Collection.objects.flush()

            redirect(url("coll-view", id=coll.id))

            return render("search_removefromcoll.html", context)

        else:
            context['all_collections'] = Collection.objects
            return render("search_entry.html", context)