Esempio n. 1
0
def playground(tag=None):

    # Change search query if tag exists or not
    if tag:
        match_query = {
            'user_id': ObjectId('5f81b103c984b7e86dc415ad'),
            'tags': tag
        }
    else:
        match_query = {'user_id': ObjectId('5f81b103c984b7e86dc415ad')}

    # Query that returns entries, sorted by creation date or update date
    entries_list = entries.aggregate([
        {
            '$match': match_query
        },
        {
            '$addFields': {
                # Sorts by created date, or updated date if it exists
                'sort_date': {
                    '$cond': {
                        'if': '$updated_on',
                        'then': '$updated_on',
                        'else': '$created_on'
                    }
                }
            }
        },
        {
            '$sort': {
                'sort_date': -1
            }
        },
    ])

    return render_template('pages/listing.html',
                           title="Listing",
                           entries=entries_list,
                           tag=tag,
                           entry_count=1,
                           playground=True)
Esempio n. 2
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
    )
Esempio n. 3
0
def listing(tag=None):

    # Change search query if tag exists or not
    if tag:
        match_query = {'user_id': current_user.id, 'tags': tag}
    else:
        match_query = {'user_id': current_user.id}

    # Number of entries per page
    limit = 12

    if 'page' in request.args and request.args['page'].isnumeric():
        # Define which page to view based on get request
        page = int(request.args['page'])
    else:
        # if no pages are defined, view page 1
        page = 1

    # Set index of first result of query
    offset = (page - 1) * limit

    # Count number of results for the query
    entry_count = entries.count_documents(match_query)
    max_page = math.ceil(entry_count/limit)

    # If a tag has been entered in the url but no entries are
    # tagged with the keyword, return a 404 error.
    if tag and entry_count == 0:
        return render_template('pages/404.html',  title="Page Not Found")

    # Ensure that if an inexistant page is entered in the address bar,
    # a 404 page is returned
    if entry_count != 0 and page > max_page or page <= 0:
        return render_template('pages/404.html',  title="Page Not Found")

    # Query that returns entries, sorted by creation date or update date
    entries_list = entries.aggregate([
        {'$match': match_query},
        {'$addFields': {
            # Sorts by created date, or updated date if it exists
            'sort_date': {
                '$cond': {
                    'if': '$updated_on',
                    'then': '$updated_on',
                    'else': '$created_on'
                }
            }
        }},
        {'$sort': {'sort_date': -1}},
        {'$skip': offset},
        {'$limit': limit}
    ])

    # Create next and previous urls for pagination
    current_url = request.path
    next_url = (current_url + "?page=" + str(page + 1)
                if (page + 1) <= max_page
                else None)
    prev_url = (current_url + "?page=" + str(page - 1)
                if (page - 1) > 0
                else None)

    return render_template(
        'pages/listing.html',
        title="Listing",
        entries=entries_list,
        tag=tag,
        next_url=next_url,
        prev_url=prev_url,
        entry_count=entry_count)