import logging from email_validator import validate_email, EmailNotValidError from rio import Column, Component, event, Text, TextStyle, TextInput, TextInputChangeEvent, Button from src.ez_lan_manager import ConfigurationService, UserService, MailingService from src.ez_lan_manager.components.AnimatedText import AnimatedText from src.ez_lan_manager.components.MainViewContentBox import MainViewContentBox MINIMUM_PASSWORD_LENGTH = 6 logger = logging.getLogger(__name__.split(".")[-1]) class RegisterPage(Component): def on_pw_focus_loss(self, _: TextInputChangeEvent) -> None: if not (self.pw_1.text == self.pw_2.text) or len(self.pw_1.text) < MINIMUM_PASSWORD_LENGTH: self.pw_1.is_valid = False self.pw_2.is_valid = False return self.pw_1.is_valid = True self.pw_2.is_valid = True def on_email_focus_loss(self, change_event: TextInputChangeEvent) -> None: try: validate_email(change_event.text, check_deliverability=False) self.email_input.is_valid = True except EmailNotValidError: self.email_input.is_valid = False def on_user_name_focus_loss(self, _: TextInputChangeEvent) -> None: current_text = self.user_name_input.text if len(current_text) > UserService.MAX_USERNAME_LENGTH: self.user_name_input.text = current_text[:UserService.MAX_USERNAME_LENGTH] async def on_submit_button_pressed(self) -> None: self.submit_button.is_loading = True self.submit_button.force_refresh() if len(self.user_name_input.text) < 1: await self.animated_text.display_text(False, "Nutzername darf nicht leer sein!") self.submit_button.is_loading = False return if not (self.pw_1.text == self.pw_2.text): await self.animated_text.display_text(False, "Passwörter stimmen nicht überein!") self.submit_button.is_loading = False return if len(self.pw_1.text) < MINIMUM_PASSWORD_LENGTH: await self.animated_text.display_text(False, f"Passwort muss mindestens {MINIMUM_PASSWORD_LENGTH} Zeichen lang sein!") self.submit_button.is_loading = False return if not self.email_input.is_valid or len(self.email_input.text) < 3: await self.animated_text.display_text(False, "E-Mail Adresse ungültig!") self.submit_button.is_loading = False return user_service = self.session[UserService] mailing_service = self.session[MailingService] lan_info = self.session[ConfigurationService].get_lan_info() if await user_service.get_user(self.email_input.text) is not None or await user_service.get_user(self.user_name_input.text) is not None: await self.animated_text.display_text(False, "Benutzername oder E-Mail bereits regestriert!") self.submit_button.is_loading = False return try: new_user = await user_service.create_user(self.user_name_input.text, self.email_input.text, self.pw_1.text) if not new_user: logger.warning(f"UserService.create_user returned: {new_user}") # ToDo: Seems like the user is created fine, even if not returned #FixMe except Exception as e: logger.error(f"Unknown error during new user registration: {e}") await self.animated_text.display_text(False, "Es ist ein unbekannter Fehler aufgetreten :(") self.submit_button.is_loading = False return await mailing_service.send_email( subject="Erfolgreiche Registrierung", body=f"Hallo {self.user_name_input.text},\n\n" f"Du hast dich erfolgreich beim EZ-LAN Manager für {lan_info.name} {lan_info.iteration} registriert.\n\n" f"Wenn du dich nicht registriert hast, kontaktiere bitte unser Team über unsere Homepage.\n\n" f"Liebe Grüße\nDein {lan_info.name} - Team", receiver=self.email_input.text ) self.submit_button.is_loading = False await self.animated_text.display_text(True, "Erfolgreich registriert!") @event.on_populate async def on_populate(self) -> None: await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Registrieren") def build(self) -> Component: self.user_name_input = TextInput( label="Benutzername", text="", margin_left=1, margin_right=1, margin_bottom=1, grow_x=True, on_lose_focus=self.on_user_name_focus_loss ) self.email_input = TextInput( label="E-Mail Adresse", text="", margin_left=1, margin_right=1, margin_bottom=1, grow_x=True, on_lose_focus=self.on_email_focus_loss ) self.pw_1 = TextInput( label="Passwort", text="", margin_left=1, margin_right=1, margin_bottom=1, grow_x=True, is_secret=True, on_lose_focus=self.on_pw_focus_loss ) self.pw_2 = TextInput( label="Passwort wiederholen", text="", margin_left=1, margin_right=1, margin_bottom=1, grow_x=True, is_secret=True, on_lose_focus=self.on_pw_focus_loss ) self.submit_button = Button( content=Text( "Registrieren", style=TextStyle(fill=self.session.theme.background_color, font_size=0.9), align_x=0.5 ), grow_x=True, margin_top=2, margin_left=1, margin_right=1, margin_bottom=1, shape="rectangle", style="minor", color=self.session.theme.secondary_color, on_press=self.on_submit_button_pressed ) self.animated_text = AnimatedText( margin_top=2, margin_left=1, margin_right=1, margin_bottom=2 ) return Column( MainViewContentBox( content=Column( Text( "Neues Konto anlegen", style=TextStyle( fill=self.session.theme.background_color, font_size=1.2 ), margin_top=2, margin_bottom=2, align_x=0.5 ), self.user_name_input, self.email_input, self.pw_1, self.pw_2, self.submit_button, self.animated_text ) ), align_y=0, )