Skip to content

Commit

Permalink
[Fixes #5699] Wrong resource number shown in group and group categori…
Browse files Browse the repository at this point in the history
…es search filter

(cherry picked from commit 0bf5e1d6ef53fb7b73ecf73c3d6e312a47fbfb99)
  • Loading branch information
afabiani committed Feb 17, 2020
1 parent 1d82d31 commit 2abfb26
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ install:
sudo update-java-alternatives --set java-1.8.0-openjdk-amd64;
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::");
export PATH=$JAVA_HOME'bin/java':$PATH;
pip install -r requirements.txt --upgrade;
pip install -e . --upgrade;
pip install -r requirements.txt;
pip install -e .;
pip install pygdal==`gdal-config --version`.*;
pip install codecov;
fi
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MAINTAINER GeoNode development team

COPY requirements.txt /usr/src/app/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt --upgrade
RUN pip install -r requirements.txt
RUN python manage.py makemigrations --settings=geonode.settings
RUN python manage.py migrate --settings=geonode.settings

Expand Down
4 changes: 2 additions & 2 deletions docs/devel/installation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ If you didn't install GeoNode-Core earlier and you wanted to install GeoNode-Pro
.. code-block:: shell
$ cd my_geonode
$ pip install -r requirements.txt --upgrade
$ pip install -e . --upgrade
$ pip install -r requirements.txt
$ pip install -e .
6- Install GDAL Utilities for Python

Expand Down
4 changes: 2 additions & 2 deletions docs/install/core/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ At this point your command prompt shows a ``(geonode)`` prefix, this indicates t
# Install the Python packages
cd /opt/geonode
pip install -r requirements.txt --upgrade --no-cache --no-cache-dir
pip install -e . --upgrade
pip install -r requirements.txt
pip install -e .
# Install GDAL Utilities for Python
pip install pygdal=="`gdal-config --version`.*"
Expand Down
4 changes: 2 additions & 2 deletions docs/install/project/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ Make an instance out of the ``Django Template``
# Install the Python packages
cd /opt/geonode_custom/my_geonode
pip install -r requirements.txt --upgrade --no-cache --no-cache-dir
pip install -e . --upgrade
pip install -r requirements.txt
pip install -e .
# Install GDAL Utilities for Python
pip install pygdal=="`gdal-config --version`.*"
Expand Down
84 changes: 57 additions & 27 deletions geonode/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def get_resources_counts(self, options):
resources = resources.filter(title__icontains=options['title_filter'])

if options['type_filter']:
resources = resources.instance_of(options['type_filter'])
resources = resources.filter(polymorphic_ctype__model=options['type_filter'])

counts = list(resources.values(options['count_type']).annotate(count=Count(options['count_type'])))

Expand Down Expand Up @@ -254,7 +254,7 @@ class TopicCategoryResource(TypeFilteredResource):
def dehydrate_layers_count(self, bundle):
request = bundle.request
obj_with_perms = get_objects_for_user(request.user,
'base.view_resourcebase').instance_of(Layer)
'base.view_resourcebase').filter(polymorphic_ctype__model='layer')
filter_set = bundle.obj.resourcebase_set.filter(id__in=obj_with_perms.values('id'))

if not settings.SKIP_PERMS_FILTER:
Expand Down Expand Up @@ -298,6 +298,26 @@ class Meta:
'name': ALL}
authorization = ApiLockdownAuthorization()

def apply_filters(self, request, applicable_filters):
user = request.user
semi_filtered = super(
GroupCategoryResource,
self).apply_filters(
request,
applicable_filters)

filtered = semi_filtered
if not user.is_authenticated or user.is_anonymous:
filtered = semi_filtered.exclude(groups__access='private')
elif not user.is_superuser:
groups_member_of = user.group_list_all()
filtered = semi_filtered.filter(
Q(groups__in=groups_member_of) |
~Q(groups__access='private')
)

return filtered

def dehydrate_detail_url(self, bundle):
return bundle.obj.get_absolute_url()

Expand All @@ -306,8 +326,10 @@ def dehydrate_member_count(self, bundle):

def dehydrate(self, bundle):
"""Provide additional resource counts"""

request = bundle.request
_user = request.user
counts = _get_resource_counts(
_user,
resourcebase_filter_kwargs={
'group__groupprofile__categories': bundle.obj
}
Expand Down Expand Up @@ -394,9 +416,12 @@ def apply_filters(self, request, applicable_filters):

def dehydrate(self, bundle):
"""Provide additional resource counts"""

request = bundle.request
_user = request.user
counts = _get_resource_counts(
resourcebase_filter_kwargs={'group': bundle.obj})
_user,
resourcebase_filter_kwargs={'group': bundle.obj}
)
bundle.data.update(resource_counts=counts)
return bundle

Expand Down Expand Up @@ -467,17 +492,17 @@ def dehydrate_email(self, bundle):

def dehydrate_layers_count(self, bundle):
obj_with_perms = get_objects_for_user(bundle.request.user,
'base.view_resourcebase').instance_of(Layer)
'base.view_resourcebase').filter(polymorphic_ctype__model='layer')
return bundle.obj.resourcebase_set.filter(id__in=obj_with_perms.values('id')).distinct().count()

def dehydrate_maps_count(self, bundle):
obj_with_perms = get_objects_for_user(bundle.request.user,
'base.view_resourcebase').instance_of(Map)
'base.view_resourcebase').filter(polymorphic_ctype__model='map')
return bundle.obj.resourcebase_set.filter(id__in=obj_with_perms.values('id')).distinct().count()

def dehydrate_documents_count(self, bundle):
obj_with_perms = get_objects_for_user(bundle.request.user,
'base.view_resourcebase').instance_of(Document)
'base.view_resourcebase').filter(polymorphic_ctype__model='document')
return bundle.obj.resourcebase_set.filter(id__in=obj_with_perms.values('id')).distinct().count()

def dehydrate_avatar_100(self, bundle):
Expand Down Expand Up @@ -819,31 +844,36 @@ class StyleResource(GeoserverStyleResource):
pass


def _get_resource_counts(
resourcebase_filter_kwargs
):
def _get_resource_counts(user, resourcebase_filter_kwargs):
"""Return a dict with counts of resources of various types
The ``resourcebase_filter_kwargs`` argument should be a dict with a suitable
queryset filter that can be applied to select only the relevant
``ResourceBase`` objects to use when retrieving counts. For example::
_get_resource_counts({
'group__slug': 'my-group',
})
_get_resource_counts(
user,
{
'group__slug': 'my-group',
}
)
The above function call would result in only counting ``ResourceBase``
objects that belong to the group that has ``my-group`` as slug
"""

qs = ResourceBase.objects.filter(
**resourcebase_filter_kwargs
).values(
resources = get_visible_resources(
ResourceBase.objects.filter(**resourcebase_filter_kwargs),
user,
admin_approval_required=settings.ADMIN_MODERATE_UPLOADS,
unpublished_not_visible=settings.RESOURCE_PUBLISHING,
private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES)
values = resources.values(
'polymorphic_ctype__model',
'is_approved',
'is_published',
).annotate(counts=Count('polymorphic_ctype__model'))
)
qs = values.annotate(counts=Count('polymorphic_ctype__model'))
types = [
'layer',
'document',
Expand All @@ -861,14 +891,14 @@ def _get_resource_counts(
for record in qs:
resource_type = record['polymorphic_ctype__model']
is_visible = all((record['is_approved'], record['is_published']))
counts['all']['total'] += 1
counts['all']['visible'] += 1 if is_visible else 0
counts['all']['published'] += 1 if record['is_published'] else 0
counts['all']['approved'] += 1 if record['is_approved'] else 0
counts['all']['total'] += record['counts']
counts['all']['visible'] += record['counts'] if is_visible else 0
counts['all']['published'] += record['counts'] if record['is_published'] else 0
counts['all']['approved'] += record['counts'] if record['is_approved'] else 0
section = counts.get(resource_type)
if section is not None:
section['total'] += 1
section['visible'] += 1 if is_visible else 0
section['published'] += 1 if record['is_published'] else 0
section['approved'] += 1 if record['is_approved'] else 0
section['total'] += record['counts']
section['visible'] += record['counts'] if is_visible else 0
section['published'] += record['counts'] if record['is_published'] else 0
section['approved'] += record['counts'] if record['is_approved'] else 0
return counts
6 changes: 2 additions & 4 deletions geonode/api/resourcebase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,9 @@ def apply_filters(self, request, applicable_filters):
Layer___storeType=LAYER_SUBTYPES[super_type])
else:
if filtered:
filtered = filtered | semi_filtered.instance_of(
FILTER_TYPES[the_type])
filtered = filtered | semi_filtered.filter(polymorphic_ctype__model=FILTER_TYPES[the_type])
else:
filtered = semi_filtered.instance_of(
FILTER_TYPES[the_type])
filtered = semi_filtered.filter(polymorphic_ctype__model=FILTER_TYPES[the_type])
else:
filtered = semi_filtered

Expand Down
2 changes: 1 addition & 1 deletion geonode/base/management/commands/updategeoip.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

try:
from django.contrib.gis.geoip2 import GeoIP2 as GeoIP
URL = 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&date=20200107&license_key=s0L0UN5Vsg8YDmVy&suffix=tar.gz'
URL = 'https://build.geo-solutions.it/geonode/geoserver/latest/GeoLite2-City.tar.gz'
OLD_FORMAT = False
except ImportError:
try:
Expand Down
10 changes: 6 additions & 4 deletions geonode/geoserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,12 @@ def layer_acls(request):

# Include permissions on the anonymous user
# use of polymorphic selectors/functions to optimize performances
resources_readable = get_objects_for_user(acl_user, 'view_resourcebase',
ResourceBase.objects.instance_of(Layer)).values_list('id', flat=True)
layer_writable = get_objects_for_user(acl_user, 'change_layer_data',
Layer.objects.all())
resources_readable = get_objects_for_user(
acl_user, 'view_resourcebase',
ResourceBase.objects.filter(polymorphic_ctype__model='layer')).values_list('id', flat=True)
layer_writable = get_objects_for_user(
acl_user, 'change_layer_data',
Layer.objects.all())

_read = set(
Layer.objects.filter(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ django-polymorphic==2.1.2
django-tastypie<0.15.0
oauthlib==3.1.0
pyopenssl==19.1.0
jsonfield==2.1.1

# geopython dependencies
pyproj>=1.9.5,<2.2.3.0
Expand Down
2 changes: 1 addition & 1 deletion scripts/cloud/fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def deploy_project(project):
put(projdir,PYLIBS,use_sudo=True)
put('requirements.txt',GEONODEDIR,use_sudo=True)
with cd(GEONODEDIR), prefix(ACT):
sudo('pip install -r requirements.txt --upgrade')
sudo('pip install -r requirements.txt')
sudo('rm requirements.txt')
put('%s/%s.apache' % (project,project),'/etc/apache2/sites-available/%s' % project, use_sudo=True)
sed('/etc/apache2/sites-available/%s' % project, 'REPLACE_WITH_SITEDIR', PYLIBS, use_sudo=True)
Expand Down
2 changes: 1 addition & 1 deletion scripts/spcgeonode/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RUN pip install pygdal==$(gdal-config --version).*
RUN mkdir /spcgeonode
WORKDIR /spcgeonode/
ADD . /spcgeonode/
RUN pip install -e . --upgrade
RUN pip install -e .
RUN chmod +x scripts/spcgeonode/django/docker-entrypoint.sh

# Export ports
Expand Down
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def initialized(ctx):
@task
def devrequirements(ctx):
print "*********************install dev requirements**********************"
ctx.run('pip install -r requirements_dev.txt --upgrade')
ctx.run('pip install -r requirements_dev.txt')


def _update_db_connstring():
Expand Down

0 comments on commit 2abfb26

Please sign in to comment.