Skip to content
Closed
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
20 changes: 15 additions & 5 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ async def setup_admin_user(db, username, email, password=None):
if not password:
password = os.getenv("KCI_INITIAL_PASSWORD")
if not password:
print("Password is empty and KCI_INITIAL_PASSWORD is not set, aborting.")
print(
"Password is empty and KCI_INITIAL_PASSWORD is not set, aborting."
)
return None
else:
retyped = getpass.getpass(f"Retype password for user '{username}': ")
retyped = getpass.getpass(
f"Retype password for user '{username}': "
)
if password != retyped:
print("Sorry, passwords do not match, aborting.")
return None
Expand Down Expand Up @@ -67,7 +71,9 @@ async def main(args):
db = Database(args.mongo, args.database)
await db.initialize_beanie()
await db.create_indexes()
created = await setup_admin_user(db, args.username, args.email, password=args.password)
created = await setup_admin_user(
db, args.username, args.email, password=args.password
)
return created is not None


Expand All @@ -79,8 +85,12 @@ async def main(args):
help="Mongo server connection string",
)
parser.add_argument("--username", default="admin", help="Admin username")
parser.add_argument("--database", default="kernelci", help="KernelCI database name")
parser.add_argument("--email", required=True, help="Admin user email address")
parser.add_argument(
"--database", default="kernelci", help="KernelCI database name"
)
parser.add_argument(
"--email", required=True, help="Admin user email address"
)
parser.add_argument(
"--password",
default="",
Expand Down
16 changes: 12 additions & 4 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def _translate_operators(self, attributes):
if isinstance(op_value, str) and op_value.isdecimal():
op_value = int(op_value)
if translated_attributes.get(key):
translated_attributes[key].update({op_key: op_value})
translated_attributes[key].update(
{op_key: op_value}
)
else:
translated_attributes[key] = {op_key: op_value}
return translated_attributes
Expand Down Expand Up @@ -251,15 +253,19 @@ async def insert_many(self, model, documents):
result = await col.insert_many(documents)
return result.inserted_ids

async def _create_recursively(self, hierarchy: Hierarchy, parent: Node, cls, col):
async def _create_recursively(
self, hierarchy: Hierarchy, parent: Node, cls, col
):
obj = parse_node_obj(hierarchy.node)
if parent:
obj.parent = parent.id
if obj.id:
obj.update()
if obj.parent == obj.id:
raise ValueError("Parent cannot be the same as the object")
res = await col.replace_one({"_id": ObjectId(obj.id)}, obj.dict(by_alias=True))
res = await col.replace_one(
{"_id": ObjectId(obj.id)}, obj.dict(by_alias=True)
)
if res.matched_count == 0:
raise ValueError(f"No object found with id: {obj.id}")
else:
Expand Down Expand Up @@ -293,7 +299,9 @@ async def update(self, obj):
obj.update()
if obj.parent == obj.id:
raise ValueError("Parent cannot be the same as the object")
res = await col.replace_one({"_id": ObjectId(obj.id)}, obj.dict(by_alias=True))
res = await col.replace_one(
{"_id": ObjectId(obj.id)}, obj.dict(by_alias=True)
)
if res.matched_count == 0:
raise ValueError(f"No object found with id: {obj.id}")
return obj.__class__(**await col.find_one(ObjectId(obj.id)))
Expand Down
16 changes: 12 additions & 4 deletions api/email_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def __init__(self):
def _smtp_connect(self):
"""Method to create a connection with SMTP server"""
if self._settings.smtp_port == 465:
smtp = smtplib.SMTP_SSL(self._settings.smtp_host, self._settings.smtp_port)
smtp = smtplib.SMTP_SSL(
self._settings.smtp_host, self._settings.smtp_port
)
else:
smtp = smtplib.SMTP(self._settings.smtp_host, self._settings.smtp_port)
smtp = smtplib.SMTP(
self._settings.smtp_host, self._settings.smtp_port
)
smtp.starttls()
smtp.login(self._settings.email_sender, self._settings.email_password)
return smtp
Expand Down Expand Up @@ -60,7 +64,11 @@ def _send_email(self, email_msg):
detail="Failed to send email",
) from exc

def create_and_send_email(self, email_subject, email_content, email_recipient):
def create_and_send_email(
self, email_subject, email_content, email_recipient
):
"""Method to create and send email"""
email_msg = self._create_email(email_subject, email_content, email_recipient)
email_msg = self._create_email(
email_subject, email_content, email_recipient
)
self._send_email(email_msg)
Loading
Loading