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)
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)
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)