示例#1
0
def profile(username):
    current_user = flask_login.current_user.id
    # Make sure the user actually exists
    profile_user_data = db.get_user_by("name", username)
    current_user_data = db.get_user_by("name", current_user)
    if (profile_user_data):
        return render_template('profile.html',
                               page_name="profile",
                               current_user=current_user,
                               profile_user=username,
                               user_data=profile_user_data,
                               current_user_data=current_user_data)
    else:
        return render_template(
            '404.html', message="User {} doesn't exist!".format(username))
示例#2
0
def feed():
    posts = {}
    current_user = flask_login.current_user.id
    user_data = db.get_user_by("name", current_user)

    [
        posts.update(x["stories"])
        for x in list(db.users.find({}, {
            'stories': 1,
            '_id': 0
        })) if "stories" in x
    ]
    print(set(list(posts.items())[0][1]["tags"]), set(user_data["tags"]))
    posts = {
        name: data
        for name, data in posts.items()
        if len(set(data["tags"]).intersection(set(user_data["tags"])))
    }
    print(posts)
    featured = list(sorted(posts.items(),
                           key=lambda k: len(k[1]["comments"])))[::-1]
    return render_template('feed.html',
                           page_name="feed",
                           featured=featured,
                           user_data=user_data)
示例#3
0
def comment():
    name = flask_login.current_user.id
    data = request.json
    title = data["title"]
    author = data["author"]
    db.comment(author, name, title, data["comment"])
    print(db.get_user_by("name", author))
    return jsonify({"user": name, "comment": data["comment"]})
示例#4
0
def request_loader(request):
    if request.form:
        username = request.form["username"]
        if not db.verify_user(username, request.form["password"]):
            # Authenticated failed
            return
        u = db.get_user_by("name", username)
        user = User()
        user.id = u["name"]
        return user
示例#5
0
def friend():
    current_user = flask_login.current_user.id
    user_data = db.get_user_by("name", current_user)
    friends = [
        x for x in list(db.users.find({})) if "tags" in x
        and len(set(x["tags"]).intersection(set(user_data["tags"])))
        and x["name"] != current_user
    ]
    return render_template('friend.html',
                           page_name="friend",
                           friends=friends,
                           user_data=user_data)
示例#6
0
def index():
    current_user = flask_login.current_user
    # If the user isn't logged in the id isn't defined
    try:
        test = current_user.id
        user = db.get_user_by("name", test)
        return render_template('profile.html',
                               page_name="profile",
                               current_user=test,
                               profile_user=test,
                               user_data=user)
    except:
        current_user = None
    return render_template('index.html',
                           page_name="index",
                           current_user=current_user)
示例#7
0
def discover():
    posts = {}
    current_user = flask_login.current_user.id
    user_data = db.get_user_by("name", current_user)

    [
        posts.update(x["stories"])
        for x in list(db.users.find({}, {
            'stories': 1,
            '_id': 0
        })) if "stories" in x
    ]
    featured = list(sorted(posts.items(),
                           key=lambda k: len(k[1]["comments"])))[::-1]
    return render_template('discover.html',
                           page_name="discover",
                           featured=featured,
                           user_data=user_data)
示例#8
0
def login():
    if request.form:
        # If inputPassword2 field is not empty, this is a registration.
        name = request.form["username"]
        pwrd = request.form["password"]
        # Registration
        if request.form["password2"]:
            # Check for illegal characters in the name
            for x in name:
                if not re.match(r'[A-Za-z0-9_]+$', name):
                    flash(
                        "Your name has an illegal character! Only letters, numbers, and underscores are allowed."
                    )
                    return render_template('login.html')
            if (pwrd != request.form["password2"]):
                flash("Passwords do not match!")
                return render_template('login.html')
            if (len(pwrd) < 6):
                flash("Password must be at least 6 characters.")
                return render_template('login.html')
            if db.get_user_by("username", name):
                flash("User '{}' already exists!".format(name))
                return render_template('login.html')
            else:
                print("New user {} registered!".format(name))
                db.create_user(name, pwrd)
                flash("User created!")
                return render_template('login.html', newuser=True)
        # Validated user
        elif db.verify_user(name, pwrd):
            print(name, "has been verified!")
            user = User()
            user.id = name
            flask_login.login_user(user)
            return redirect("/profile/" + name)
        else:
            flash("Incorrect credentials!")
            return render_template('login.html', page_name="login")
    else:
        return render_template('login.html', page_name="login")
示例#9
0
def user_loader(username):
    u = db.get_user_by("name", username)
    if not u: return
    user = User()
    user.id = u["name"]
    return user
示例#10
0
def remove_friend():
    name = flask_login.current_user.id
    data = request.json
    db.unfriend(name, data["target"])
    print("REMOVING", db.get_user_by("name", name)["friends"])
    return jsonify({})
示例#11
0
def index():
    if request.form:
        # If inputPassword2 field is not empty, this is a registration.
        name = request.form["username"]
        pwrd = request.form["password"]
        # Registration
        if request.form["password2"]:
            # Check for illegal characters in the name
            for x in name:
                if not re.match(r'[A-Za-z0-9_]+$', name):
                    flash(
                        "Your name has an illegal character! Only letters, numbers, and underscores are allowed."
                    )
                    return render_template('index.html',
                                           page_name="index",
                                           current_user=False)
            if (pwrd != request.form["password2"]):
                flash("Passwords do not match!")
                return render_template('index.html',
                                       page_name="index",
                                       current_user=False)
            if (len(pwrd) < 6):
                flash("Password must be at least 6 characters.")
                return render_template('index.html',
                                       page_name="index",
                                       current_user=False)
            if db.get_user_by("username", name):
                flash("User '{}' already exists!".format(name))
                return render_template('index.html',
                                       page_name="index",
                                       current_user=False)
            else:
                print("New user {} registered!".format(name))
                db.create_user(name, pwrd)
                flash("User created!")
                return render_template('index.html',
                                       page_name="index",
                                       newuser=True,
                                       current_user=False)
        # Validated user
        elif db.verify_user(name, pwrd):
            print(name, "has been verified!")
            user = User()
            user.id = name
            flask_login.login_user(user)
            return redirect("/")
        else:
            flash("Incorrect credentials!")
            return render_template('index.html',
                                   page_name="index",
                                   current_user=False)
    else:
        current_user = flask_login.current_user
        # Structure of threads
        thread = db.get_comments()
        # If the user isn't logged in the id isn't defined
        try:
            current_user.id
        except:
            current_user = None
        return render_template('index.html',
                               page_name="index",
                               current_user=current_user,
                               main_thread=thread)