def h_keywords(request): from geonode.base.models import HierarchicalKeyword as hk p_type = request.GET.get('type', None) resource_name = request.GET.get('resource_name', None) keywords = hk.resource_keywords_tree(request.user, resource_type=p_type, resource_name=resource_name) subtypes = [] if p_type == 'geoapp': for label, app in apps.app_configs.items(): if hasattr(app, 'type') and app.type == 'GEONODE_APP': if hasattr(app, 'default_model'): _model = apps.get_model(label, app.default_model) if issubclass(_model, GeoApp): subtypes.append(_model.__name__.lower()) for _type in subtypes: _bulk_tree = hk.resource_keywords_tree(request.user, resource_type=_type, resource_name=resource_name) if isinstance(_bulk_tree, list): for _elem in _bulk_tree: keywords.append(_elem) else: keywords.append(_bulk_tree) return HttpResponse(content=json.dumps(keywords))
def h_keywords(request): from geonode.base.models import HierarchicalKeyword as hk #print("Função h_keywords do views do Geonode: ") keywords = json.dumps(hk.dump_bulk_tree()) #print(keywords) return HttpResponse(content=keywords)
def filter_h_keywords(self, queryset, keywords): filtered = queryset treeqs = HierarchicalKeyword.objects.none() for keyword in keywords: try: kw = HierarchicalKeyword.objects.get(name=keyword) treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except ObjectDoesNotExist: # Ignore keywords not actually used? pass filtered = queryset.filter(Q(keywords__in=treeqs)) return filtered
def filter_h_keywords(self, queryset, keywords): treeqs = HierarchicalKeyword.objects.none() if keywords and len(keywords) > 0: for keyword in keywords: try: kws = HierarchicalKeyword.objects.filter( Q(name__iexact=keyword) | Q(slug__iexact=keyword)) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except ObjectDoesNotExist: # Ignore keywords not actually used? pass filtered = queryset.filter(Q(keywords__in=treeqs)) else: filtered = queryset return filtered
def facets(context): request = context['request'] title_filter = request.GET.get('title__icontains', '') extent_filter = request.GET.get('extent', None) keywords_filter = request.GET.getlist('keywords__slug__in', None) category_filter = request.GET.getlist('category__identifier__in', None) regions_filter = request.GET.getlist('regions__name__in', None) owner_filter = request.GET.getlist('owner__username__in', None) date_gte_filter = request.GET.get('date__gte', None) date_lte_filter = request.GET.get('date__lte', None) date_range_filter = request.GET.get('date__range', None) facet_type = context.get('facet_type', 'all') if not settings.SKIP_PERMS_FILTER: authorized = [] try: authorized = get_objects_for_user( request.user, 'base.view_resourcebase').values('id') except Exception: pass if facet_type == 'geoapps': facets = {} from django.apps import apps for label, app in apps.app_configs.items(): if hasattr(app, 'type') and app.type == 'GEONODE_APP': if hasattr(app, 'default_model'): geoapps = get_visible_resources( apps.get_model(label, app.default_model).objects.all(), request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if category_filter: geoapps = geoapps.filter(category__identifier__in=category_filter) if regions_filter: geoapps = geoapps.filter(regions__name__in=regions_filter) if owner_filter: geoapps = geoapps.filter(owner__username__in=owner_filter) if date_gte_filter: geoapps = geoapps.filter(date__gte=date_gte_filter) if date_lte_filter: geoapps = geoapps.filter(date__lte=date_lte_filter) if date_range_filter: geoapps = geoapps.filter(date__range=date_range_filter.split(',')) if extent_filter: geoapps = filter_bbox(geoapps, extent_filter) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass geoapps = geoapps.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: geoapps = geoapps.filter(id__in=authorized) facets[app.default_model] = geoapps.count() return facets elif facet_type == 'documents': documents = Document.objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter(category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter(date__range=date_range_filter.split(',')) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: documents = documents.filter(id__in=authorized) counts = documents.values('doc_type').annotate(count=Count('doc_type')) facets = {count['doc_type']: count['count'] for count in counts} return facets else: layers = Layer.objects.filter(title__icontains=title_filter) if category_filter: layers = layers.filter(category__identifier__in=category_filter) if regions_filter: layers = layers.filter(regions__name__in=regions_filter) if owner_filter: layers = layers.filter(owner__username__in=owner_filter) if date_gte_filter: layers = layers.filter(date__gte=date_gte_filter) if date_lte_filter: layers = layers.filter(date__lte=date_lte_filter) if date_range_filter: layers = layers.filter(date__range=date_range_filter.split(',')) layers = get_visible_resources( layers, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: layers = filter_bbox(layers, extent_filter) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass layers = layers.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: layers = layers.filter(id__in=authorized) counts = layers.values('storeType').annotate(count=Count('storeType')) counts_array = [] try: for count in counts: counts_array.append((count['storeType'], count['count'])) except Exception: pass count_dict = dict(counts_array) vector_time_series = layers.exclude(has_time=False).filter(storeType='dataStore'). \ values('storeType').annotate(count=Count('storeType')) if vector_time_series: count_dict['vectorTimeSeries'] = vector_time_series[0]['count'] facets = { 'raster': count_dict.get('coverageStore', 0), 'vector': count_dict.get('dataStore', 0), 'vector_time': count_dict.get('vectorTimeSeries', 0), 'remote': count_dict.get('remoteStore', 0), 'wms': count_dict.get('wmsStore', 0), } # Break early if only_layers is set. if facet_type == 'layers': return facets maps = Map.objects.filter(title__icontains=title_filter) documents = Document.objects.filter(title__icontains=title_filter) if category_filter: maps = maps.filter(category__identifier__in=category_filter) documents = documents.filter(category__identifier__in=category_filter) if regions_filter: maps = maps.filter(regions__name__in=regions_filter) documents = documents.filter(regions__name__in=regions_filter) if owner_filter: maps = maps.filter(owner__username__in=owner_filter) documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: maps = maps.filter(date__gte=date_gte_filter) documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: maps = maps.filter(date__lte=date_lte_filter) documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: maps = maps.filter(date__range=date_range_filter.split(',')) documents = documents.filter(date__range=date_range_filter.split(',')) maps = get_visible_resources( maps, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: documents = filter_bbox(documents, extent_filter) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass maps = maps.filter(Q(keywords__in=treeqs)) documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: maps = maps.filter(id__in=authorized) documents = documents.filter(id__in=authorized) facets['map'] = maps.count() facets['document'] = documents.count() if facet_type == 'home': facets['user'] = get_user_model().objects.exclude( username='******').count() facets['group'] = GroupProfile.objects.exclude( access="private").count() facets['layer'] = facets['raster'] + facets['vector'] + facets['remote'] + facets['wms'] return facets
def facets(context): request = context['request'] title_filter = request.GET.get('title__icontains', '') extent_filter = request.GET.get('extent', None) keywords_filter = request.GET.getlist('keywords__slug__in', None) category_filter = request.GET.getlist('category__identifier__in', None) regions_filter = request.GET.getlist('regions__name__in', None) owner_filter = request.GET.getlist('owner__username__in', None) date_gte_filter = request.GET.get('date__gte', None) date_lte_filter = request.GET.get('date__lte', None) date_range_filter = request.GET.get('date__range', None) facet_type = context['facet_type'] if 'facet_type' in context else 'all' if not settings.SKIP_PERMS_FILTER: authorized = [] try: authorized = get_objects_for_user( request.user, 'base.view_resourcebase').values('id') except Exception: pass if facet_type == 'documents': documents = Document.objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter(category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter(date__range=date_range_filter.split(',')) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: documents = documents.filter(id__in=authorized) counts = documents.values('doc_type').annotate(count=Count('doc_type')) facets = dict([(count['doc_type'], count['count']) for count in counts]) return facets else: layers = Layer.objects.filter(title__icontains=title_filter) if category_filter: layers = layers.filter(category__identifier__in=category_filter) if regions_filter: layers = layers.filter(regions__name__in=regions_filter) if owner_filter: layers = layers.filter(owner__username__in=owner_filter) if date_gte_filter: layers = layers.filter(date__gte=date_gte_filter) if date_lte_filter: layers = layers.filter(date__lte=date_lte_filter) if date_range_filter: layers = layers.filter(date__range=date_range_filter.split(',')) layers = get_visible_resources( layers, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: from geonode.utils import bbox_to_projection bbox = extent_filter.split(',') bbox = list(map(str, bbox)) intersects = (Q(bbox_x0__gt=bbox[0]) & Q(bbox_x1__lt=bbox[2]) & Q(bbox_y0__gt=bbox[1]) & Q(bbox_y1__lt=bbox[3])) for proj in Layer.objects.order_by('srid').values('srid').distinct(): if proj['srid'] != 'EPSG:4326': proj_bbox = bbox_to_projection(bbox + ['4326', ], target_srid=int(proj['srid'][5:])) if proj_bbox[-1] != 4326: intersects = intersects | (Q(bbox_x0__gt=proj_bbox[0]) & Q(bbox_x1__lt=proj_bbox[2]) & Q( bbox_y0__gt=proj_bbox[1]) & Q(bbox_y1__lt=proj_bbox[3])) layers = layers.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass layers = layers.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: layers = layers.filter(id__in=authorized) counts = layers.values('storeType').annotate(count=Count('storeType')) counts_array = [] try: for count in counts: counts_array.append((count['storeType'], count['count'])) except Exception: pass count_dict = dict(counts_array) vector_time_series = layers.exclude(has_time=False).filter(storeType='dataStore'). \ values('storeType').annotate(count=Count('storeType')) if vector_time_series: count_dict['vectorTimeSeries'] = vector_time_series[0]['count'] facets = { 'raster': count_dict.get('coverageStore', 0), 'vector': count_dict.get('dataStore', 0), 'vector_time': count_dict.get('vectorTimeSeries', 0), 'remote': count_dict.get('remoteStore', 0), 'wms': count_dict.get('wmsStore', 0), } # Break early if only_layers is set. if facet_type == 'layers': return facets maps = Map.objects.filter(title__icontains=title_filter) documents = Document.objects.filter(title__icontains=title_filter) if category_filter: maps = maps.filter(category__identifier__in=category_filter) documents = documents.filter(category__identifier__in=category_filter) if regions_filter: maps = maps.filter(regions__name__in=regions_filter) documents = documents.filter(regions__name__in=regions_filter) if owner_filter: maps = maps.filter(owner__username__in=owner_filter) documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: maps = maps.filter(date__gte=date_gte_filter) documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: maps = maps.filter(date__lte=date_lte_filter) documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: maps = maps.filter(date__range=date_range_filter.split(',')) documents = documents.filter(date__range=date_range_filter.split(',')) maps = get_visible_resources( maps, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) maps = maps.filter(intersects) documents = documents.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except Exception: # Ignore keywords not actually used? pass maps = maps.filter(Q(keywords__in=treeqs)) documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: maps = maps.filter(id__in=authorized) documents = documents.filter(id__in=authorized) facets['map'] = maps.count() facets['document'] = documents.count() if facet_type == 'home': facets['user'] = get_user_model().objects.exclude( username='******').count() facets['group'] = GroupProfile.objects.exclude( access="private").count() facets['layer'] = facets['raster'] + facets['vector'] + facets['remote'] + facets['wms'] return facets
def facets(context): request = context['request'] is_admin = False is_staff = False is_manager = False if request.user: is_admin = request.user.is_superuser if request.user else False is_staff = request.user.is_staff if request.user else False try: is_manager = request.user.groupmember_set.all().filter( role='manager').exists() except: is_manager = False title_filter = request.GET.get('title__icontains', '') extent_filter = request.GET.get('extent', None) keywords_filter = request.GET.getlist('keywords__slug__in', None) category_filter = request.GET.getlist('category__identifier__in', None) regions_filter = request.GET.getlist('regions__name__in', None) owner_filter = request.GET.getlist('owner__username__in', None) date_gte_filter = request.GET.get('date__gte', None) date_lte_filter = request.GET.get('date__lte', None) date_range_filter = request.GET.get('date__range', None) facet_type = context['facet_type'] if 'facet_type' in context else 'all' if not settings.SKIP_PERMS_FILTER: authorized = get_objects_for_user( request.user, 'base.view_resourcebase').values('id') if facet_type == 'documents': documents = Document.objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter( category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter( date__range=date_range_filter.split(',')) if settings.ADMIN_MODERATE_UPLOADS: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(group__in=group_list_all) | Q(owner__username__iexact=str(request.user))) except: documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: documents = documents.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.RESOURCE_PUBLISHING: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(group__in=group_list_all) | Q(owner__username__iexact=str(request.user))) except: documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: documents = documents.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.GROUP_PRIVATE_RESOURCES: public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') except: anonymous_group = None if is_admin: pass elif request.user: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass if anonymous_group: documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) else: documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: if anonymous_group: documents = documents.filter( Q(group__isnull=True) | Q(group__in=public_groups) | Q(group=anonymous_group)) else: documents = documents.filter( Q(group__isnull=True) | Q(group__in=public_groups)) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter( name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: documents = documents.filter(id__in=authorized) counts = documents.values('doc_type').annotate(count=Count('doc_type')) facets = dict([(count['doc_type'], count['count']) for count in counts]) return facets else: layers = Layer.objects.filter(title__icontains=title_filter) if category_filter: layers = layers.filter(category__identifier__in=category_filter) if regions_filter: layers = layers.filter(regions__name__in=regions_filter) if owner_filter: layers = layers.filter(owner__username__in=owner_filter) if date_gte_filter: layers = layers.filter(date__gte=date_gte_filter) if date_lte_filter: layers = layers.filter(date__lte=date_lte_filter) if date_range_filter: layers = layers.filter(date__range=date_range_filter.split(',')) if settings.ADMIN_MODERATE_UPLOADS: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: layers = layers.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.RESOURCE_PUBLISHING: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: layers = layers.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.GROUP_PRIVATE_RESOURCES: public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') except: anonymous_group = None if is_admin: pass elif request.user: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass if anonymous_group: layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) else: layers = layers.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: if anonymous_group: layers = layers.filter( Q(group__isnull=True) | Q(group__in=public_groups) | Q(group=anonymous_group)) else: layers = layers.filter( Q(group__isnull=True) | Q(group__in=public_groups)) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) layers = layers.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter( name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except: # Ignore keywords not actually used? pass layers = layers.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: layers = layers.filter(id__in=authorized) counts = layers.values('storeType').annotate(count=Count('storeType')) count_dict = dict([(count['storeType'], count['count']) for count in counts]) facets = { 'raster': count_dict.get('coverageStore', 0), 'vector': count_dict.get('dataStore', 0), 'remote': count_dict.get('remoteStore', 0), 'wms': count_dict.get('wmsStore', 0), } # Break early if only_layers is set. if facet_type == 'layers': return facets maps = Map.objects.filter(title__icontains=title_filter) documents = Document.objects.filter(title__icontains=title_filter) if category_filter: maps = maps.filter(category__identifier__in=category_filter) documents = documents.filter( category__identifier__in=category_filter) if regions_filter: maps = maps.filter(regions__name__in=regions_filter) documents = documents.filter(regions__name__in=regions_filter) if owner_filter: maps = maps.filter(owner__username__in=owner_filter) documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: maps = maps.filter(date__gte=date_gte_filter) documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: maps = maps.filter(date__lte=date_lte_filter) documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: maps = maps.filter(date__range=date_range_filter.split(',')) documents = documents.filter( date__range=date_range_filter.split(',')) if settings.ADMIN_MODERATE_UPLOADS: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') maps = maps.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: maps = maps.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: maps = maps.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.RESOURCE_PUBLISHING: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') maps = maps.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: maps = maps.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: maps = maps.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.GROUP_PRIVATE_RESOURCES: public_groups = GroupProfile.objects.exclude( access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') except: anonymous_group = None if is_admin: pass elif request.user: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values( 'group') except: pass if anonymous_group: maps = maps.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) else: maps = maps.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(owner__username__iexact=str(request.user))) else: if anonymous_group: maps = maps.filter( Q(group__isnull=True) | Q(group=anonymous_group)) documents = documents.filter( Q(group__isnull=True) | Q(group=anonymous_group)) else: maps = maps.filter(Q(group__isnull=True)) documents = documents.filter(Q(group__isnull=True)) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) maps = maps.filter(intersects) documents = documents.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter( name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except: # Ignore keywords not actually used? pass maps = maps.filter(Q(keywords__in=treeqs)) documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: maps = maps.filter(id__in=authorized) documents = documents.filter(id__in=authorized) facets['map'] = maps.count() facets['document'] = documents.count() if facet_type == 'home': facets['user'] = get_user_model().objects.exclude( username='******').count() facets['group'] = GroupProfile.objects.exclude( access="private").count() facets['layer'] = facets['raster'] + \ facets['vector'] + facets['remote'] + facets['wms'] return facets
def h_keywords(request): from geonode.base.models import HierarchicalKeyword as hk keywords = json.dumps(hk.dump_bulk_tree()) return HttpResponse(content=keywords)
def facets(context): request = context['request'] is_admin = False is_staff = False is_manager = False if request.user: is_admin = request.user.is_superuser if request.user else False is_staff = request.user.is_staff if request.user else False try: is_manager = request.user.groupmember_set.all().filter(role='manager').exists() except: is_manager = False title_filter = request.GET.get('title__icontains', '') extent_filter = request.GET.get('extent', None) keywords_filter = request.GET.getlist('keywords__slug__in', None) category_filter = request.GET.getlist('category__identifier__in', None) regions_filter = request.GET.getlist('regions__name__in', None) owner_filter = request.GET.getlist('owner__username__in', None) date_gte_filter = request.GET.get('date__gte', None) date_lte_filter = request.GET.get('date__lte', None) date_range_filter = request.GET.get('date__range', None) facet_type = context['facet_type'] if 'facet_type' in context else 'all' if not settings.SKIP_PERMS_FILTER: authorized = get_objects_for_user( request.user, 'base.view_resourcebase').values('id') if facet_type == 'documents': documents = Document.objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter(category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter(date__range=date_range_filter.split(',')) if settings.ADMIN_MODERATE_UPLOADS: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(group__in=group_list_all) | Q(owner__username__iexact=str(request.user))) except: documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: documents = documents.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.RESOURCE_PUBLISHING: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(group__in=group_list_all) | Q(owner__username__iexact=str(request.user))) except: documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: documents = documents.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.GROUP_PRIVATE_RESOURCES: public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') except: anonymous_group = None if is_admin: pass elif request.user: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass if anonymous_group: documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) else: documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: if anonymous_group: documents = documents.filter( Q(group__isnull=True) | Q(group__in=public_groups) | Q(group=anonymous_group)) else: documents = documents.filter( Q(group__isnull=True) | Q(group__in=public_groups)) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: documents = documents.filter(id__in=authorized) counts = documents.values('doc_type').annotate(count=Count('doc_type')) facets = dict([(count['doc_type'], count['count']) for count in counts]) return facets else: layers = Layer.objects.filter(title__icontains=title_filter) if category_filter: layers = layers.filter(category__identifier__in=category_filter) if regions_filter: layers = layers.filter(regions__name__in=regions_filter) if owner_filter: layers = layers.filter(owner__username__in=owner_filter) if date_gte_filter: layers = layers.filter(date__gte=date_gte_filter) if date_lte_filter: layers = layers.filter(date__lte=date_lte_filter) if date_range_filter: layers = layers.filter(date__range=date_range_filter.split(',')) if settings.ADMIN_MODERATE_UPLOADS: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: layers = layers.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.RESOURCE_PUBLISHING: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: layers = layers.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.GROUP_PRIVATE_RESOURCES: public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') except: anonymous_group = None if is_admin: pass elif request.user: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass if anonymous_group: layers = layers.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) else: layers = layers.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: if anonymous_group: layers = layers.filter( Q(group__isnull=True) | Q(group__in=public_groups) | Q(group=anonymous_group)) else: layers = layers.filter( Q(group__isnull=True) | Q(group__in=public_groups)) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) layers = layers.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except: # Ignore keywords not actually used? pass layers = layers.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: layers = layers.filter(id__in=authorized) counts = layers.values('storeType').annotate(count=Count('storeType')) count_dict = dict([(count['storeType'], count['count']) for count in counts]) facets = { 'raster': count_dict.get('coverageStore', 0), 'vector': count_dict.get('dataStore', 0), 'remote': count_dict.get('remoteStore', 0), 'wms': count_dict.get('wmsStore', 0), } # Break early if only_layers is set. if facet_type == 'layers': return facets maps = Map.objects.filter(title__icontains=title_filter) documents = Document.objects.filter(title__icontains=title_filter) if category_filter: maps = maps.filter(category__identifier__in=category_filter) documents = documents.filter(category__identifier__in=category_filter) if regions_filter: maps = maps.filter(regions__name__in=regions_filter) documents = documents.filter(regions__name__in=regions_filter) if owner_filter: maps = maps.filter(owner__username__in=owner_filter) documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: maps = maps.filter(date__gte=date_gte_filter) documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: maps = maps.filter(date__lte=date_lte_filter) documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: maps = maps.filter(date__range=date_range_filter.split(',')) documents = documents.filter(date__range=date_range_filter.split(',')) if settings.ADMIN_MODERATE_UPLOADS: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') maps = maps.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: maps = maps.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: maps = maps.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) documents = documents.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.RESOURCE_PUBLISHING: if not is_admin and not is_staff: if is_manager: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') maps = maps.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=groups) | Q(group__in=group_list_all) | Q(group__in=public_groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) except: maps = maps.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group__in=public_groups) | Q(owner__username__iexact=str(request.user))) else: maps = maps.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) documents = documents.filter(Q(is_published=True) | Q(owner__username__iexact=str(request.user))) if settings.GROUP_PRIVATE_RESOURCES: public_groups = GroupProfile.objects.exclude(access="private").values('group') try: anonymous_group = Group.objects.get(name='anonymous') except: anonymous_group = None if is_admin: pass elif request.user: groups = request.user.groups.all() group_list_all = [] try: group_list_all = request.user.group_list_all().values('group') except: pass if anonymous_group: maps = maps.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(group=anonymous_group) | Q(owner__username__iexact=str(request.user))) else: maps = maps.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(owner__username__iexact=str(request.user))) documents = documents.filter( Q(group__isnull=True) | Q(group__in=group_list_all) | Q(group__in=groups) | Q(owner__username__iexact=str(request.user))) else: if anonymous_group: maps = maps.filter(Q(group__isnull=True) | Q(group=anonymous_group)) documents = documents.filter(Q(group__isnull=True) | Q(group=anonymous_group)) else: maps = maps.filter(Q(group__isnull=True)) documents = documents.filter(Q(group__isnull=True)) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) maps = maps.filter(intersects) documents = documents.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except: # Ignore keywords not actually used? pass maps = maps.filter(Q(keywords__in=treeqs)) documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: maps = maps.filter(id__in=authorized) documents = documents.filter(id__in=authorized) facets['map'] = maps.count() facets['document'] = documents.count() if facet_type == 'home': facets['user'] = get_user_model().objects.exclude( username='******').count() facets['group'] = GroupProfile.objects.exclude( access="private").count() facets['layer'] = facets['raster'] + \ facets['vector'] + facets['remote'] + facets['wms'] return facets
def h_keywords(request): from geonode.base.models import HierarchicalKeyword as hk keywords = json.dumps( hk.dump_bulk_tree(request.user, type=request.GET.get('type', None))) return HttpResponse(content=keywords)
def get_or_create(keyword): try: return HierarchicalKeyword.objects.get(name=keyword) except HierarchicalKeyword.DoesNotExist: return HierarchicalKeyword.add_root(name=keyword)
def dataresources(context): request = context['request'] title_filter = request.GET.get('title__icontains', '') extent_filter = request.GET.get('extent', None) keywords_filter = request.GET.getlist('keywords__slug__in', None) category_filter = request.GET.getlist('category__identifier__in', None) regions_filter = request.GET.getlist('regions__name__in', None) owner_filter = request.GET.getlist('owner__username__in', None) date_gte_filter = request.GET.get('date__gte', None) date_lte_filter = request.GET.get('date__lte', None) date_range_filter = request.GET.get('date__range', None) facet_type = context['facet_type'] if 'facet_type' in context else 'all' if facet_type == 'documents': documents = context.get('basemodel', Document).objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter(category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter(date__range=date_range_filter.split(',')) if 'datasource__in' in request.GET: documents = documents.filter(datasource__in=request.GET.get('datasource__in')) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except BaseException: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) # if not settings.SKIP_PERMS_FILTER: # documents = documents.filter(id__in=authorized) # return facets counts = documents.values('datasource').annotate(datasource_count=Count('datasource')) facets = dict([(count['datasource'], {'title':count['datasource'],'count':count['datasource_count'], 'facet_type':'documents'}) for count in counts if count['datasource'] ]) return facets else: layers = Layer.objects.filter(title__icontains=title_filter) if category_filter: layers = layers.filter(category__identifier__in=category_filter) if regions_filter: layers = layers.filter(regions__name__in=regions_filter) if owner_filter: layers = layers.filter(owner__username__in=owner_filter) if date_gte_filter: layers = layers.filter(date__gte=date_gte_filter) if date_lte_filter: layers = layers.filter(date__lte=date_lte_filter) if date_range_filter: layers = layers.filter(date__range=date_range_filter.split(',')) if 'store__in' in request.GET: layers = layers.filter(store__in=request.GET.get('store__in')) layers = get_visible_resources( layers, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) layers = layers.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except BaseException: # Ignore keywords not actually used? pass layers = layers.filter(Q(keywords__in=treeqs)) # if not settings.SKIP_PERMS_FILTER: # layers = layers.filter(id__in=authorized) counts = layers.values('storeType').annotate(count=Count('storeType')) facets = {} for count in counts: if count['storeType'] == 'remoteStore': count_service = layers.values('remote_service').filter(storeType__exact='remoteStore').annotate(store_count=Count('store')) for cs in count_service: remoteId = cs['remote_service'] # print(remoteId) getremoteurl = Service.objects.values('base_url','name','title').filter(id=remoteId) remoteurlcount = dict([(gm['name'], {'title':gm['title'], 'count':cs['store_count'], 'facet_type':'layers'}) for gm in getremoteurl]) facets.update(remoteurlcount) if facet_type == 'layers': return facets documents = Document.objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter(category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter(date__range=date_range_filter.split(',')) if 'datasource__in' in request.GET: documents = documents.filter(datasource__in=request.GET.get('datasource__in')) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) documents = documents.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except BaseException: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) # if not settings.SKIP_PERMS_FILTER: # maps = maps.filter(id__in=authorized) # documents = documents.filter(id__in=authorized) documentcounts = documents.values('datasource').annotate(datasource_count=Count('datasource')) facets.update(dict([(count['datasource'], {'title':count['datasource'],'count':count['datasource_count'], 'facet_type':'documents'}) for count in documentcounts if count['datasource'] ])) return facets
def facets(context): request = context['request'] title_filter = request.GET.get('title__icontains', '') extent_filter = request.GET.get('extent', None) keywords_filter = request.GET.getlist('keywords__slug__in', None) category_filter = request.GET.getlist('category__identifier__in', None) regions_filter = request.GET.getlist('regions__name__in', None) owner_filter = request.GET.getlist('owner__username__in', None) date_gte_filter = request.GET.get('date__gte', None) date_lte_filter = request.GET.get('date__lte', None) date_range_filter = request.GET.get('date__range', None) facet_type = context['facet_type'] if 'facet_type' in context else 'all' if not settings.SKIP_PERMS_FILTER: authorized = [] try: authorized = get_objects_for_user( request.user, 'base.view_resourcebase').values('id') except BaseException: pass if facet_type == 'documents': documents = Document.objects.filter(title__icontains=title_filter) if category_filter: documents = documents.filter(category__identifier__in=category_filter) if regions_filter: documents = documents.filter(regions__name__in=regions_filter) if owner_filter: documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: documents = documents.filter(date__range=date_range_filter.split(',')) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except BaseException: # Ignore keywords not actually used? pass documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: documents = documents.filter(id__in=authorized) counts = documents.values('doc_type').annotate(count=Count('doc_type')) facets = dict([(count['doc_type'], count['count']) for count in counts]) return facets else: layers = Layer.objects.filter(title__icontains=title_filter) if category_filter: layers = layers.filter(category__identifier__in=category_filter) if regions_filter: layers = layers.filter(regions__name__in=regions_filter) if owner_filter: layers = layers.filter(owner__username__in=owner_filter) if date_gte_filter: layers = layers.filter(date__gte=date_gte_filter) if date_lte_filter: layers = layers.filter(date__lte=date_lte_filter) if date_range_filter: layers = layers.filter(date__range=date_range_filter.split(',')) layers = get_visible_resources( layers, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) layers = layers.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except BaseException: # Ignore keywords not actually used? pass layers = layers.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: layers = layers.filter(id__in=authorized) counts = layers.values('storeType').annotate(count=Count('storeType')) counts_array = [] try: for count in counts: counts_array.append((count['storeType'], count['count'])) except BaseException: pass count_dict = dict(counts_array) vector_time_series = layers.exclude(has_time=False).filter(storeType='dataStore'). \ values('storeType').annotate(count=Count('storeType')) if vector_time_series: count_dict['vectorTimeSeries'] = vector_time_series[0]['count'] facets = { 'raster': count_dict.get('coverageStore', 0), 'vector': count_dict.get('dataStore', 0), 'vector_time': count_dict.get('vectorTimeSeries', 0), 'remote': count_dict.get('remoteStore', 0), 'wms': count_dict.get('wmsStore', 0), } # Break early if only_layers is set. if facet_type == 'layers': return facets maps = Map.objects.filter(title__icontains=title_filter) documents = Document.objects.filter(title__icontains=title_filter) if category_filter: maps = maps.filter(category__identifier__in=category_filter) documents = documents.filter(category__identifier__in=category_filter) if regions_filter: maps = maps.filter(regions__name__in=regions_filter) documents = documents.filter(regions__name__in=regions_filter) if owner_filter: maps = maps.filter(owner__username__in=owner_filter) documents = documents.filter(owner__username__in=owner_filter) if date_gte_filter: maps = maps.filter(date__gte=date_gte_filter) documents = documents.filter(date__gte=date_gte_filter) if date_lte_filter: maps = maps.filter(date__lte=date_lte_filter) documents = documents.filter(date__lte=date_lte_filter) if date_range_filter: maps = maps.filter(date__range=date_range_filter.split(',')) documents = documents.filter(date__range=date_range_filter.split(',')) maps = get_visible_resources( maps, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) documents = get_visible_resources( documents, request.user if request else None, admin_approval_required=settings.ADMIN_MODERATE_UPLOADS, unpublished_not_visible=settings.RESOURCE_PUBLISHING, private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES) if extent_filter: bbox = extent_filter.split( ',') # TODO: Why is this different when done through haystack? bbox = map(str, bbox) # 2.6 compat - float to decimal conversion intersects = ~(Q(bbox_x0__gt=bbox[2]) | Q(bbox_x1__lt=bbox[0]) | Q(bbox_y0__gt=bbox[3]) | Q(bbox_y1__lt=bbox[1])) maps = maps.filter(intersects) documents = documents.filter(intersects) if keywords_filter: treeqs = HierarchicalKeyword.objects.none() for keyword in keywords_filter: try: kws = HierarchicalKeyword.objects.filter(name__iexact=keyword) for kw in kws: treeqs = treeqs | HierarchicalKeyword.get_tree(kw) except BaseException: # Ignore keywords not actually used? pass maps = maps.filter(Q(keywords__in=treeqs)) documents = documents.filter(Q(keywords__in=treeqs)) if not settings.SKIP_PERMS_FILTER: maps = maps.filter(id__in=authorized) documents = documents.filter(id__in=authorized) facets['map'] = maps.count() facets['document'] = documents.count() if facet_type == 'home': facets['user'] = get_user_model().objects.exclude( username='******').count() facets['group'] = GroupProfile.objects.exclude( access="private").count() facets['layer'] = facets['raster'] + \ facets['vector'] + facets['remote'] + facets['wms'] # + facets['vector_time'] return facets