Example #1
0
 def num_deals_shared(self):
     key = "".join([self.name, '_num_deals_shared'])
     if r.exists(key):
         return r.get(key)
     else:
         num_votes_given = len(self.deals_submitted)
         r.setex(key, 3600, num_votes_given)
         return num_votes_given
Example #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)
Example #3
0
    def num_likes_received(self):
        '''
        This method returns the total amounts of points that this user's
        submitted deal has received
        '''

        key = "".join([self.name, '_num_likes_received'])
        if r.exists(key):
            return r.get(key)
        else:
            # points = [deal.num_votes for deal in self.deals_submitted]
            # total_points = sum(points)
            deals = Deal.objects(id__in=self.deals_submitted)
            points = [deal.num_votes for deal in deals]
            total_points = sum(points)
            r.setex(key, 3600, total_points)
            return total_points
Example #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)
Example #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)