• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rero / sonar / 17121500737

21 Aug 2025 08:28AM UTC coverage: 96.245% (-0.1%) from 96.376%
17121500737

push

github

Garfield-fr
feat(document): add latex transformation

* Closes #875.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>

7945 of 8255 relevant lines covered (96.24%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/sonar/modules/users/cli.py
1
# -*- coding: utf-8 -*-
2
#
3
# Swiss Open Access Repository
4
# Copyright (C) 2021 RERO
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, version 3 of the License.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17

18
"""Click command-line interface for user management."""
19

20
from __future__ import absolute_import, print_function
1✔
21

22
import json
1✔
23

24
import click
1✔
25
from click.exceptions import ClickException
1✔
26
from flask import current_app
1✔
27
from flask.cli import with_appcontext
1✔
28
from flask_security.confirmable import confirm_user
1✔
29
from werkzeug.local import LocalProxy
1✔
30

31
from ..users.api import UserRecord
1✔
32

33
datastore = LocalProxy(lambda: current_app.extensions["security"].datastore)
1✔
34

35

36
@click.group()
1✔
37
def users():
1✔
38
    """Users CLI commands."""
39

40

41
@users.command("import")
1✔
42
@click.argument("infile", type=click.File("r"))
1✔
43
@with_appcontext
1✔
44
def import_users(infile):
1✔
45
    """Import users."""
46
    click.secho(f"Importing users from {infile.name}")
1✔
47

48
    data = json.load(infile)
1✔
49
    for user_data in data:
1✔
50
        try:
1✔
51
            email = user_data.get("email")
1✔
52

53
            # No email found in user's data, account cannot be created
54
            if not email:
1✔
55
                raise ClickException("Email not defined")
1✔
56

57
            user = datastore.find_user(email=email)
1✔
58

59
            # User already exists, skip account creation
60
            if user:
1✔
61
                raise ClickException(f"User with email {email} already exists")
1✔
62

63
            password = user_data.get(
1✔
64
                "password",
65
                "$pbkdf2-sha512$25000$29ubk1KqFUJorTXmHAPAmA$ooj0RJyHyinmZw"
66
                "/.pNMXne8p70X/BDoX5Ypww24OIguSWEo3y.KT6hiwxwHS5OynZNkgnLvf"
67
                "R3m1mNVfsHgfgA",
68
            )
69
            del user_data["password"]
1✔
70

71
            if not user_data.get("role"):
1✔
72
                user_data["role"] = UserRecord.ROLE_USER
1✔
73

74
            if not datastore.find_role(user_data["role"]):
1✔
75
                datastore.create_role(name=user_data["role"])
×
76
                datastore.commit()
×
77

78
            # Create account and activate it
79
            datastore.create_user(
1✔
80
                email=email, password=password, roles=[user_data["role"]]
81
            )
82
            datastore.commit()
1✔
83
            user = datastore.find_user(email=email)
1✔
84
            confirm_user(user)
1✔
85
            datastore.commit()
1✔
86

87
            click.secho(
1✔
88
                f"User {email} with ID #{user.id} created successfully",
89
                fg="green",
90
            )
91

92
            # Create user resource
93
            user = UserRecord.create(user_data, dbcommit=True)
1✔
94
            user.reindex()
1✔
95

96
        except Exception as error:
1✔
97
            click.secho(
1✔
98
                f"User {user_data} could not be imported: {error}",
99
                fg="red",
100
            )
101

102
    click.secho("Finished", fg="green")
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc