def wrapper(*args, **kwargs): if request.authorization: ucontr = UserController() try: user = ucontr.get(nickname=request.authorization.username) except NotFound: raise Forbidden("Couldn't authenticate your user") if not ucontr.check_password(user, request.authorization.password): raise Forbidden("Couldn't authenticate your user") if not user.is_active: raise Forbidden("User is deactivated") login_user_bundle(user) if current_user.is_authenticated: return func(*args, **kwargs) raise Unauthorized()
def user_stream(per_page, nickname=None): """ Display the stream of a user (list of articles of public feed). """ user_contr = UserController() user = user_contr.get(nickname=nickname) if not user.is_public_profile: if current_user.is_authenticated and current_user.id == user.id: flash(gettext("You must set your profile to public."), "info") return redirect(url_for("user.profile")) category_id = int(request.args.get("category_id", 0)) category = CategoryController().read(id=category_id).first() # Load the public feeds filters = {} filters["private"] = False if category_id: filters["category_id"] = category_id feeds = FeedController().read(**filters).all() # Re-initializes the filters to load the articles filters = {} filters["feed_id__in"] = [feed.id for feed in feeds] if category: filters["category_id"] = category_id articles = ArticleController(user.id).read_ordered(**filters) # Server-side pagination page, per_page, offset = get_page_args(per_page_parameter="per_page") pagination = Pagination( page=page, total=articles.count(), css_framework="bootstrap4", search=False, record_name="articles", per_page=per_page, ) return render_template( "user_stream.html", user=user, articles=articles.offset(offset).limit(per_page), category=category, pagination=pagination, )
def validate(self): validated = super().validate() ucontr = UserController() try: user = ucontr.get(nickname=self.nickmane.data) except NotFound: self.nickmane.errors.append("Wrong nickname") validated = False else: if not user.is_active: self.nickmane.errors.append("Account not active") validated = False if not ucontr.check_password(user, self.password.data): self.password.errors.append("Wrong password") validated = False self.user = user return validated
def profile(): """ Edit the profile of the currently logged user. """ user_contr = UserController(current_user.id) user = user_contr.get(id=current_user.id) form = ProfileForm() if request.method == "POST": if form.validate(): try: user_contr.update( {"id": current_user.id}, { "nickname": form.nickname.data, "password": form.password.data, "automatic_crawling": form.automatic_crawling.data, "is_public_profile": form.is_public_profile.data, "bio": form.bio.data, "webpage": form.webpage.data, "twitter": form.twitter.data, }, ) except Exception as error: flash( gettext( "Problem while updating your profile: " "%(error)s", error=error), "danger", ) else: flash( gettext("User %(nick)s successfully updated", nick=user.nickname), "success", ) return redirect(url_for("user.profile")) else: return render_template("profile.html", user=user, form=form) if request.method == "GET": form = ProfileForm(obj=user) return render_template("profile.html", user=user, form=form)
def process_user_form(user_id=None): """ Create or edit a user. """ form = UserForm() user_contr = UserController() if not form.validate(): return render_template( "/admin/create_user.html", form=form, message=gettext("Some errors were found"), ) if user_id is not None: # Edit a user user_contr.update( {"id": user_id}, { "nickname": form.nickname.data, "password": form.password.data, "automatic_crawling": form.automatic_crawling.data, }, ) user = user_contr.get(id=user_id) flash( gettext("User %(nick)s successfully updated", nick=user.nickname), "success") else: # Create a new user (by the admin) user = user_contr.create( nickname=form.nickname.data, password=form.password.data, automatic_crawling=form.automatic_crawling.data, is_admin=False, is_active=True, ) flash( gettext("User %(nick)s successfully created", nick=user.nickname), "success") return redirect(url_for("admin.user_form", user_id=user.id))
def profile_public(nickname=None): """ Display the public profile of the user. """ category_id = int(request.args.get("category_id", 0)) user_contr = UserController() user = user_contr.get(nickname=nickname) if not user.is_public_profile: if current_user.is_authenticated and current_user.id == user.id: flash(gettext("You must set your profile to public."), "info") return redirect(url_for("user.profile")) filters = {} filters["private"] = False if category_id: filters["category_id"] = category_id feeds = FeedController(user.id).read(**filters) return render_template("profile_public.html", user=user, feeds=feeds, selected_category_id=category_id)
def toggle_user(user_id=None): """ Enable or disable the account of a user. """ ucontr = UserController() user = ucontr.get(id=user_id) user_changed = ucontr.update({"id": user_id}, {"is_active": not user.is_active}) if not user_changed: flash(gettext("This user does not exist."), "danger") return redirect(url_for("admin.dashboard")) else: act_txt = "activated" if user.is_active else "desactivated" message = gettext( "User %(nickname)s successfully %(is_active)s", nickname=user.nickname, is_active=act_txt, ) flash(message, "success") return redirect(url_for("admin.dashboard"))