Example #1
0
def login() -> str:
    if CurrentUser().authenticated():
        return PageRedirect(PageUrlFor("home")).link()
    form: FlaskForm = LoginForm()
    if form.validate_on_submit():
        user: User = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            CurrentUser().login(user, form)
            next_page: str = PageRequest("next").get()
            return PageRedirect(
                PageUrlFor(next_page if next_page else "home")).link()
        else:
            PageFlash("Login Unsuccessful. Please check email and password",
                      "danger").display()
    return YFoxTemplate("login.html").render(title="Login", form=form)
Example #2
0
def delete_post(post_id) -> str:
    _post = Post.query.get_or_404(post_id)
    if _post.author != CurrentUser().get_user:
        AbortPage(_forbidden).perform()
    UserSession(db).delete(_post)
    return InformPage("Your post has been deleted!", "success",
                      "home").perform()
Example #3
0
def new_post() -> str:
    form = PostForm()
    if form.validate_on_submit():
        _post = Post(title=form.title.data,
                     content=form.content.data,
                     author=CurrentUser().get_user)
        UserSession(db).add(_post)
        form.success()
    return YFoxTemplate("create_post.html").render(title="New Post",
                                                   form=form,
                                                   legend="New Post")
Example #4
0
def register() -> str:
    if CurrentUser().authenticated():
        return PageRedirect(PageUrlFor("home")).link()
    form: FlaskForm = RegistrationForm()
    if form.validate_on_submit():
        hashed_pass: str = bcrypt.generate_password_hash(
            form.password.data).decode("utf-8")
        user: User = User(username=form.username.data,
                          email=form.email.data,
                          password=hashed_pass)
        UserSession(db).add(user)
        InformPage(
            f"Your account has been created! You are now able to login with {user.username} username",
            "success",
            "login",
        ).perform()
    return YFoxTemplate("register.html").render(title="Register", form=form)
Example #5
0
def account() -> str:
    form: FlaskForm = UpdateAccountForm()
    user: users.User = CurrentUser()
    if form.validate_on_submit():
        if form.picture.data:
            image = UpdateImage(form, yfox)
            user.image_file = image.perform()
        user.username = form.username.data
        user.email = form.email.data
        UserSession(db).add(user)
        form.success()
    elif PageRequest().method() == _GET:
        form.username.data = user.username
        form.email.data = user.email
    image_file: Callable = PageUrlFor("static",
                                      filename=f"accounts/{user.image_file}")
    return YFoxTemplate("account.html").render(title="Account",
                                               image_file=image_file(),
                                               form=form)
Example #6
0
def update_post(post_id) -> str:
    _post = Post.query.get_or_404(post_id)
    form = PostForm()
    if _post.author != CurrentUser().get_user:
        AbortPage(_forbidden).perform()
    if form.validate_on_submit():
        _post.title = form.title.data
        _post.content = form.content.data
        UserSession(db).add(_post)
        InformPage("Your post has been updated!",
                   "success",
                   "post",
                   post_id=_post.id).perform()
    elif PageRequest().method() == _GET:
        form.title.data = _post.title
        form.content.data = _post.content
    return YFoxTemplate("create_post.html").render(title="Update Post",
                                                   form=form,
                                                   legend="Update Post")
Example #7
0
class UpdateAccountForm(FlaskForm, GenericForm):
    """Represent registration page."""

    username: Field = StringField(
        "Username", [DataRequired(), Length(min=2, max=20)])
    submit: Field = SubmitField("Update")
    picture = FileField("Updated Profile Picture",
                        validators=[FileAllowed(["jpg", "png"])])
    validation: Validation = ValidationField(User.query)
    user: users.User = CurrentUser()
    inform: Inform = InformPage("Your account has been updated!", "success",
                                "account")

    def success(self) -> str:
        return self.inform.perform()

    def validate_username(self, username: User) -> None:
        if username.data != self.user.username:
            self.validation.validate_username(username)

    def validate_email(self, email: User) -> None:
        if email.data != self.user.email:
            self.validation.validate_email(email)
Example #8
0
def logout() -> str:
    CurrentUser().logout()
    return PageRedirect(PageUrlFor("home")).link()