[ckan-dev] Re: Re: Re: h.get_facet_items_dict and c.search_facets

Florian.Brucker at mb.karlsruhe.de Florian.Brucker at mb.karlsruhe.de
Fri Apr 8 05:54:04 UTC 2016


That was easier than I thought: I added a little template helper function
that performs a fake search (for 0 rows) using the same code that the
controllers use:


   import ckan.logic
   import ckan.model
   from ckan.common import g, c


   def fake_search():
       """
       Perform a fake dataset search.

       Some template helper functions (in particular
       `h.get_facet_items_dict`) only work if a search was performed
       before. Since this is not the case on all pages this function offers
       a way to perform a fake search for 0 rows.
       """
       context = {'model': ckan.model, 'session': ckan.model.Session,
                  'user': c.user, 'auth_user_obj': c.userobj}
       data_dict = {
           'q': '*:*',
           'facet.field': g.facets,
           'rows': 0,
           'start': 0,
           'fq': 'capacity:"public"'
       }
       query = ckan.logic.get_action('package_search')(context, data_dict)
       c.search_facets = query['search_facets']

       #  Return an empty string so now output is shown in the template
       return ''


After registering it via ITemplateHelpers I can just call it from my
template before calling get_facet_items_dict.


Regards,
Florian


"ckan-dev" <ckan-dev-bounces at lists.okfn.org> schrieb am 08.04.2016
07:30:54:

> Von: Florian.Brucker at mb.karlsruhe.de
> An: CKAN Development Discussions <ckan-dev at lists.okfn.org>,
> Datum: 08.04.2016 07:31
> Betreff: [ckan-dev] Antwort: Re: h.get_facet_items_dict and
c.search_facets
> Gesendet von: "ckan-dev" <ckan-dev-bounces at lists.okfn.org>
>
> Ian,
>
> I am not sure whether my use case is sufficient for any fundamental
> changes -- a tag cloud is just nice to have and I'm sure there are
> more important things to work on. I was hoping that I misunderstood
> something or that there was a quick workaround.
>
> Since you seem to have a bigger idea regarding this topic (i.e.
> removing c.search_facets) please file a ticket yourself (I don't
> know much about that part of CKAN).
>
>
> Regards,
> Florian
>
>
> "ckan-dev" <ckan-dev-bounces at lists.okfn.org> schrieb am 07.04.2016
18:30:26:
>
> > Von: Ian Ward <ian at excess.org>
> > An: CKAN Development Discussions <ckan-dev at lists.okfn.org>,
> > Datum: 07.04.2016 18:30
> > Betreff: Re: [ckan-dev] h.get_facet_items_dict and c.search_facets
> > Gesendet von: "ckan-dev" <ckan-dev-bounces at lists.okfn.org>
> >
> > We'll need to perform a search query to get the facet counts on pages
> > that weren't generated from a search query. How expensive is issuing a
> > query with rows=0 just to get the facet results? We could cache the
> > result so that each call to get_facet_items_dict won't trigger another
> > query.
> >
> > If possible, while we're at it I'd love to remove c.search_facets
> completely.
> >
> > Florian: I'd be happy to discuss this on a ticket in the ideas repo,
> > or if you're planning to implement this soon, on the ckan repo.
> >
> > On Thu, Apr 7, 2016 at 12:16 PM,  <Florian.Brucker at mb.karlsruhe.de>
wrote:
> > > Hi!
> > >
> > > I'm trying to add a tag cloud with the most popular tags to my CKAN
theme.
> > > To get the tags and their frequency I've found `h.get_facet_items`
and it
> > > seems to do the job nicely. However, it only works on some pages
(home,
> > > group, package search) while it doesn't on others (package/read and
> > > package/resource_read). The issue seems to be that
> `c.search_facets` is not
> > > defined on all pages, and  `h.get_facet_items` returns an empty list
if it
> > > isn't.
> > >
> > > Is that intended behavior? If so, how can I get
> `h.get_facet_items` to work
> > > on these pages?
> > >
> > >
> > > Regards,
> > > Florian
> > >
> > >
> > > _______________________________________________
> > > ckan-dev mailing list
> > > ckan-dev at lists.okfn.org
> > > https://lists.okfn.org/mailman/listinfo/ckan-dev
> > > Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
> > >
> > _______________________________________________
> > ckan-dev mailing list
> > ckan-dev at lists.okfn.org
> > https://lists.okfn.org/mailman/listinfo/ckan-dev
> > Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160408/080a418d/attachment-0003.html>


More information about the ckan-dev mailing list