Skip to content

Commit

Permalink
Give to Editor Level 1 user upload geo-data grant (#594)
Browse files Browse the repository at this point in the history
* Add capabilities to add files into filemanager to Editor level 1 users.

* Add sidebar menu for Editor level 1.

* Fix tests.

* RAdd testing for creations of a subdirectory inside the settings.DATASOURCE_PATH for Editor Level 1 user.

* Typo.

* Fix test typos.

---------

Co-authored-by: wlorenzetti <[email protected]>
  • Loading branch information
wlorenzetti and wlorenzetti authored Sep 14, 2023
1 parent 319b112 commit a419a63
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 460 deletions.
4 changes: 4 additions & 0 deletions g3w-admin/filemanager/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class FilemanagerConfig(AppConfig):
name = 'filemanager'

def ready(self):

# Import signals receivers
import filemanager.receivers

from django.conf import settings
import filemanager.filemanager_settings
for a in dir(filemanager.filemanager_settings):
Expand Down
41 changes: 41 additions & 0 deletions g3w-admin/filemanager/receivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# coding=utf-8
"""" Django signals receivers
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the Mozilla Public License 2.0.
"""

__author__ = "[email protected]"
__date__ = "2023-08-29"
__copyright__ = "Copyright 2015 - 2023, Gis3w"
__license__ = "MPL 2.0"

from django.conf import settings
from django.dispatch import receiver
from usersmanage.signals import after_save_user_form
from usersmanage.configs import G3W_EDITOR1
from usersmanage.utils import userHasGroups

from os import makedirs, path
import logging

logger = logging.getLogger('filemanager')



@receiver(after_save_user_form)
def make_project_data_subdir(sender, user, **kwargs):
"""
Given a user instance create if not exists a folder into `settings.DATASOURCE_PATH`
"""

# Check if user is an Editor level 1
if not userHasGroups(user, [G3W_EDITOR1]):
return

makedirs(path.join(settings.DATASOURCE_PATH, user.username), exist_ok=True)





6 changes: 6 additions & 0 deletions g3w-admin/filemanager/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def test_files_folders_get(self):
# as editor1
client.login(username=self.test_editor1.username, password=self.test_editor1.username)
res = client.get(url)
self.assertEqual(res.status_code, 200)
client.logout()

# as viewer1
client.login(username=self.test_viewer1.username, password=self.test_viewer1.username)
res = client.get(url)
self.assertEqual(res.status_code, 403)
client.logout()

Expand Down
14 changes: 12 additions & 2 deletions g3w-admin/filemanager/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_main_view(self):
url = reverse('filemanager-home')
client = Client()

print(url)
# TEST ACL
# ====================================
# not login: redirect to login page.
Expand All @@ -36,6 +35,12 @@ def test_main_view(self):
# as editor1
client.login(username=self.test_editor1.username, password=self.test_editor1.username)
res = client.get(url)
self.assertEqual(res.status_code, 200)
client.logout()

# as viewer1
client.login(username=self.test_viewer1.username, password=self.test_viewer1.username)
res = client.get(url)
self.assertEqual(res.status_code, 403)
client.logout()

Expand All @@ -52,7 +57,6 @@ def test_server_config(self):
url = reverse('filemanager-serve-file-config', args=['filemanager.config.json'])
client = Client()

print (url)
# TEST ACL
# ====================================
# not login: redirect to login page.
Expand All @@ -63,6 +67,12 @@ def test_server_config(self):
# as editor1
client.login(username=self.test_editor1.username, password=self.test_editor1.username)
res = client.get(url)
self.assertEqual(res.status_code, 200)
client.logout()

# as viewer1
client.login(username=self.test_viewer1.username, password=self.test_viewer1.username)
res = client.get(url)
self.assertEqual(res.status_code, 403)
client.logout()

Expand Down
11 changes: 8 additions & 3 deletions g3w-admin/filemanager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.http.response import JsonResponse, HttpResponse
from django.utils.decorators import method_decorator
from usersmanage.decorators import user_passes_test_or_403
from usersmanage.utils import userHasGroups, G3W_EDITOR1
from .filemanager import FileManager
import json
import os
Expand All @@ -13,7 +14,7 @@ class FilemanagerView(TemplateView):
"""Main templateview for fielmanager"""
template_name = 'filemanager/filemanager.html'

@method_decorator(user_passes_test_or_403(lambda u: u.is_superuser))
@method_decorator(user_passes_test_or_403(lambda u: u.is_superuser or userHasGroups(u, [G3W_EDITOR1])))
def dispatch(self, *args, **kwargs):
return super(FilemanagerView, self).dispatch(*args, **kwargs)

Expand All @@ -30,7 +31,7 @@ class FilemanagerServeConfigView(View):
""" Filemanager main config view
Return main RichFileManager json config settings.
"""
@method_decorator(user_passes_test_or_403(lambda u: u.is_superuser))
@method_decorator(user_passes_test_or_403(lambda u: u.is_superuser or userHasGroups(u, [G3W_EDITOR1])))
def dispatch(self, *args, **kwargs):
return super(FilemanagerServeConfigView, self).dispatch(*args, **kwargs)

Expand Down Expand Up @@ -59,7 +60,7 @@ def get(self, request, *args, **kwargs):


@csrf_exempt
@user_passes_test_or_403(lambda u: u.is_superuser)
@user_passes_test_or_403(lambda u: u.is_superuser or userHasGroups(u, [G3W_EDITOR1]))
def files_view(request):
"""File Manager API endpoint"""

Expand All @@ -74,6 +75,10 @@ def files_view(request):
else:
root_folder = settings.DATASOURCE_PATH

# Append user.username to the root_folder if user is an Editor level 1
if userHasGroups(request.user, [G3W_EDITOR1]):
root_folder = os.path.join(root_folder, request.user.username)

fileManager = FileManager(request, root_folder=root_folder)

mode = None
Expand Down
Loading

0 comments on commit a419a63

Please sign in to comment.