add login_diagram
This commit is contained in:
119
docs/render_diagrams_md.py
Normal file
119
docs/render_diagrams_md.py
Normal file
@@ -0,0 +1,119 @@
|
||||
# Render markdown template for nKode enrollment
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||
from secrets import choice
|
||||
from string import ascii_lowercase
|
||||
from src.models import NKodePolicy, KeypadSize
|
||||
from src.nkode_api import NKodeAPI
|
||||
from src.utils import select_keys_with_passcode_values
|
||||
|
||||
|
||||
def render_markdown_template(template_path, output_path, context):
|
||||
"""
|
||||
Render a Jinja2 markdown template with the given context and save to output_path.
|
||||
|
||||
Args:
|
||||
template_path (Path): Path to the template file
|
||||
output_path (Path): Path where the rendered file will be saved
|
||||
context (dict): Template variables
|
||||
"""
|
||||
template_dir = template_path.parent
|
||||
template_file = template_path.name
|
||||
|
||||
env = Environment(
|
||||
loader=FileSystemLoader(template_dir),
|
||||
autoescape=select_autoescape(['html', 'xml']),
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True
|
||||
)
|
||||
|
||||
template = env.get_template(template_file)
|
||||
rendered = template.render(**context)
|
||||
|
||||
with open(output_path, 'w') as f:
|
||||
f.write(rendered)
|
||||
|
||||
print(f"Template rendered to {output_path}")
|
||||
|
||||
|
||||
def display_icons(icons_array: np.ndarray, kp: KeypadSize) -> str:
|
||||
icons = "["
|
||||
for row in icons_array.reshape(-1, kp.numb_of_keys):
|
||||
icons += ",".join(row)
|
||||
icons += "<br/>"
|
||||
icons = icons[:-5]
|
||||
icons += "]"
|
||||
return icons
|
||||
|
||||
def display_icons_keypad(icons_array: np.ndarray, props_per_key: int) -> str:
|
||||
icons = ""
|
||||
for idx, row in enumerate(icons_array.reshape(-1, props_per_key)):
|
||||
icons += f"Key {idx}: "
|
||||
icons += str(row)
|
||||
icons += "<br/>"
|
||||
return icons
|
||||
|
||||
if __name__ == "__main__":
|
||||
api = NKodeAPI()
|
||||
user_icons = np.array([
|
||||
"😀", "😂", "🥳", "😍", "🤓",
|
||||
"😎", "🥺", "😡", "😱", "🤯",
|
||||
"🥰", "😴", "🤔", "🙃", "😇",
|
||||
"🤖", "👽", "👾", "🐱", "🐶",
|
||||
"🦁", "🐻", "🐸", "🐙", "🦄",
|
||||
"🌟", "⚡", "🔥", "🍕", "🎉"
|
||||
])
|
||||
policy = NKodePolicy(
|
||||
max_nkode_len=10,
|
||||
min_nkode_len=4,
|
||||
distinct_positions=0,
|
||||
distinct_properties=4,
|
||||
)
|
||||
keypad_size = KeypadSize(
|
||||
numb_of_keys=5,
|
||||
props_per_key=6
|
||||
)
|
||||
customer_id = api.create_new_customer(keypad_size, policy)
|
||||
signup_session_id, set_signup_keypad = api.generate_signup_keypad(customer_id)
|
||||
ordered_set_icons = user_icons[set_signup_keypad]
|
||||
username = "test_username" + "".join([choice(ascii_lowercase) for _ in range(6)])
|
||||
passcode_len = 4
|
||||
passcode_property_indices = np.random.choice(set_signup_keypad.reshape(-1), size=passcode_len,
|
||||
replace=False).tolist()
|
||||
selected_keys_set = select_keys_with_passcode_values(passcode_property_indices, set_signup_keypad,
|
||||
keypad_size.numb_of_keys)
|
||||
confirm_keypad = api.set_nkode(username, customer_id, selected_keys_set, signup_session_id)
|
||||
selected_keys_confirm = select_keys_with_passcode_values(passcode_property_indices, confirm_keypad,
|
||||
keypad_size.numb_of_keys)
|
||||
ordered_confirm_icons = user_icons[confirm_keypad]
|
||||
print(f"Selected Keys\n{selected_keys_confirm}")
|
||||
success = api.confirm_nkode(username, customer_id, selected_keys_confirm, signup_session_id)
|
||||
context = {
|
||||
"email": "user@example.com",
|
||||
"signup_session_id": signup_session_id,
|
||||
"set_keypad": display_icons_keypad(set_signup_keypad.reshape(-1, keypad_size.numb_of_keys), keypad_size.numb_of_keys),
|
||||
"keypad_icons": display_icons(user_icons, keypad_size),
|
||||
"ordered_keypad": display_icons_keypad(ordered_set_icons, keypad_size.numb_of_keys),
|
||||
"passcode_user_icons": str(user_icons[passcode_property_indices]),
|
||||
"selected_keys_set": str(selected_keys_set),
|
||||
"confirm_keypad": display_icons_keypad(confirm_keypad.reshape(-1, keypad_size.numb_of_keys), keypad_size.numb_of_keys),
|
||||
"confirm_ordered_keypad": display_icons_keypad(ordered_confirm_icons.reshape(-1, keypad_size.numb_of_keys), keypad_size.numb_of_keys),
|
||||
"confirm_key_selection": selected_keys_confirm
|
||||
}
|
||||
render_markdown_template(Path("./enrollment_diagram.template.md"), Path("./enrollment_diagram.md"), context)
|
||||
login_keypad = api.get_login_keypad(username, customer_id)
|
||||
selected_keys_login = select_keys_with_passcode_values(passcode_property_indices, login_keypad,
|
||||
keypad_size.props_per_key)
|
||||
ordered_login_keypad = user_icons[login_keypad]
|
||||
context = {
|
||||
"email": "user@example.com",
|
||||
"keypad_icons": display_icons(user_icons, keypad_size),
|
||||
"login_keypad": display_icons_keypad(login_keypad.reshape(-1, keypad_size.props_per_key), keypad_size.props_per_key),
|
||||
"ordered_login_icons": display_icons_keypad(ordered_login_keypad.reshape(-1, keypad_size.props_per_key),keypad_size.props_per_key),
|
||||
"passcode_user_icons": str(user_icons[passcode_property_indices]),
|
||||
"selected_keys_login": str(selected_keys_login)
|
||||
}
|
||||
api.login(customer_id, username, selected_keys_login)
|
||||
render_markdown_template(Path("./login_diagram.template.md"), Path("./login_diagram.md"), context)
|
||||
Reference in New Issue
Block a user