From e80912c02d6f96d80c1223c17e70b30382ef4555 Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Wed, 12 Feb 2020 10:07:33 +0100
Subject: [PATCH] [databases][commands] Improve database map output

The scripts now lists by default only the users with
an active account. There's an option that will print
all users with their activate state.

The user email has been added to the output.
---
 .../commands/print_db_access_map.py           | 22 ++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/beat/web/databases/management/commands/print_db_access_map.py b/beat/web/databases/management/commands/print_db_access_map.py
index 3445689de..2f7b2e168 100644
--- a/beat/web/databases/management/commands/print_db_access_map.py
+++ b/beat/web/databases/management/commands/print_db_access_map.py
@@ -42,6 +42,13 @@ class Command(BaseCommand):
             default=None,
             help="Path element up to which the the string must be cleaned",
         )
+        parser.add_argument(
+            "--show-all",
+            action="store_true",
+            dest="show_all",
+            default=False,
+            help="Show all users",
+        )
 
     def handle(self, *ignored, **options):
 
@@ -50,6 +57,7 @@ class Command(BaseCommand):
         for database in Database.objects.all():
             folder = database.declaration["root_folder"]
             folder_filter = options["folder_filter"]
+            show_all = options["show_all"]
             if folder_filter:
                 try:
                     start = folder.rindex(folder_filter) + len(folder_filter)
@@ -61,10 +69,14 @@ class Command(BaseCommand):
                 database.fullname(), database.get_sharing_display(), folder
             )
 
-            for user in [
-                "{}: {} {}".format(user.username, user.first_name, user.last_name)
-                for user in database.all_shared_with_users()
-            ]:
-                self.stdout.write("{}, {}".format(db_info, user))
+            for user in database.all_shared_with_users():
+                if user.is_active or show_all:
+                    user_info = "{}: {} {} {}".format(
+                        user.username, user.first_name, user.last_name, user.email
+                    )
+                    message = "{}, {}".format(db_info, user_info)
+                    if show_all:
+                        message += " active: {}".format(user.is_active)
+                    self.stdout.write(message)
             else:
                 self.stdout.write(db_info)
-- 
GitLab