[GeoNode-users] Create new filter for layers

Simone Dalmasso simone.dalmasso at gmail.com
Sat Jun 20 13:38:37 PDT 2015


Ok then you have to track the http request from the browser and see whether
your api endpoint is correctly returning results as expected.

2015-06-20 22:32 GMT+02:00 Melvin Ramos <melvin.ramos1991 at gmail.com>:

> I'm guessing you are saying this:
>
>   module.load_categories = function ($http, $rootScope, $location){
>>         var params = typeof FILTER_TYPE == 'undefined' ? {} : {'type':
>> FILTER_TYPE};
>>         if ($location.search().hasOwnProperty('title__icontains')){
>>           params['title__icontains'] =
>> $location.search()['title__icontains'];
>>         }
>>         $http.get(CATEGORIES_ENDPOINT, {params:
>> params}).success(function(data){
>>
>> if($location.search().hasOwnProperty('category__identifier__in')){
>>                 data.objects =
>> module.set_initial_filters_from_query(data.objects,
>>                     $location.search()['category__identifier__in'],
>> 'identifier');
>>             }
>>             $rootScope.categories = data.objects;
>>             if (HAYSTACK_FACET_COUNTS && $rootScope.query_data) {
>>                 module.haystack_facets($http, $rootScope, $location);
>>             }
>>         });
>>         $http.get(IUCN_CATEGORIES_ENDPOINT, {params:
>> params}).success(function(data){
>>
>> if($location.search().hasOwnProperty('iucn_category__identifier__in')){
>>                 data.objects =
>> module.set_initial_filters_from_query(data.objects,
>>                     $location.search()['iucn_category__identifier__in'],
>> 'identifier');
>>             }
>>             $rootScope.iucn_categories = data.objects;
>>             if (HAYSTACK_FACET_COUNTS && $rootScope.query_data) {
>>                 module.haystack_facets($http, $rootScope, $location);
>>             }
>>         });
>>     }
>
>
> where I should also write this:
>
>     if ($('#categories').length > 0 && $('#iucn_categories').length > 0){
>>        module.load_categories($http, $rootScope, $location);
>>     }
>
>
> But it doesn't work either way. I was sort of debugging this and I found
> out that the rootScope Object is showing that iucn_categories is an
> Array[0] against categories (Array[19]). I guess that says that the query
> is not getting anything or something but I have no idea where I can check
> this
>
>
>
>
>
>
> Enviado con MailTrack
> <https://mailtrack.io/install?source=signature&lang=es&referral=melvin.ramos1991@gmail.com&idSignature=23>
>
> 2015-06-19 2:29 GMT-04:30 Simone Dalmasso <simone.dalmasso at gmail.com>:
>
>> One issue is in the first part of the js. Module.load-ounce-categories.
>> Despite the name the original function is used to load also the keywords
>> etc. so instead or rewrite the entire function just add the chunk $http.get
>> for your categories into the original module.load-categories.
>>
>> That should help. :)
>>
>>
>> Il venerdì 19 giugno 2015, Melvin Ramos <melvin.ramos1991 at gmail.com> ha
>> scritto:
>>
>>> Hello Simone,
>>>
>>> "You did well young padawan" (:
>>>
>>> Thanks. I just checked it out. In theory I replicated everything but
>>> still not working. I will go step by step like before:
>>>
>>> *Modified search_scripts.html in the search templates*
>>>
>>> I added this line of code:
>>>
>>> *IUCN_CATEGORIES_ENDPOINT = '{% url 'api_dispatch_list' api_name='api'
>>>> resource_name='iucn_categories' %}';*
>>>
>>>
>>> Just like the categories endpoint
>>>
>>> *Modified the search.js in the static folder*
>>>
>>> I added in the loaded section the following
>>>
>>>
>>>> *  module.load_iucn_categories = function ($http, $rootScope,
>>>> $location){*
>>>> *        var params = typeof FILTER_TYPE == 'undefined' ? {} : {'type':
>>>> FILTER_TYPE};*
>>>> *        if ($location.search().hasOwnProperty('title__icontains')){*
>>>> *          params['title__icontains'] =
>>>> $location.search()['title__icontains'];*
>>>> *        }*
>>>> *        $http.get(IUCN_CATEGORIES_ENDPOINT, {params:
>>>> params}).success(function(data){*
>>>> *
>>>> if($location.search().hasOwnProperty('iucn_category__identifier__in')){*
>>>> *                data.objects =
>>>> module.set_initial_filters_from_query(data.objects,*
>>>> *
>>>> $location.search()['iucn_category__identifier__in'], 'identifier');*
>>>> *            }*
>>>> *            $rootScope.categories = data.objects;*
>>>> *            if (HAYSTACK_FACET_COUNTS && $rootScope.query_data) {*
>>>> *                module.haystack_facets($http, $rootScope, $location);*
>>>> *            }*
>>>> *        });**    }*
>>>
>>>
>>> Then I added in the module.haystack_facets the following
>>>
>>>
>>>> *      if ("iucn_categories" in $rootScope) {*
>>>> *          $rootScope.category_counts = data.meta.facets.category;*
>>>> *          for (var id in $rootScope.categories) {*
>>>> *              var category = $rootScope.categories[id];*
>>>> *              if (category.identifier in $rootScope.category_counts) {*
>>>> *                  category.count =
>>>> $rootScope.category_counts[category.identifier]*
>>>> *              } else {*
>>>> *                  category.count = 0;*
>>>> *              }*
>>>> *          }**      }*
>>>
>>>
>>> Finally, in the module.run I added this
>>>
>>>
>>>> *    if ($('#iucn_categories').length > 0){*
>>>> *       module.load_categories($http, $rootScope, $location);**    }*
>>>
>>>
>>> But it didn't work, still shows nothing on the template.What am I
>>> missing??
>>>
>>> Thanks in advance,
>>>
>>> Melvin
>>>
>>>
>>>
>>>
>>> Enviado con MailTrack
>>> <https://mailtrack.io/install?source=signature&lang=es&referral=melvin.ramos1991@gmail.com&idSignature=23>
>>>
>>> 2015-06-18 17:01 GMT-04:30 Simone Dalmasso <simone.dalmasso at gmail.com>:
>>>
>>>> Hi Melvin,
>>>> you did well, the only missing thing is here
>>>> https://github.com/GeoNode/geonode/blob/master/geonode/static/geonode/js/search/search.js,
>>>> javascript part that loads the categories through the api and compiles the
>>>> query.
>>>>
>>>> Ciao
>>>>
>>>> 2015-06-18 23:17 GMT+02:00 Melvin Ramos <melvin.ramos1991 at gmail.com>:
>>>>
>>>>> Hello,
>>>>>
>>>>> I'm trying to duplicate the Categories filter, because I'm need to use
>>>>> a new set of categories for my layers, but I'm having a hard time trying to
>>>>> find out how the templates gets the data since it uses AngularJS.
>>>>>
>>>>> So here is what I''ve done so far:
>>>>>
>>>>> *Modified the models.py of the Base app*
>>>>> I added a new class called IUCNCategory
>>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> *class IUCNCategory(models.Model):    """    Metadata regarding IUCN
>>>>>> standards    """    identifier = models.CharField(max_length=255)
>>>>>> category_type = models.CharField(        choices=(
>>>>>> ('risk_criteria', 'Risk Criteria'),            ('bio_realm',
>>>>>> 'Biogeographical Realm'),            ('typology', 'Typology')        ),
>>>>>>     max_length=255,    )    description = models.TextField(default='')
>>>>>> gn_description = models.TextField('RLE description', default='',
>>>>>> null=True)    # is_choice = models.BooleanField(default=True)    def
>>>>>> __unicode__(self):        return u"{0}".format(self.gn_description)
>>>>>> class Meta:        ordering = ("category_type","identifier",)
>>>>>> verbose_name_plural = 'IUCN Categories'*
>>>>>
>>>>>
>>>>> It's just like TopicCategory with one different attribute.
>>>>>
>>>>> I added to the class ResourceBase the following attribute
>>>>>
>>>>> *iucn_category = models.ForeignKey(IUCNCategory, null=True,
>>>>>> blank=True)*
>>>>>
>>>>>
>>>>>
>>>>> *Modified the api.py **of the Api app*
>>>>> I created a new class called IUCNCategoryResource
>>>>>
>>>>>
>>>>>> *class IUCNCategoryResource(TypeFilteredResource):**    """IUCN
>>>>>> Category api"""*
>>>>>>
>>>>>> *    def serialize(self, request, data, format, options={}):**
>>>>>>   options['count_type'] = 'iucn_category'*
>>>>>> *        return super(IUCNCategoryResource, self).serialize(request,
>>>>>> data, format, options)*
>>>>>>
>>>>>> *    class Meta:*
>>>>>> *        queryset = IUCNCategory.objects.all()*
>>>>>> *        resource_name = 'iucn_categories'*
>>>>>> *        allowed_methods = ['get']*
>>>>>> *        filtering = {*
>>>>>> *            'identifier': ALL,*
>>>>>> *        }**        serializer = CountJSONSerializer()*
>>>>>
>>>>>
>>>>> It's like the class TopicCategoryResource
>>>>>
>>>>> *Modified the* *resourcebase_api.py **of the Api app*
>>>>>
>>>>> I added to the class CommonModelApi the following attribute
>>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> *iucn_category = fields.ToOneField(        IUCNCategoryResource,
>>>>>>   'iucn_category',        null=True,        full=True)*
>>>>>
>>>>>
>>>>> In the definition on the Haystack filters I added the following in the
>>>>> proper places:
>>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> *...iucn_category =
>>>>>> parameters.getlist("iucn_category__identifier__in")...if iucn_category:
>>>>>>         sqs = (SearchQuerySet() if sqs is None else sqs).narrow(
>>>>>>     'iucn_category:%s' % ','.join(map(str, iucn_category)))...*
>>>>>
>>>>>
>>>>> Just like the category field.
>>>>>
>>>>> *Modified the** urls**.py **of the Api app*
>>>>>
>>>>> I imported the IUCNCategoryResource from the apy.py and wrote
>>>>>
>>>>> *api.register(IUCNCategoryResource())*
>>>>>
>>>>>
>>>>> *Modified the template _general_filters.html inside the search
>>>>> templates*
>>>>>
>>>>> I added the following code
>>>>>
>>>>>
>>>>>> *<nav class="filter">*
>>>>>> *  <h4><a href="#" class="toggle toggle-nav"><i class="fa
>>>>>> fa-chevron-right"></i>{% trans "Risk Criteria" %}</a></h4>*
>>>>>> *  <ul class="nav closed" id="iucn_categories">*
>>>>>> *    {% verbatim %}*
>>>>>> *      <li ng-repeat="iucn_category in iucn_categories"
>>>>>> ng-if="iucn_category.count > 0">*
>>>>>> *        <a data-value="{{ iucn_category.identifier }}"
>>>>>> data-filter="iucn_category__identifier__in" *
>>>>>> *         ng-click="multiple_choice_listener($event)"
>>>>>> class="{{iucn_category.active}}">{{ iucn_category.gn_description | limitTo:
>>>>>> 25 }}{{ iucn_category.gn_description.length > 25 ? '...' : ''}}*
>>>>>> *          <span class="badge pull-right">{{ iucn_category.count
>>>>>> }}</span>*
>>>>>> *        </a>*
>>>>>> *      </li>*
>>>>>> *    {% endverbatim %}*
>>>>>> *  </ul>**</nav>*
>>>>>
>>>>>
>>>>> But nothing shows beneath the filter name in the template, even when I
>>>>> have one IUCN category stored in database. I'm guessing there is a view
>>>>> somewhere or I'm missing something that allows to pass the data and I can't
>>>>> find it or just Angular is too mystic for me.
>>>>>
>>>>> Can anybody help me?
>>>>>
>>>>> Thanks in advance,
>>>>>
>>>>> Melvin
>>>>>
>>>>>
>>>>> --
>>>>> Melvin David Ramos Macías
>>>>> 07-41408
>>>>> Ingeniería de Computación
>>>>> Universidad Simón Bolívar
>>>>>
>>>>>
>>>>>
>>>>> Enviado con MailTrack
>>>>> <https://mailtrack.io/install?source=signature&lang=es&referral=melvin.ramos1991@gmail.com&idSignature=23>
>>>>>
>>>>> _______________________________________________
>>>>> geonode-users mailing list
>>>>> geonode-users at lists.osgeo.org
>>>>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/geonode-users
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Simone
>>>>
>>>
>>>
>>>
>>> --
>>> Melvin David Ramos Macías
>>> 07-41408
>>> Ingeniería de Computación
>>> Universidad Simón Bolívar
>>>
>>
>>
>> --
>> Simone
>>
>
>
>
> --
> Melvin David Ramos Macías
> 07-41408
> Ingeniería de Computación
> Universidad Simón Bolívar
>



-- 
Simone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/geonode-users/attachments/20150620/ddce212c/attachment-0001.html>


More information about the geonode-users mailing list