def share(url_id): form = ShareForm() if request.method == "POST" and form.validate_on_submit(): object_list = User.objects(user_name=form.user_name.data) if len(object_list) == 0: flash("The user name does not exist.", "danger") return redirect(url_for("dashboard")) if len(object_list) == 1 and object_list[0].user_name == User.objects( id=current_user.id).first().user_name: flash("Enter a username that is not the same as yours :P", "danger") return redirect(url_for("dashboard")) if url_id in User.objects( user_name=form.user_name.data).first().shared_url_list: flash( f"Shortened URL information has already been shared with the {form.user_name.data}", "warning") return redirect(url_for("dashboard")) object_list.first().update(push__shared_url_list=url_id) flash(f"Shared shortened URL information with {form.user_name.data}", "success") return redirect(url_for("dashboard"))
def account(): form = AccountForm() user = User.objects(id=current_user.id).first() if request.method == "POST" and form.validate_on_submit(): changes = [] if form.first_name.data: user.update(set__first_name=form.first_name.data) changes.append("First Name") if form.last_name.data: user.update(set__last_name=form.last_name.data) changes.append("Last Name") if form.password.data and bcrypt.check_password_hash( user.password, form.password.data ) and form.new_password.data == form.confirm_new_password.data: hashed_password = bcrypt.generate_password_hash( form.new_password.data).decode('utf-8') user.update(password=hashed_password) changes.append("Password") change_message = "Updated: " + ", ".join(changes) flash(change_message, "info") return redirect(url_for("account")) return render_template("account.html", title="Account", form=form, user=user)
def login(): # If the user is already logged in and # hence has user id stored in the session, # then we redirect this individual back to # index page if current_user.is_authenticated: return redirect(url_for("dashboard")) # create oboject of the WTForm to be used # by end users to add in the original and # the (optional) shortened URL form = LoginForm() if request.method == "POST" and form.validate_on_submit(): # Acquire user object based on form data user = User.objects(user_name=form.user_name.data) if len(user) == 1 and bcrypt.check_password_hash( user.first().password, form.password.data): # if information matches, log the user in login_user(user=user.first()) flash("You were successfully logged in!", "success") # If the login is a result of a redirection due to # end user trying to access a route with login required, # we redirect the user to that initial route if request.args.get('next'): return redirect(request.args.get('next')) return redirect(url_for("index")) flash("Login unsuccessful", "danger") return render_template("login.html", title="Login", form=form)
def dashboard(): # TODO: Acquire information for all the # links belonging to the end user and display # these stats form = ShareForm() url_list = URL.objects(owner=current_user.id) shared_id_list = User.objects(id=current_user.id).first().shared_url_list shared_list = [] for url_id in shared_id_list: shared_list.append(URL.objects(id=url_id).first()) return render_template("dashboard.html", title="Dashboard", form=form, url_list=url_list, shared_list=shared_list)
import sys sys.path.insert(0, 'E:\Projects\Short-It') from datetime import datetime from json import loads, dumps from short_it.models import URL, User from short_it import db url_test = URL.objects(shortened="test1") if len(url_test) == 1 and url_test[0].shortened == "test1": url_test[0].update(inc__counter=1) url_test[0].update(push__date_array=datetime.utcnow) print(url_test[0].counter) print(url_test[0].date_array) user_nisarg42 = User.objects(user_name="nisarg42") print(user_nisarg42)
def validate_email(self, email): object_list = User.objects(email=email.data) if len(object_list) > 0: raise ValidationError("The email is already taken.")
def validate_user_name(self, user_name): object_list = User.objects(user_name=user_name.data) if len(object_list) > 0: raise ValidationError("The user name is already taken.")