def find_profile_prods(user, profile_user):
  """
      Finds the best and worst commprods
      for a given profile if they exist.
      If none exists boolean is sent back and nothing is
      rendered.
  """
  if cpm.CommProd.objects.filter(
          user_profile=profile_user.profile).exists():
    sorted_commprods = cpm.CommProd.objects.filter(
        user_profile=profile_user.profile
    ).order_by('score')
    count = sorted_commprods.count()

    if count == 0:
      return False, False

    worst_prod = sorted_commprods[0]
    best_prod = sorted_commprods[count - 1]

    # render html
    best_prod, worst_prod = commprod_renderer(
        user, [best_prod, worst_prod], 'list')
  else:
    best_prod = False
    worst_prod = False

  return best_prod, worst_prod
def commprod_query_manager(get_dict, user, return_type='html'):
  """
      Takes in a get request's dictionary of
      values and returns an HTMl template based
      on the search query
  """
  valid_params = ['cp_id', 'query', 'direction',
                  'username', 'startDate', 'endDate',
                  'limit', 'unvoted', 'orderBy', 'rec']
  valid_types = {
      'best': {
          'orderBy': 'score',
          'direction': 'lh',
      },
      'worst': {
          'orderBy': 'score',
      },
      'recent': {
          'orderBy': 'date',
          'direction': 'lh',
      },
      'trending': {
          'orderBy': 'trending_score',
          'direction': 'lh',
      },
      'media': {
          'orderBy': 'date',
          'media': True,
          'direction': 'lh',
      },
  }

  search_params = {k: v for k, v in get_dict.items()
                   if k in valid_params}

  # overwrite given parameters with default for type.
  type = get_dict.get('type', None)
  if type in valid_types:
    search_params = dict(search_params, **valid_types[type])

  if 'unvoted' in search_params:
    search_params['unvoted'] = user.username

  # if 'rec' in search_params:
  #     search_params['rec'] = user.username

  if type == 'favorites':
    # user who's profile is being viewed
    commprods = get_commprod_favs(get_dict['username'])

  else:
    commprods = commprod_search(**search_params)

  return commprod_renderer(user, commprods,
                           return_type, type, get_dict.get('page', 1))