From c4b7ef91b5ad08c9e98f0bb8437fa770752a2e39 Mon Sep 17 00:00:00 2001 From: Abdelrahman Moustafa Date: Wed, 10 Jul 2024 16:59:53 +0200 Subject: [PATCH 1/2] Seperate extracting and fetchng users from UserReport class --- opengever/globalindex/browser/report.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/opengever/globalindex/browser/report.py b/opengever/globalindex/browser/report.py index 6c6dbbaed2..4d6537ae38 100644 --- a/opengever/globalindex/browser/report.py +++ b/opengever/globalindex/browser/report.py @@ -91,8 +91,7 @@ def check_permissions(self): if not is_administrator(): raise Unauthorized - def __call__(self): - self.check_permissions() + def extract_user_ids_from_request(self): user_ids = self.request.form.get("user_ids") if not user_ids: @@ -105,10 +104,17 @@ def __call__(self): else: return self.request.RESPONSE.redirect( self.context.absolute_url()) + return user_ids + def fetch_users(self): + user_ids = self.extract_user_ids_from_request() users = [ogds_service().fetch_user(user_id) for user_id in user_ids] users = [user for user in users if user] + return users + def __call__(self): + self.check_permissions() + users = self.fetch_users() reporter = XLSReporter( self.context.REQUEST, self.columns(), From dcfc21245fc47b836017dcbff2c9ab6ce50224f8 Mon Sep 17 00:00:00 2001 From: Abdelrahman Moustafa Date: Wed, 10 Jul 2024 17:10:17 +0200 Subject: [PATCH 2/2] Include group users in Teamraum participant Excel export --- changes/TI-669.other | 1 + opengever/workspace/report.py | 16 +++ .../test_workspace_participation_reporter.py | 109 ++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 changes/TI-669.other create mode 100644 opengever/workspace/tests/test_workspace_participation_reporter.py diff --git a/changes/TI-669.other b/changes/TI-669.other new file mode 100644 index 0000000000..d6797d4b15 --- /dev/null +++ b/changes/TI-669.other @@ -0,0 +1 @@ +Include group users in workspace participant Excel export and ensure unique user listing. [amo] diff --git a/opengever/workspace/report.py b/opengever/workspace/report.py index b7d3b18231..9e73777a1b 100644 --- a/opengever/workspace/report.py +++ b/opengever/workspace/report.py @@ -1,4 +1,5 @@ from opengever.globalindex.browser.report import UserReport +from opengever.ogds.models.service import ogds_service from zope.i18n import translate from zope.i18nmessageid import MessageFactory @@ -20,3 +21,18 @@ def filename(self): def check_permissions(self): pass + + def fetch_users(self): + user_ids = self.extract_user_ids_from_request() + users = set() + + for user_id in user_ids: + group_members = ogds_service().fetch_group(user_id) + if group_members: + users.update(group_members.users) + else: + user = ogds_service().fetch_user(user_id) + if user: + users.add(user) + + return list(users) diff --git a/opengever/workspace/tests/test_workspace_participation_reporter.py b/opengever/workspace/tests/test_workspace_participation_reporter.py new file mode 100644 index 0000000000..b9fbdf12b6 --- /dev/null +++ b/opengever/workspace/tests/test_workspace_participation_reporter.py @@ -0,0 +1,109 @@ +from ftw.builder.builder import Builder +from ftw.builder.builder import create +from ftw.testbrowser import browsing +from opengever.ogds.models.service import ogds_service +from opengever.testing import IntegrationTestCase +from openpyxl import load_workbook +from tempfile import NamedTemporaryFile + + +class TestWorkspaceParticipationReporter(IntegrationTestCase): + + @browsing + def test_empty_users_report(self, browser): + self.login(self.administrator, browser=browser) + browser.open(view='workspace_participants_report', data={'user_ids': []}) + + self.assertEquals('Error You have not selected any items.', + browser.css('.portalMessage.error').text[0]) + + @browsing + def test_download_users_xlsx(self, browser): + """Test downloading a user report in XLSX format. + This test verifies that the user report correctly generates an XLSX file + including individual users and users within a group. + """ + self.login(self.administrator, browser=browser) + + # Create a group and add regular_user to the group + group_users = ogds_service().find_user(self.regular_user.id) + group = create(Builder('ogds_group') + .having(groupid='group1', + title='Group 1', users=[group_users, ])) + + user_ids = { + 'user_ids': [ + self.regular_user.id, + self.administrator.id, + group.groupid + ] + } + + browser.open(view='workspace_participants_report', data=user_ids) + self.assertEqual(browser.status_code, 200) + with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmpfile: + tmpfile.write(browser.contents) + tmpfile.flush() + workbook = load_workbook(tmpfile.name) + + task_cells = list(workbook.active.rows) + + # if ror_num != 0 will remove the table header + cell_values = [[cell.value for cell in row] for row_num, row in enumerate(task_cells) if row_num != 0] + expected_values = [ + [ + u'kathi.barfuss', + True, + u'K\xe4thi', + u'B\xe4rfuss', + u'B\xe4rfuss K\xe4thi', + u'Staatsarchiv', + u'Arch', + u'Staatskanzlei', + u'SK', + None, + u'foo@example.com', + u'bar@example.com', + u'http://www.example.com', + u'012 34 56 78', + u'012 34 56 77', + u'012 34 56 76', + u'Frau', + u'Gesch\xe4ftsf\xfchrerin', + u'nix', + u'Kappelenweg 13', + u'Postfach 1234', + u'1234', + u'Vorkappelen', + u'Schweiz', + None + ], + [ + u'nicole.kohler', + True, + u'Nicole', + u'Kohler', + u'Kohler Nicole', + None, + None, + None, + None, + None, + u'nicole.kohler@gever.local', + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None + ] + ] + self.assertSequenceEqual(expected_values, cell_values)