Example #1
0
def send_notification(_id, msg=None):
    if not msg:
        try:
            msg = request.values['msg']
        except:
            abort(404)
    users.update_one({'_id': ObjectId(_id)}, {'$push': {'notifications': msg}})
    return 'True'
Example #2
0
def delete_notification(_id, index):  # this is not atomic
    try:
        users.update_one({'_id': ObjectId(_id)},
                         {'$unset': {
                             "notifications.{}".format(index): 1
                         }})
        users.update_one({'_id': ObjectId(_id)},
                         {'$pull': {
                             "notifications": None
                         }})
        return 'true'
    except:
        return 'false'
Example #3
0
def like(home_id, user_id, json):
    from config import users
    if json['like']:
        collection.update_one({'_id': home_id},
                              {'$addToSet': {
                                  'reviews.likes.all': user_id
                              }})
        users.update_one({'_id': user_id},
                         {'$addToSet': {
                             'wish_list': home_id
                         }})
    if not json['like']:
        collection.update_one({'_id': home_id},
                              {'$pull': {
                                  'reviews.likes.all': user_id
                              }})
        users.update_one({'_id': user_id}, {'$pull': {'wish_list': home_id}})
Example #4
0
def give_up(pr_id=None, _id=None):
    if not pr_id:
        _id = ObjectId(_id)
        if _id not in current_user.carts:
            abort(403, 'sorry but this cart is reserved')
        del (current_user.carts[_id])
        users.update_one({'_id': current_user._id},
                         {'$unset': {
                             'carts.{}'.format(str(_id)): 1
                         }}, False, True)
        return json.dumps({
            'success': True,
            'message': 'cart removed'
        }), 200, {
            'ContentType': 'application/json'
        }
    pr_id = ObjectId(pr_id)
    if _id:
        _id = ObjectId(_id)
        if _id not in current_user.carts or current_user.carts[_id][1]:
            abort(403, 'sorry but this cart is reserved')
        try:
            pair = next(pair for pair in current_user.carts[_id][0]
                        if pair[0] == pr_id)
            current_user.carts[_id][0].remove(pair)
            users.update_one({'_id': current_user._id},
                             {'$pop': {
                                 'carts.{}.0'.format(str(_id)): pair
                             }})
            return json.dumps({
                'success': True,
                'message': 'from specific cart gave up'
            }), 200, {
                'ContentType': 'application/json'
            }
        except:
            abort(403, 'pr not existed')
    elif not current_user.carts:
        abort(403, 'no cart existed')
    try:
        _id = current_user.carts.top()
        pair = next(pair for pair in current_user.carts[_id][0]
                    if pair[0] == pr_id)
        current_user.carts[_id][0].remove(pair)
        users.update_one({'_id': current_user._id},
                         {'$pop': {
                             'carts.{}.0'.format(str(_id)): pair
                         }})
        return json.dumps({
            'success': True,
            'message': 'gave up from top cart'
        }), 200, {
            'ContentType': 'application/json'
        }
    except:
        abort(403, 'pr not existed')
Example #5
0
def purchase(pr_id, qty, _id=None):
    pr_id = ObjectId(pr_id)
    if qty <= 0:
        abort(403, 'positive qty please')
    if _id:
        _id = ObjectId(_id)
        if _id not in current_user.carts or current_user.carts[_id][1]:
            abort(403, 'sorry but this cart is reserved')
        current_user.carts[_id][0].append([pr_id, qty])
        users.update_one(
            {'_id': current_user._id},
            {'$push': {
                'carts.{}.0'.format(str(_id)): [pr_id, qty]
            }})
        return json.dumps({'success': True}), 200, {
            'ContentType': 'application/json'
        }
    elif request.method == 'POST' or not current_user.carts:
        _id = ObjectId()
        now = datetime.now()
        current_user.carts[_id] = [[[pr_id, qty]], {'paid': False}, now]
        users.update_one({'_id': current_user._id}, {
            '$set': {
                'carts.{}'.format(str(_id)): [[[pr_id, qty]], {
                    'paid': False
                }, now]
            }
        })
    else:
        _id = current_user.carts.top()
        current_user.carts[_id][0].append([pr_id, qty])
        users.update_one(
            {'_id': current_user._id},
            {'$push': {
                'carts.{}.0'.format(str(_id)): [pr_id, qty]
            }})
        return json.dumps({
            'success': True,
            'message': 'purchased to top cart'
        }), 200, {
            'ContentType': 'application/json'
        }
Example #6
0
def profile():
    form = UpdateAccount()
    # Count the number of entries created by user
    num_entries = entries.count_documents({'user_id': current_user.id})
    # Count the number of entries favorites by user
    num_fav = entries.count({'user_id': current_user.id, 'is_fav': True})
    # Find average rating of all reviews created by user
    avg_rating = entries.aggregate([
        {"$match": {"user_id": current_user.id}},
        {"$group": {
                "_id": None,
                "result": {"$avg": "$rating"}
           }
         }
    ])

    # The average is rounded to the second decimal.
    # If no entries have been created, the average sent to the html
    # template is 0.
    if num_entries == 0:
        rounded_avg = 0
    else:
        rounded_avg = round(list(avg_rating)[0]['result'], 2)

    # The following manages updates of the account information.
    # Data from each field is sent to update the user document in mongodb.
    # If a field is left blank, the current user's data for that field is
    # sent instead.
    if form.is_submitted():
        if form.validate() and bcrypt.check_password_hash(
                                current_user.password,
                                form.password.data.encode('utf-8')):

            if form.username.data:
                new_username = form.username.data
            else:
                new_username = current_user.username

            if form.email.data:
                new_email = form.email.data
            else:
                new_email = current_user.email

            if form.new_password.data:
                new_password = bcrypt.generate_password_hash(
                                        form.new_password.data
                                        ).decode('utf-8')
            else:
                new_password = current_user.password

            users.update_one(
                {"_id": current_user.id},
                {"$set":
                    {
                        "username": new_username,
                        "email": new_email,
                        "password": new_password
                    }
                 }
            )
            flash("Account information updated successfully", "success")
            # A redirect is used to reload the page, to ensure that the newly
            # updated information is displayed
            return redirect(url_for("profile"))

        else:
            flash("There was a problem updating your information.", "danger")
    return render_template(
        'pages/profile.html',
        title="Profile",
        num_entries=num_entries,
        num_fav=num_fav,
        avg_rating=rounded_avg,
        username=current_user.username,
        email=current_user.email,
        form=form
    )