def search():

    client = data.get_client()
    search_by = request.args.get('search_by')
    search_str = request.args.get('search')
    filter_by_cat = request.args.get('filter_by_cat')
    filter_by_rating = request.args.get('filter_by_rating')
    results = dao.search_by_query(client, search_str,
                                  search_by,
                                  filter_by_cat,
                                  filter_by_rating)

    no_of_result = results.count()
    categories = dao.get_all_categories(client)

    all_users = dao.get_all_users(client)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
    else:
        user = None

    return render_template('search_result.template.html',results=results, search_str=search_str, user=user, cat=categories, all_users=all_users, no_of_result=no_of_result)
def process_edit_review(review_id):
    client = data.get_client()

    selected_categories = request.form.getlist('categories')
    cat_to_add = []

    review_to_edit = dao.get_review_by_review_id(client, review_id)

    posted_date = review_to_edit['posted']

    title = request.form.get('title')
    posted = posted_date
    user_id = request.form.get('user_id')
    product_name = request.form.get('product_name')
    product_brand = request.form.get('product_brand')
    country_of_origin = request.form.get('country_of_origin')
    rating = request.form.get('rating')
    review = request.form.get('review')

    if request.form.get('product_image'):
        image = request.form.get('product_image')
    else:
        image = request.form.get('existing_product_image')

    for sc in selected_categories:
        current_cat = dao.get_category_by_id(client, sc)

        cat_to_add.append({
            'category_id': ObjectId(current_cat['_id']),
            'name': current_cat['name']
        })

    dao.update_review_by_id(client, review_id, user_id, title, posted, product_name, product_brand, country_of_origin, rating, review, image, cat_to_add)

    return redirect(url_for('view_my_reviews', user_id=user_id))
def process_add_review():

    client = data.get_client()
    selected_categories = request.form.getlist('categories')
    cat_to_add = []

    user = check_user_log_in(client)

    user_id = user['_id']

    for sc in selected_categories:
        current_cat = dao.get_category_by_id(client, sc)

        cat_to_add.append({
            'category_id': ObjectId(current_cat['_id']),
            'name': current_cat['name']
        })

    client[DB_NAME].user_reviews.insert_one({
        'title': request.form.get('title'),
        'posted': datetime.datetime.now(),
        'user_id': ObjectId(user_id),
        'product_name': request.form.get('product_name'),
        'product_brand': request.form.get('product_brand'),
        'country_of_origin': request.form.get('country_of_origin'),
        'categories': cat_to_add,
        'rating': request.form.get('rating'),
        'review': request.form.get('review'),
        'image': request.form.get('product_image')
    })

    return redirect(url_for('view_my_reviews', user_id=user_id))
def process_add_user():

    client = data.get_client()
    categories = dao.get_all_categories(client)

    password = request.form.get('password')
    encrypted_password = pbkdf2_sha256.hash(password)

    email = request.form.get('email')
    name = request.form.get('name')
    age = request.form.get('age')
    gender = request.form.get('gender')
    occupation = request.form.get('occupation')

    user_exist = dao.get_user_by_email(client, email)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
    else:
        user = None

    if user_exist:
        flash('You have already registered! Please login.')
        return render_template('register.template.html', cat=categories, user=user)
    else:
        dao.add_user_to_db(client, email, name, encrypted_password, age, gender, occupation)
        redirect_url = request.args.get('redirect')

        if redirect_url is None:
            redirect_url = "/"

        return redirect(redirect_url)
def edit_profile(user_id):

    client = data.get_client()

    user = check_user_log_in(client)
    categories = dao.get_all_categories(client)

    return render_template('edit_profile.template.html', user=user, cat=categories)
def edit_review(review_id):
    client = data.get_client()
    public_key = os.environ.get('UPLOADCARE_PUBLIC_KEY')

    review = dao.get_review_by_review_id(client, review_id)
    user = dao.get_one_user(client, review['user_id'])
    categories = dao.get_all_categories(client)

    return render_template('edit_review.template.html', r=review, user=user, cat=categories, public_key=public_key, ratings=ratings)
def confirm_delete_review(review_id):

    client = data.get_client()

    review = dao.get_review_by_review_id(client, review_id)
    user = check_user_log_in(client)
    categories = dao.get_all_categories(client)

    return render_template('delete_review.template.html', r=review, user=user, cat=categories)
def read_reviews():
    client = data.get_client()

    latest_review = client[DB_NAME].user_reviews.find().sort('posted', pymongo.DESCENDING).limit(4)

    users = dao.get_all_users(client)
    categories = dao.get_all_categories(client)

    skincare_reviews = client[DB_NAME].user_reviews.find(
        {
             'categories.name': 'Skin Care'
        }
    ).sort('posted', pymongo.DESCENDING).limit(4)

    cosmetic_reviews = client[DB_NAME].user_reviews.find(
        {
            'categories.name': 'Make Up'
        }
    ).sort('posted', pymongo.DESCENDING).limit(4)

    bodycare_reviews = client[DB_NAME].user_reviews.find(
        {
            'categories.name': 'Body Care'
        }
    ).sort('posted', pymongo.DESCENDING).limit(1)

    haircare_reviews = client[DB_NAME].user_reviews.find(
        {
            'categories.name': 'Hair Care'
        }
    ).sort('posted', pymongo.DESCENDING).limit(1)

    formen_reviews = client[DB_NAME].user_reviews.find(
        {
            'categories.name': 'For Men'
        }
    ).sort('posted', pymongo.DESCENDING).limit(1)

    user_list = []
    for user in users:
        user_list.append(user)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
        else:
            user = None


    return render_template('read_reviews.template.html', review=latest_review, cat=categories, users=user_list, skincare=skincare_reviews, cosmetics=cosmetic_reviews, bodycare=bodycare_reviews, haircare=haircare_reviews, formen=formen_reviews, user=user)
def delete_review(review_id):
    client = data.get_client()

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
        else:
            user = None

    dao.delete_review_by_id(client, review_id)

    return redirect(url_for('view_my_reviews', user_id=user['_id']))
def user_loader(email):

    client = data.get_client()

    # attempt to get the user
    user_in_db = dao.get_user_by_email(client, email)

    user = User()
    user.id = user_in_db['email']

    if user:
        return user
    else:
        return None
def add_user():

    client = data.get_client()

    categories = dao.get_all_categories(client)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
    else:
        user = None

    return render_template('register.template.html', cat=categories, user=user)
def view_review_details(review_id):

    client = data.get_client()

    review = dao.get_review_by_review_id(client, review_id)
    all_users = dao.get_all_users(client)
    all_cat = dao.get_all_categories(client)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
    else:
        user = None

    return render_template('review_details.template.html', cat=all_cat, review=review, user=user, all_users=all_users)
def read_reviews_by_category(cat_id):

    client = data.get_client()
    reviews = dao.get_reviews_by_cat_id(client, cat_id)
    current_cat = dao.get_category_by_id(client, cat_id)
    all_cat = dao.get_all_categories(client)
    all_users = dao.get_all_users(client)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
    else:
        user = None

    return render_template('read_reviews_by_cat.template.html', current_cat=current_cat, cat=all_cat, reviews=reviews, user=user, all_users=all_users)
def process_edit_profile(user_id):
    client = data.get_client()

    user = check_user_log_in(client)
    email = request.form.get('email')
    name = request.form.get('name')
    password = user['password']
    age = request.form.get('age')
    gender = request.form.get('gender')
    occupation = request.form.get('occupation')

    dao.update_user_profile(client, user_id, email, name, password, age, gender, occupation)

    user = dao.get_one_user(client, user_id)
    categories = dao.get_all_categories(client)

    flash('Profile Updated')

    return render_template('edit_profile.template.html', user=user, cat=categories)
def view_my_reviews(user_id):

    client = data.get_client()

    my_reviews = dao.get_review_by_userid(client, user_id)
    categories = dao.get_all_categories(client)
    user = dao.get_one_user(client, user_id)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
        else:
            user = None
    else:
        return redirect('/user_login?redirect=/read_reviews')

    return render_template('my_reviews.template.html', reviews=my_reviews, user=user, cat=categories)
def add_review():

    client = data.get_client()
    public_key = os.environ.get('UPLOADCARE_PUBLIC_KEY')

    categories = dao.get_all_categories(client)
    cat_list = []

    for cat in categories:
        cat_list.append(cat)

    if flask_login.current_user.is_authenticated:
        current_user = flask_login.current_user

        if current_user:
            user = dao.get_user_by_email(client, current_user.id)
        else:
            user = None
    else:
        return redirect('/user_login?redirect=/add_review')

    return render_template('add_review.template.html',cat=cat_list, ratings=ratings, public_key=public_key, user=user)
def proccess_user_login():

    client = data.get_client()

    user_in_db = dao.get_user_by_email(client, request.form.get('email'))

    print(user_in_db)

    if user_in_db:
    
        user = User()
        user.id = user_in_db['email']

        redirect_url = request.args.get('redirect')

        if redirect_url is None:
            redirect_url = "/"

        if verify_password(request.form.get('password'), user_in_db['password']):
            flask_login.login_user(user)
            return redirect(redirect_url)
    else:
        flash('Login Failed. Invalid Email or Password')
        return render_template('user_login.template.html')
Beispiel #18
0
def teardown_function():
    data.get_client().drop_database(TEST_DB)