Beispiel #1
0
def signUp():
    """create  users"""
    if request.method == 'POST':
        f = request.form['full_name']
        e = request.form['email']
        p = request.form['password']
        if not e and not f and not p:
            return render_template('index.html',
                                   cache_id=uuid.uuid4(),
                                   error="Missing information")
        all_cls = storage.all(User)
        for value in all_cls.values():
            if (value.email == e):
                return render_template('index.html',
                                       cache_id=uuid.uuid4(),
                                       error="Already Registred")
        req_data = request.form.to_dict()
        new = User()
        for k, v in req_data.items():
            if k == "password":
                v = new.hashpwd(v)
            setattr(new, k, v)
        setattr(new, "user_avatar", '/static/images/default-user-image.png')
        new.auth = True
        session['email'] = request.form['email']
        new.save()
        token = Emailsecurity()
        token.email = request.form['email']
        print(token.id)
        token.save()
        body = "registration.html"
        subj = "Welcome  to CrossMe Platform"
        m = Mailings(body, subj, new, token)
        m.send()
        my_user = storage.getbyemail(User, request.form['email'])
        return render_template('steptwo.html',
                               cache_id=uuid.uuid4(),
                               user_info=my_user)
    else:
        if session:
            storage.reload()
            user_info = storage.getbyemail(User,
                                           session['email']).as_dict_nopwd()
            if not user_info["fb_access_token"] and not user_info[
                    "ig_access_token"]:
                return render_template('steptwo.html',
                                       cache_id=uuid.uuid4(),
                                       user_info=user_info)
            elif not user_info["fb_access_token"] or not user_info[
                    "ig_access_token"]:
                return render_template('steptwo.html',
                                       cache_id=uuid.uuid4(),
                                       user_info=user_info)
            else:
                return render_template('home.html',
                                       cache_id=uuid.uuid4(),
                                       user=session['email'])
        else:
            return render_template('index.html', cache_id=uuid.uuid4())
Beispiel #2
0
def logg():
    if request.method == 'POST':
        storage.reload()
        if request.form['email'] and request.form['password']:
            req_data = request.form.to_dict()
            users = storage.getbyemail(User, request.form['email'])
            if users is None:
                return render_template('index.html',
                                       cache_id=uuid.uuid4(),
                                       error="Invalid Details")
            a = users.as_dict()
            print(a)
            print(a.__class__.__name__)
            b = User().verify_password(req_data['password'], a["password"])
            if b:
                session['email'] = request.form['email']
                if not a["fb_access_token"] and not a["ig_access_token"]:
                    return render_template('steptwo.html',
                                           cache_id=uuid.uuid4(),
                                           user_info=session['email'])
                else:
                    return redirect('/')
            else:
                return render_template('index.html',
                                       cache_id=uuid.uuid4(),
                                       error="Invalid Details")
        else:
            return render_template('index.html',
                                   cache_id=uuid.uuid4(),
                                   error="Missing information")
    else:
        if session:
            storage.reload()
            user_info = storage.getbyemail(User,
                                           session['email']).as_dict_nopwd()
            if not user_info["fb_access_token"] and not user_info[
                    "ig_access_token"]:
                return render_template('steptwo.html',
                                       cache_id=uuid.uuid4(),
                                       user_info=user_info)
            elif not user_info["fb_access_token"] or not user_info[
                    "ig_access_token"]:
                return render_template('steptwo.html',
                                       cache_id=uuid.uuid4(),
                                       user_info=user_info)
            else:
                return render_template('home.html',
                                       cache_id=uuid.uuid4(),
                                       user=session['email'])
        else:
            return render_template('index.html', cache_id=uuid.uuid4())
Beispiel #3
0
def get_user_byemail(user_email):
    """
    Get user information by email
    Return:
        if not valid user_email or no user found:
            error code: 8
        if success:
            return user info
        else
            unknown error (code : 0)
    """
    response_dict = {}
    response_dict["error"] = "invalid parameter"
    response_dict["usage"] = "/users/<user_id>"
    response_dict["error_code"] = "8"

    if user_email.find("@") == -1:
        return make_response(jsonify(response_dict), 202)
    else:
        user = storage.getbyemail(User, user_email)
        if not user:
            return make_response(jsonify(response_dict), 202)
        else:
            user_info = user.to_dict()
            return make_response(jsonify(user_info), 200)

    unknown_dict = {}
    unknown_dict["error"] = "unknown error"
    unknown_dict["error_code"] = "0"
    return make_response(jsonify(unknown_dict), 404)
Beispiel #4
0
def settingpahe():
    """setting page"""
    if request.method == "GET":
        storage.reload()
        Token.delete_expired()
        storage.save()
        if "email" in session:
            print()
            user_email = session['email']
            user_obj = storage.getbyemail(User, user_email)
            try:
                session['id'] = user_obj.id
            except:
                session['id'] = session['id']
                user_obj = storage.get(User, str(session['id']))
            before_request_func()
            newstr = str(uuid.uuid4()).replace("-", "")
            tokenobj = Token()
            tokenobj.user_id = session['id']
            tokenobj.securecode = newstr
            tokenobj.save()
            return render_template('settings.html',
                                   cache_id=uuid.uuid4(),
                                   me=user_obj,
                                   token=newstr)
    return redirect('/')
Beispiel #5
0
def home():
    """
    serve home page
    """
    """        try:
            if session['id']:
                newmail=storage.get(User, session['id'])
                session['email']=newmail.mail
                print(session)
    """
    if "email" in session:
        ten_post = []
        email = session['email']
        user = storage.getbyemail(User, email)
        user_dict = user.to_dict()
        user_id = user_dict["id"]

        user_follows = user.follow_list()

        if not user_follows:
            return render_template('home.html',
                                   cache_id=uuid.uuid4(),
                                   post_st=ten_post,
                                   user_info=user_dict)

        friends_posts = []
        for friend_id in user_follows:
            post_lst = storage.getlist_by_attr(Post, friend_id, user_id)
            friends_posts = friends_posts + post_lst

        if not friends_posts:
            return render_template('home.html',
                                   cache_id=uuid.uuid4(),
                                   ten_post=ten_post,
                                   user_info=user_dict)

        sorted_post_list = storage.sort_posts(friends_posts)

        for i in range(10):
            if len(sorted_post_list) < i + 1:
                break
            post = sorted_post_list[i]
            friend_profile_obj = storage.get(User, post["user_id"])
            friend_profile = friend_profile_obj.to_dict()
            post["full_name"] = friend_profile["full_name"]
            post["user_avatar"] = friend_profile["user_avatar"]
            ten_post.append(post)

        return render_template('home.html',
                               cache_id=uuid.uuid4(),
                               ten_post=ten_post,
                               user_info=user_dict)

    return render_template('index.html', cache_id=uuid.uuid4())
Beispiel #6
0
def del_follow(followed_id):
    """send request to the qpi to delet a follow"""
    if request.method == "DELETE":
        if "email" in session:
            user_email = session['email']
            user_obj = storage.getbyemail(User, user_email)
            follower_id = user_obj.id
            response = rq.delete('https://0.0.0.0:5002/api/v1/del_follow/' +
                                 follower_id + '/' + followed_id + '/onetwo',
                                 verify=False)
            succ = {}
            succ["status"] = "ok"
            return make_response(jsonify(succ), 200)
    return redirect('/profile/' + followed_id)
Beispiel #7
0
def render_me():
    """
    Render The profile page
    User will be redirected by it id to his profile
    """
    if "email" in session:
        storage.reload()
        user_email = session['email']
        print(user_email)
        me_user = storage.getbyemail(User, user_email)
        user_id = me_user.id
        NEWURL = '/profile/' + user_id
        return redirect(NEWURL)
    else:
        redirect('/')
Beispiel #8
0
def CMsearch(the_search=None):
    if "email" in session:
        storage.reload()
        selfuserobj = storage.getbyemail(User, session["email"])
        user_id = selfuserobj.id
        followinglist = selfuserobj.follow_list()
        rslt = {}
        if request.method == "POST":
            pattern = request.form['pt']
        else:
            if the_search == None:
                return render_template('search.html',
                                       cache_id=uuid.uuid4(),
                                       me=user_id)
            else:
                pattern = the_search
        if len(pattern) is None or len(pattern) > 25:
            return render_template('search.html',
                                   cache_id=uuid.uuid4(),
                                   result=[],
                                   me=user_id)
        if "@" in pattern:
            my_list = []
            my_user = storage.getbyemail(User, pattern)
            if my_user:
                if my_user in followinglist:
                    rslt["status"] = "ok"
                else:
                    rslt["status"] = "no"
                rslt["user"] = my_user
                my_list.append(rslt)
                return render_template('search.html',
                                       cache_id=uuid.uuid4(),
                                       result=my_list,
                                       me=user_id)
            else:
                return render_template('search.html',
                                       cache_id=uuid.uuid4(),
                                       result=[],
                                       me=user_id)
        strsplitted = pattern.upper().split()
        allusers = storage.all(User)
        my_list = []
        for i in allusers.values():
            if i.full_name.upper() == pattern.upper():
                if i.id in followinglist:
                    rslt["status"] = "ok"
                else:
                    rslt["status"] = "no"
                rslt["user"] = i
                my_list.append(rslt)
                rslt = {}
            else:
                for j in strsplitted:
                    if j in i.full_name.upper().split():
                        if i.id in followinglist:
                            rslt["status"] = "ok"
                        else:
                            rslt["status"] = "no"
                        rslt["user"] = i
                        my_list.append(rslt)
                        rslt = {}
        return render_template('search.html',
                               cache_id=uuid.uuid4(),
                               result=my_list,
                               me=user_id)
    else:
        return redirect('/')
Beispiel #9
0
def render_profile(user_id):
    """
    Render The profile page
    if user doesn't exist or invalid id redirect to home

    """
    try:
        uuid_obj = uuid.UUID(user_id, version=4)
    except ValueError:
        return redirect('/')
    storage.reload()
    user = storage.get(User, user_id)
    user_info = user.to_dict()

    if "email" in session:
        my_email = session['email']
        my_user = storage.getbyemail(User, my_email)
        my_id = my_user.id

        all_user_post = storage.getlist_by_attr(Post, user_id, my_id)
    else:
        all_user_post = storage.getlist_by_attr(Post, user_id)

    number_of_followers = storage.follower_number(Follow, user_id)

    if "email" in session:

        my_email = session['email']
        my_user = storage.getbyemail(User, my_email)
        my_id = my_user.id

        current_user_email = session['email']

        if user_info["email"] == current_user_email:
            follow_code = "3"
            return render_template('profile.html',
                                   cache_id=str(uuid.uuid4()),
                                   user_info=user_info,
                                   all_user_post=all_user_post,
                                   is_user="******",
                                   follow_code=follow_code,
                                   number_of_followers=number_of_followers,
                                   current_user_id=my_id)

        else:
            my_current_user = storage.getbyemail(User, current_user_email)
            current_user_id = my_current_user.id
            follow_obj = storage.get_by_two(Follow, current_user_id, user_id)
            if follow_obj:
                follow_code = str(follow_obj.follow_code)
                if follow_code != "0":
                    rev_obj = storage.get_by_two(Follow, user_id,
                                                 current_user_id)
                    if rev_obj:
                        if follow_obj.follow_code == 1 and rev_obj.follow_code == 1:
                            follow_code = "1"
                        elif rev_obj.follow_code == 1:
                            follow_code = "2"
            else:
                follow_code = "0"
                rev_obj = storage.get_by_two(Follow, user_id, current_user_id)
                if rev_obj:
                    if follow_obj:
                        if follow_obj.follow_code == 1 and rev_obj.follow_code == 1:
                            follow_code = "1"
                        elif rev_obj.follow_code == 1:
                            follow_code = "2"
                    else:
                        follow_code = "2"

            return render_template('profile.html',
                                   cache_id=str(uuid.uuid4()),
                                   user_info=user_info,
                                   all_user_post=all_user_post,
                                   is_user="******",
                                   follow_code=follow_code,
                                   number_of_followers=number_of_followers,
                                   current_user_id=my_id)

    return render_template('profile.html',
                           cache_id=str(uuid.uuid4()),
                           user_info=user_info,
                           all_user_post=all_user_post,
                           is_user="******",
                           follow_code="9",
                           number_of_followers=number_of_followers)