Example #1
0
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"))
Example #2
0
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)
Example #3
0
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)
Example #4
0
def register():
    form = RegistrationForm()

    if request.method == "POST" and form.validate_on_submit():

        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        new_user = User(
            first_name=form.first_name.data,
            last_name=form.last_name.data,
            user_name=form.user_name.data,
            email=form.email.data,
            password=hashed_password,
        )
        new_user.save()

        flash(
            f"Welcome, {form.user_name.data}! You have been successfully registered as a user of Short It :)",
            "success")
        return redirect(url_for("index"))

    return render_template("register.html", title="Register", form=form)
Example #5
0
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)
Example #6
0
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)
Example #7
0
 def validate_email(self, email):
     object_list = User.objects(email=email.data)
     if len(object_list) > 0:
         raise ValidationError("The email is already taken.")
Example #8
0
 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.")
Example #9
0
import sys
sys.path.insert(0, 'E:\Projects\Short-It')

from short_it import db
from short_it.models import URL, User
from datetime import datetime

test_URL = URL(
    shortened="test1",
    URL="www.test_url.com",
)
test_URL.save()

test_user_one = User(
    user_name="nisarg42",
    email="*****@*****.**",
    first_name="Nisarg0",
    last_name="Shah0",
)
test_user_one.save()

test_user_two = User(
    user_name="nisarg123",
    email="*****@*****.**",
    first_name="Nisarg1",
    last_name="Shah1",
)
test_user_two.save()

for document in URL.objects:
    # print(document.to_json()) # to_json() converts the docuemnt object to a readable json format
    print(document.shortened)