1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 06:46:15 +01:00

Merge pull request #1567 from dmach/person-register-search

Add 'person register' and 'person search' commands
This commit is contained in:
Daniel Mach 2024-05-21 13:49:45 +02:00 committed by GitHub
commit a010e99c34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 177 additions and 0 deletions

12
osc/commands/person.py Normal file
View File

@ -0,0 +1,12 @@
import osc.commandline
class PersonCommand(osc.commandline.OscCommand):
"""
Manage persons
"""
name = "person"
def run(self, args):
pass

View File

@ -0,0 +1,58 @@
import osc.commandline
class PersonRegisterCommand(osc.commandline.OscCommand):
"""
Register a new person (user)
"""
name = "register"
parent = "PersonCommand"
def init_arguments(self):
self.add_argument(
"--login",
required=True,
help="Login.",
)
self.add_argument(
"--realname",
required=True,
help="Real name of the person.",
)
self.add_argument(
"--email",
required=True,
help="Email address.",
)
self.add_argument(
"--password",
help="Password. An interactive prompt is shown if password is not specified.",
)
self.add_argument(
"--note",
help="Any notes about the person.",
)
self.add_argument(
"--state",
help="State of the account. Defaults to 'unconfirmed'.",
)
def run(self, args):
from osc import obs_api
from osc.util.helper import raw_input
if args.password:
password = args.password
else:
password = raw_input(f"Enter password for {args.login}@{args.apiurl}: ")
obs_api.Person.cmd_register(
args.apiurl,
login=args.login,
realname=args.realname,
email=args.email,
password=password,
note=args.note,
state=args.state,
)

View File

@ -0,0 +1,51 @@
import osc.commandline
class PersonSearchCommand(osc.commandline.OscCommand):
"""
Search a person (user)
"""
name = "search"
parent = "PersonCommand"
def init_arguments(self):
self.add_argument(
"--login",
help="Search by a login.",
)
self.add_argument(
"--login-contains",
metavar="SUBSTR",
help="Search by a substring in a login.",
)
self.add_argument(
"--realname-contains",
metavar="SUBSTR",
help="Search by a substring in a realname.",
)
self.add_argument(
"--email",
help="Search by an email address.",
)
self.add_argument(
"--email-contains",
metavar="SUBSTR",
help="Search by a substring in an email address.",
)
def run(self, args):
from .. import obs_api
persons = obs_api.Person.search(
args.apiurl,
login=args.login,
login__contains=args.login_contains,
realname__contains=args.realname_contains,
email=args.email,
email__contains=args.email_contains,
)
for person in persons:
print(person.to_human_readable_string())
print()

View File

@ -2,6 +2,7 @@ from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-i
from .enums import BoolString
from .person_owner import PersonOwner
from .person_watchlist import PersonWatchlist
from .status import Status
class Person(XmlModel):
@ -32,6 +33,19 @@ class Person(XmlModel):
ignore_auth_services: Optional[BoolString] = Field(
)
def to_human_readable_string(self) -> str:
"""
Render the object as a human readable string.
"""
from ..output import KeyValueTable
table = KeyValueTable()
table.add("Login", self.login, color="bold")
table.add("Real name", self.realname)
table.add("Email", self.email)
table.add("State", self.state)
return f"{table}"
@classmethod
def from_api(cls, apiurl: str, username: str):
url_path = ["person", username]
@ -69,3 +83,45 @@ class Person(XmlModel):
for node in root:
result.append(cls.from_xml(node, apiurl=apiurl))
return result
@classmethod
def cmd_register(
cls,
apiurl: str,
*,
login: str,
realname: str,
email: str,
password: str,
note: Optional[str] = None,
state: Optional[str] = "confirmed",
):
person = UnregisteredPerson(login=login, realname=realname, email=email, password=password, note=note, state=state)
url_path = ["person"]
url_query = {
"cmd": "register",
}
response = cls.xml_request("POST", apiurl, url_path, url_query, data=person.to_string())
return Status.from_file(response, apiurl=apiurl)
class UnregisteredPerson(XmlModel):
XML_TAG = "unregisteredperson"
login: str = Field(
)
realname: str = Field(
)
email: str = Field(
)
password: str = Field(
)
note: Optional[str] = Field(
)
state: Optional[str] = Field(
)