Ejemplo n.º 1
0
def remove_deal_num_from_lists(deal_sequence_num):
    '''
    Removing deal_sequence_num from any list of deal_sequence_nums that contains
    the specific deal_sequence_num that's passed into this function.

    This is called when a user deletes a deal that he or she has authored.
    '''
    pipe = r.pipeline()
    keys = r.lrange(set_of_deal_list_keys, 0, -1)
    for key in keys:
        nums = pipe.lrange(key, 0, -1)
        if str(deal_sequence_num) in nums:
            pipe.lrem(key, 1, deal_sequence_num)
    pipe.execute()
Ejemplo n.º 2
0
def show_user_profile(name, filter_by, page, sort):
    '''
    This method retrieve a list of deals shared or bookmarked by the
    user. This method is used to show the user's history on the site.

    A user's bookmark is private to the user's eye's only. Abort 404 will be
    thrown if a users try to access some other user's bookmark.
    '''
    #sanity check our inputs
    if sort not in sorts:
        return abort(404)
    #are we trying to retrieve the profile of a non-existent user?
    page_owner = User.objects(name=name).first()
    if page_owner is None:
        return abort(404)
    # if we are trying filtering by the correct categories?
    if filter_by not in user_history_filters:
        return abort(404)
    #do not allow other users to see other user's bookmark
    current_user = get_current_user()
    if filter_by == 'bookmarked' and (current_user == None
                                      or current_user.name != name):
        return abort(404)
    key = [name, '_', filter_by, '_', sort]
    key = ''.join(key)
    deal_seq_nums = None
    if r.exists(key):
        deal_seq_nums = r.lrange(key, 0, -1)
        if deal_seq_nums == ['None']:
            deal_seq_nums = []
    else:
        deal_queryset = query_for_deals(page_owner, filter_by, sort)
        deal_seq_nums = [deal.sequence_num for deal in deal_queryset]
        store_list_of_deals(key, deal_seq_nums)
        for deal in deal_queryset:
            store_deal(deal)

    start = (page - 1) * per_page
    end = page * per_page
    has_next = True if end < len(deal_seq_nums) else False
    has_previous = True if start > 0 else False
    deal_seq_nums = deal_seq_nums[start:end]
    return render_template('user_history.html',
                           current_filter=filter_by,
                           current_page=page,
                           current_sort=sort,
                           owner=page_owner,
                           deal_seq_nums=deal_seq_nums,
                           has_next=has_next,
                           has_previous=has_previous)
Ejemplo n.º 3
0
def remove_deal_num_from_lists(deal_sequence_num):
    '''
    Removing deal_sequence_num from any list of deal_sequence_nums that contains
    the specific deal_sequence_num that's passed into this function.

    This is called when a user deletes a deal that he or she has authored.
    '''
    pipe = r.pipeline()
    keys = r.lrange(set_of_deal_list_keys, 0, -1)
    for key in keys:
        nums = pipe.lrange(key, 0, -1)
        if str(deal_sequence_num) in nums:
            pipe.lrem(key, 1, deal_sequence_num)
    pipe.execute()
Ejemplo n.º 4
0
def show_user_profile(name, filter_by, page, sort):
    '''
    This method retrieve a list of deals shared or bookmarked by the
    user. This method is used to show the user's history on the site.

    A user's bookmark is private to the user's eye's only. Abort 404 will be
    thrown if a users try to access some other user's bookmark.
    '''
    #sanity check our inputs
    if sort not in sorts:
        return abort(404)
    #are we trying to retrieve the profile of a non-existent user?
    page_owner = User.objects(name=name).first()
    if page_owner is None:
        return abort(404)
    # if we are trying filtering by the correct categories?
    if filter_by not in user_history_filters:
        return abort(404)
    #do not allow other users to see other user's bookmark
    current_user = get_current_user()
    if filter_by == 'bookmarked' and (current_user == None or
                                     current_user.name != name):
        return abort(404)
    key = [name, '_', filter_by, '_', sort]
    key = ''.join(key)
    deal_seq_nums = None
    if r.exists(key):
        deal_seq_nums = r.lrange(key, 0, -1)
        if deal_seq_nums == ['None']:
            deal_seq_nums = []
    else:
        deal_queryset = query_for_deals(page_owner, filter_by, sort)
        deal_seq_nums = [deal.sequence_num for deal in deal_queryset]
        store_list_of_deals(key, deal_seq_nums)
        for deal in deal_queryset:
            store_deal(deal)

    start = (page - 1) * per_page
    end = page * per_page
    has_next = True if end < len(deal_seq_nums) else False
    has_previous = True if start > 0 else False
    deal_seq_nums = deal_seq_nums[start:end]
    return render_template('user_history.html', current_filter=filter_by,
                            current_page=page, current_sort=sort,
                            owner=page_owner, deal_seq_nums=deal_seq_nums,
                            has_next=has_next, has_previous=has_previous)
Ejemplo n.º 5
0
def show_homepage(category, date_range, sort, page):
    '''
    This method returns a liste of deals as filtered and sorted by
    category, date_range, and sort. The list of deals is then paginated
    which allows us to display deals per page in the template view.
    '''
    #sanity check: making sure the input values are valid
    if page < 1 or category not in categories or sort not in sorts or \
       date_range not in date_ranges:
        abort(404)

    #checking to see if the requestesd deal data is stored in redis
    key = [category, '_', date_range, '_', sort]
    key = ''.join(key)
    deal_seq_nums = None
    if r.exists(key):
        deal_seq_nums = r.lrange(key, 0, -1)
        if deal_seq_nums == ['None']:
            deal_seq_nums = []
    else:
        deal_queryset = query_for_deals(category, date_range, sort)
        deal_seq_nums = [deal.sequence_num for deal in deal_queryset]
        store_list_of_deals(key, deal_seq_nums)
        # for deal in deal_queryset:
        #     store_deal(deal)

    #preparing some date to pass into the template
    start = (page - 1) * per_page
    end = page * per_page
    has_next = True if end < len(deal_seq_nums) else False
    has_previous = True if start > 0 else False
    deal_seq_nums = deal_seq_nums[start:end]

    # remembering  current_date, and current_sort in session
    # this is useful when a user's in a specific deal's page and we need
    # to highlight the correct sidebar filter links
    session['current_date'] = date_range
    session['current_sort'] = sort

    return render_template('homepage.html', current_category=category,
                           current_date=date_range, current_sort=sort,
                           current_page=page, deal_seq_nums=deal_seq_nums,
                           has_next=has_next, has_previous=has_previous,
                           next_page=page + 1, previous_page=page - 1)