temporary move
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from snek.system.form import Form, FormButtonElement, FormInputElement, HTMLElement
|
||||
|
||||
|
||||
class AuthField(FormInputElement):
|
||||
|
||||
@property
|
||||
async def errors(self):
|
||||
result = await super().errors
|
||||
if self.model.password.value and self.model.username.value:
|
||||
if not await self.app.services.user.validate_login(
|
||||
self.model.username.value, self.model.password.value
|
||||
):
|
||||
return ["Invalid username or password"]
|
||||
return result
|
||||
|
||||
|
||||
class LoginForm(Form):
|
||||
|
||||
title = HTMLElement(tag="h1", text="Login")
|
||||
|
||||
username = AuthField(
|
||||
name="username",
|
||||
required=True,
|
||||
min_length=2,
|
||||
max_length=20,
|
||||
regex=r"^[a-zA-Z0-9_]+$",
|
||||
place_holder="Username",
|
||||
type="text",
|
||||
)
|
||||
password = AuthField(
|
||||
name="password",
|
||||
required=True,
|
||||
regex=r"^[a-zA-Z0-9_.+-]{6,}",
|
||||
type="password",
|
||||
place_holder="Password",
|
||||
)
|
||||
|
||||
action = FormButtonElement(
|
||||
name="action", value="submit", text="Login", type="button"
|
||||
)
|
||||
|
||||
@property
|
||||
async def is_valid(self):
|
||||
return all(
|
||||
[
|
||||
self["username"],
|
||||
self["password"],
|
||||
not await self.username.errors,
|
||||
not await self.password.errors,
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,44 @@
|
||||
from snek.system.form import Form, FormButtonElement, FormInputElement, HTMLElement
|
||||
|
||||
|
||||
class UsernameField(FormInputElement):
|
||||
|
||||
@property
|
||||
async def errors(self):
|
||||
result = await super().errors
|
||||
if self.value and await self.app.services.user.count(username=self.value):
|
||||
result.append("Username is not available.")
|
||||
return result
|
||||
|
||||
|
||||
class RegisterForm(Form):
|
||||
|
||||
title = HTMLElement(tag="h1", text="Register")
|
||||
|
||||
username = UsernameField(
|
||||
name="username",
|
||||
required=True,
|
||||
min_length=2,
|
||||
max_length=20,
|
||||
regex=r"^[a-zA-Z0-9_]+$",
|
||||
place_holder="Username",
|
||||
type="text",
|
||||
)
|
||||
email = FormInputElement(
|
||||
name="email",
|
||||
required=False,
|
||||
regex=r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
|
||||
place_holder="Email address",
|
||||
type="email",
|
||||
)
|
||||
password = FormInputElement(
|
||||
name="password",
|
||||
required=True,
|
||||
regex=r"^[a-zA-Z0-9_.+-]{6,}",
|
||||
type="password",
|
||||
place_holder="Password",
|
||||
)
|
||||
|
||||
action = FormButtonElement(
|
||||
name="action", value="submit", text="Register", type="button"
|
||||
)
|
||||
Reference in New Issue
Block a user