Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add "piccolo user list" command #921

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/piccolo/authentication/baseuser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ script), you can pass all of the arguments in as follows:
If you choose this approach then be careful, as the password will be in the
shell's history.

list
~~~~

List existing users.

change_password
~~~~~~~~~~~~~~~

Expand Down
15 changes: 15 additions & 0 deletions piccolo/apps/user/commands/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from piccolo.apps.user.tables import BaseUser
from piccolo.utils.printing import print_dict_table


def list():
"""
List existing users.
"""
users = (
BaseUser.select(BaseUser.all_columns(exclude=[BaseUser.password]))
.order_by(BaseUser.username)
.run_sync()
)

print_dict_table(users)
2 changes: 2 additions & 0 deletions piccolo/apps/user/piccolo_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .commands.change_password import change_password
from .commands.change_permissions import change_permissions
from .commands.create import create
from .commands.list import list
from .tables import BaseUser

CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -19,6 +20,7 @@
migration_dependencies=[],
commands=[
Command(callable=create, aliases=["new"]),
Command(callable=list, aliases=["ls"]),
Command(callable=change_password, aliases=["password", "pass"]),
Command(callable=change_permissions, aliases=["perm", "perms"]),
],
Expand Down
36 changes: 36 additions & 0 deletions piccolo/utils/printing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import List


def get_fixed_length_string(string: str, length=20) -> str:
"""
Add spacing to the end of the string so it's a fixed length, or truncate
Expand All @@ -16,3 +19,36 @@ def print_heading(string: str, width: int = 64) -> None:
"""
print(f"\n{string.upper():^{width}}")
print("-" * width)


def print_dict_table(data: List[dict], header_separator: bool = False) -> None:
"""
Prints out a list of dictionaries in tabular form.
Uses the first list element to extract the
column names and their order within the row.
"""

if len(data) < 1:
print("No data")
return

ref_order = [column for column in data[0]]
width = {column: len(str(column)) for column in ref_order}

for item in data:
for column in ref_order:
if len(str(item[column])) > width[column]:
width[column] = len(str(item[column]))

format_string = " | ".join([f"{{:<{width[w]}}}" for w in ref_order])

print(format_string.format(*[str(w) for w in ref_order]))

if header_separator:
format_string_sep = "-+-".join(
[f"{{:<{width[w]}}}" for w in ref_order]
)
print(format_string_sep.format(*["-" * width[w] for w in ref_order]))

for item in data:
print(format_string.format(*[str(item[w]) for w in ref_order]))
Loading