refactor user profile edit and add it to the user management system
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
from datetime import date, datetime
|
||||
from hashlib import sha256
|
||||
from typing import Optional
|
||||
|
||||
from from_root import from_root
|
||||
from rio import Column, Component, event, Text, TextStyle, Button, Color, Row, TextInput, Image, TextInputChangeEvent, NoFileSelectedError, \
|
||||
ProgressCircle
|
||||
from email_validator import validate_email, EmailNotValidError
|
||||
from rio import Column, Component, event, Spacer
|
||||
|
||||
from src.ez_lan_manager import ConfigurationService, UserService
|
||||
from src.ez_lan_manager.components.AnimatedText import AnimatedText
|
||||
from src.ez_lan_manager.components.MainViewContentBox import MainViewContentBox
|
||||
from src.ez_lan_manager.components.UserEditForm import UserEditForm
|
||||
from src.ez_lan_manager.types.SessionStorage import SessionStorage
|
||||
from src.ez_lan_manager.types.User import User
|
||||
|
||||
@@ -18,210 +13,14 @@ class EditProfilePage(Component):
|
||||
user: Optional[User] = None
|
||||
pfp: Optional[bytes] = None
|
||||
|
||||
@staticmethod
|
||||
def optional_date_to_str(d: Optional[date]) -> str:
|
||||
if not d:
|
||||
return ""
|
||||
return d.strftime("%d.%m.%Y")
|
||||
|
||||
@event.on_populate
|
||||
async def on_populate(self) -> None:
|
||||
await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Profil bearbeiten")
|
||||
self.user = await self.session[UserService].get_user(self.session[SessionStorage].user_id)
|
||||
self.pfp = await self.session[UserService].get_profile_picture(self.user.user_id)
|
||||
|
||||
def on_email_changed(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_birthday_changed(self, change_event: TextInputChangeEvent) -> None:
|
||||
if len(change_event.text) == 0:
|
||||
self.birthday_input.is_valid = True
|
||||
return
|
||||
try:
|
||||
day, month, year = change_event.text.split(".")
|
||||
year = int(year)
|
||||
if year < 1900 or year > datetime.now().year - 12:
|
||||
raise ValueError
|
||||
date(day=int(day), month=int(month), year=year)
|
||||
self.birthday_input.is_valid = True
|
||||
except (ValueError, TypeError, IndexError):
|
||||
self.birthday_input.is_valid = False
|
||||
|
||||
async def upload_new_pfp(self) -> None:
|
||||
try:
|
||||
new_pfp = await self.session.file_chooser(file_extensions=("png", "jpg", "jpeg"), multiple=False)
|
||||
except NoFileSelectedError:
|
||||
await self.animated_text.display_text(False, "Keine Datei ausgewählt!")
|
||||
return
|
||||
|
||||
if new_pfp.size_in_bytes > 2 * 1_000_000:
|
||||
await self.animated_text.display_text(False, "Bild zu groß! (> 2MB)")
|
||||
return
|
||||
|
||||
image_data = await new_pfp.read_bytes()
|
||||
await self.session[UserService].set_profile_picture(self.session[SessionStorage].user_id, image_data)
|
||||
self.pfp_image_container.image = image_data
|
||||
await self.animated_text.display_text(True, "Gespeichert!")
|
||||
|
||||
async def on_save_pressed(self) -> None:
|
||||
if not all((self.email_input.is_valid, self.birthday_input.is_valid)):
|
||||
await self.animated_text.display_text(False, "Ungültige Werte!")
|
||||
return
|
||||
|
||||
if len(self.new_pw_1_input.text.strip()) > 0:
|
||||
if self.new_pw_1_input.text.strip() != self.new_pw_2_input.text.strip():
|
||||
await self.animated_text.display_text(False, "Passwörter nicht gleich!")
|
||||
return
|
||||
|
||||
user: User = await self.session[UserService].get_user(self.session[SessionStorage].user_id)
|
||||
user.user_mail = self.email_input.text
|
||||
|
||||
if len(self.birthday_input.text) == 0:
|
||||
user.user_birth_day = None
|
||||
else:
|
||||
day, month, year = self.birthday_input.text.split(".")
|
||||
user.user_birth_day = date(day=int(day), month=int(month), year=int(year))
|
||||
|
||||
user.user_first_name = self.first_name_input.text
|
||||
user.user_last_name = self.last_name_input.text
|
||||
if len(self.new_pw_1_input.text.strip()) > 0:
|
||||
user.user_password = sha256(self.new_pw_1_input.text.encode(encoding="utf-8")).hexdigest()
|
||||
|
||||
await self.session[UserService].update_user(user)
|
||||
await self.animated_text.display_text(True, "Gespeichert!")
|
||||
|
||||
def build(self) -> Component:
|
||||
if not self.user:
|
||||
return Column(
|
||||
MainViewContentBox(
|
||||
ProgressCircle(
|
||||
color="secondary",
|
||||
align_x=0.5,
|
||||
margin_top=2,
|
||||
margin_bottom=2
|
||||
)
|
||||
),
|
||||
align_y=0
|
||||
)
|
||||
|
||||
self.animated_text = AnimatedText(
|
||||
margin_top=2,
|
||||
margin_bottom=1,
|
||||
align_x=0.1
|
||||
)
|
||||
|
||||
self.email_input = TextInput(
|
||||
label="E-Mail Adresse",
|
||||
text=self.user.user_mail,
|
||||
margin_left=1,
|
||||
margin_right=1,
|
||||
margin_bottom=1,
|
||||
grow_x=True,
|
||||
on_change=self.on_email_changed
|
||||
)
|
||||
self.first_name_input = TextInput(
|
||||
label="Vorname",
|
||||
text=self.user.user_first_name,
|
||||
margin_left=1,
|
||||
margin_right=1,
|
||||
grow_x=True
|
||||
)
|
||||
self.last_name_input = TextInput(
|
||||
label="Nachname",
|
||||
text=self.user.user_last_name,
|
||||
margin_right=1,
|
||||
grow_x=True
|
||||
)
|
||||
self.birthday_input = TextInput(
|
||||
label="Geburtstag (TT.MM.JJJJ)",
|
||||
text=self.optional_date_to_str(self.user.user_birth_day),
|
||||
margin_left=1,
|
||||
margin_right=1,
|
||||
margin_bottom=1,
|
||||
grow_x=True,
|
||||
on_change=self.on_birthday_changed
|
||||
)
|
||||
self.new_pw_1_input = TextInput(
|
||||
label="Neues Passwort setzen",
|
||||
text="",
|
||||
margin_left=1,
|
||||
margin_right=1,
|
||||
margin_bottom=1,
|
||||
grow_x=True,
|
||||
is_secret=True
|
||||
)
|
||||
self.new_pw_2_input = TextInput(
|
||||
label="Neues Passwort wiederholen",
|
||||
text="",
|
||||
margin_left=1,
|
||||
margin_right=1,
|
||||
margin_bottom=1,
|
||||
grow_x=True,
|
||||
is_secret=True
|
||||
)
|
||||
|
||||
self.pfp_image_container = Image(
|
||||
from_root("src/ez_lan_manager/assets/img/anon_pfp.png") if self.pfp is None else self.pfp,
|
||||
align_x=0.5,
|
||||
min_width=10,
|
||||
min_height=10,
|
||||
margin_top=1,
|
||||
margin_bottom=1
|
||||
)
|
||||
|
||||
return Column(
|
||||
MainViewContentBox(
|
||||
content=Column(
|
||||
self.pfp_image_container,
|
||||
Button(
|
||||
content=Text(
|
||||
"Neues Bild hochladen",
|
||||
style=TextStyle(fill=Color.from_hex("02dac5"), font_size=0.9)
|
||||
),
|
||||
align_x=0.5,
|
||||
margin_bottom=1,
|
||||
shape="rectangle",
|
||||
style="major",
|
||||
color="primary",
|
||||
on_press=self.upload_new_pfp
|
||||
),
|
||||
Row(
|
||||
TextInput(label="Deine User-ID", text=self.user.user_id, is_sensitive=False, margin_left=1, grow_x=False),
|
||||
TextInput(label="Dein Nickname", text=self.user.user_name, is_sensitive=False, margin_left=1, margin_right=1, grow_x=True),
|
||||
margin_bottom=1
|
||||
),
|
||||
self.email_input,
|
||||
Row(
|
||||
self.first_name_input,
|
||||
self.last_name_input,
|
||||
margin_bottom=1
|
||||
),
|
||||
self.birthday_input,
|
||||
self.new_pw_1_input,
|
||||
self.new_pw_2_input,
|
||||
|
||||
Row(
|
||||
self.animated_text,
|
||||
Button(
|
||||
content=Text(
|
||||
"Speichern",
|
||||
style=TextStyle(fill=self.session.theme.success_color, font_size=0.9),
|
||||
align_x=0.2
|
||||
),
|
||||
align_x=0.9,
|
||||
margin_top=2,
|
||||
margin_bottom=1,
|
||||
shape="rectangle",
|
||||
style="major",
|
||||
color="primary",
|
||||
on_press=self.on_save_pressed
|
||||
),
|
||||
)
|
||||
)
|
||||
),
|
||||
align_y=0,
|
||||
MainViewContentBox(UserEditForm(is_own_profile=True)),
|
||||
Spacer(grow_y=True)
|
||||
)
|
||||
|
||||
@@ -4,10 +4,11 @@ from typing import Optional, Coroutine
|
||||
|
||||
import rio
|
||||
from rio import Column, Component, event, TextStyle, Text, TextInput, ThemeContextSwitcher, Grid, \
|
||||
PointerEventListener, PointerEvent, Rectangle, CursorStyle, Color, TextInputChangeEvent
|
||||
PointerEventListener, PointerEvent, Rectangle, CursorStyle, Color, TextInputChangeEvent, Spacer
|
||||
|
||||
from src.ez_lan_manager import ConfigurationService, UserService
|
||||
from src.ez_lan_manager.components.MainViewContentBox import MainViewContentBox
|
||||
from src.ez_lan_manager.components.UserEditForm import UserEditForm
|
||||
from src.ez_lan_manager.types.User import User
|
||||
|
||||
logger = logging.getLogger(__name__.split(".")[-1])
|
||||
@@ -104,24 +105,29 @@ class ManageUsersPage(Component):
|
||||
)
|
||||
),
|
||||
MainViewContentBox(
|
||||
Text(
|
||||
text=f"Nutzer {self.selected_user.user_name} gewählt.",
|
||||
style=TextStyle(
|
||||
fill=self.session.theme.background_color,
|
||||
font_size=1.2
|
||||
),
|
||||
margin_top=2,
|
||||
margin_bottom=2,
|
||||
align_x=0.5
|
||||
) if self.selected_user else Text(
|
||||
text="Bitte Nutzer auswählen...",
|
||||
style=TextStyle(
|
||||
fill=self.session.theme.background_color,
|
||||
font_size=1.2
|
||||
),
|
||||
margin_top=2,
|
||||
margin_bottom=2,
|
||||
align_x=0.5
|
||||
)),
|
||||
Column(
|
||||
Text(
|
||||
text="Allgemeines",
|
||||
style=TextStyle(
|
||||
fill=self.session.theme.background_color,
|
||||
font_size=1.2
|
||||
),
|
||||
margin_top=2,
|
||||
margin_bottom=2,
|
||||
align_x=0.5
|
||||
) if self.selected_user else Spacer(),
|
||||
UserEditForm(
|
||||
is_own_profile=False,
|
||||
user=self.selected_user
|
||||
) if self.selected_user else Text(
|
||||
text="Bitte Nutzer auswählen...",
|
||||
style=TextStyle(
|
||||
fill=self.session.theme.background_color,
|
||||
font_size=1.2
|
||||
),
|
||||
margin_top=2,
|
||||
margin_bottom=2,
|
||||
align_x=0.5
|
||||
))),
|
||||
align_y=0
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user