Пример #1
0
 def test_build_url(self):
   """
   Verify that url can be built for 21045.
   """
   zip_code = '21045'
   oodle = OodleAPI()
   url = oodle.build_url(zip_code)
   self.assertTrue(zip_code in url)
   f = urlopen(url)
   self.assertEqual(200, f.code)
Пример #2
0
  def handle_noargs(self, **options):
    
    # get all requested items that haven't been closed,
    requested_items = RequestedItem.objects.filter(is_closed=False)


    oodle = OodleAPI()
    for item in requested_items:

      kwargs_dict = {'search_term': item.description}

      if item.has_price_range:
        kwargs_dict['attributes'] = 'price_%d_%d' % (item.min_price, item.max_price)

      oodle_api_url = oodle.build_url(location=item.location, **kwargs_dict)
      total_results, posts = oodle.get_oodle_results(oodle_api_url)

      # handle total results? right now just leaving it so they only get the first page of results
      for post in posts:

        # this doens't seem right but it seems easy, there will be duplicate found items, but have different
        # users.
        try:
          FoundItem.objects.get(url=post['url'], user=item.user)
          continue
        except FoundItem.DoesNotExist:
          pass

        # round this price up to the next, if there is a price
        try:
          price = int(ceil(Decimal(post['attributes']['price'])))
        except KeyError:
          # missing price
          #import ipdb; ipdb.set_trace()
          continue

        found_item = FoundItem(url=post['url'], description=post['title'], 
                               price=price, user=item.user)
        if post['images']:
          found_item.photo_url = post['images'][0]['src']
        found_item.save()

        item.found_items.add(found_item)
Пример #3
0
def classifieds(request):
  """
  Check the location of user for best guess and render classifieds template.
  """
  user_ip = request.META.get('REMOTE_ADDR')
  #import ipdb; ipdb.set_trace()

  #user_ip = '68.48.6.208'
  # Check that user ip is valid if not either show all states, and cities
  # or handle it a different way
  g = GeoIP()
  user_location = g.city(user_ip)
  oodle = OodleAPI(num_results=15)

  location = 'San Fransisco'
  if user_location:
    #location = user_location['postal_code']
    location = '%s, %s' % (user_location['city'], user_location['region'])

  oodle_api_url = oodle.build_url(location=location)
  try:
    total_results, posts = oodle.get_oodle_results(oodle_api_url)
  except OodleAPIException:
    return HttpResponseRedirect(reverse('about_signup'))

  # get all main categories
  categories_dict = {}
  """
  root_categories = Category.objects.filter(is_root=True)
  for root_category in root_categories:
    categories_dict[root_category] = Category.objects.filter(is_root=False, 
                                                             url__contains=root_category.url)
  """
  template_name = 'classifieds.html'

  return render_to_response(template_name, {'posts': posts, 'city': location,
                            'categories': categories_dict, 'user_ip': user_ip},
                            context_instance=RequestContext(request))
Пример #4
0
def search(request):
  """
  Search oodle for a city.  Optional get parameters include page, category
  """
  if not request.GET.get('paginated'):
    required_params = ['city']
    for param in required_params:
      if not request.GET.get(param):
        return HttpResponse(json.dumps({'success': False, 
          'message': 'Missing %s param from request' % (param)}))

    # make sure one of the following params is available
    if 'search_term' not in request.GET and 'category' not in request.GET:
      return HttpResponse(json.dumps({'success': False, 
        'message': 'Include either a search search_term or a category please'}))

    # make sure that user passed page is an integer, wacky validation mess
    additional_params = {}
    try:
      current_page = int(request.GET.get('page', 1))
      additional_params['page'] = current_page
    except ValueError:
      current_page = 1

    optional_params = ['category', 'search_term']
    for param in optional_params:
      try:
        additional_params[param] = request.GET[param]
      except KeyError:
        pass
    oodle = OodleAPI()
    oodle_api_url = oodle.build_url(location=request.GET['city'], **additional_params)
    try:
      total_results, posts = oodle.get_oodle_results(oodle_api_url)
    except OodleAPIException:
      return HttpResponse(json.dumps({'success': False, 
        'message': 'Classifieds are down, sorry for the inconvenience'}))

    last_query = oodle.additional_params_str

  else:
    # This is a pagineated request get last query from the session.
    #import ipdb; ipdb.set_trace()
    oodle = OodleAPI()

    # wasn't a paginated request give them any data that can be found
    if not request.session.get('last_query'):
      oodle_api_url = oodle.build_url(location='')
    else:
      # use the value from the session
      oodle_api_url = '%s?%s&%s&start=%s' % (oodle.base_url, urlencode(oodle.params_dict),
                                            request.session.get('last_query'), request.GET['paginated'])
      last_query = request.session.get('last_query')
      try:
        current_page = int(request.GET['paginated'])
      except ValueError:
        current_page = 1
        oodle_api_url = oodle.build_url(location='')
    try:  
      total_results, posts = oodle.get_oodle_results(oodle_api_url)
    except OodleAPIException:
      return HttpResponse(json.dumps({'success': False, 
        'message': 'Classifieds are down, sorry for the inconvenience'}))

  # do pagination, if there is only one page don't show any pagination 
  total_pages = int(ceil(total_results / 30.0))
  pagination_html = ''
  if total_pages > 1:
    pagination_html = render_to_string('pagination.html', {'total_pages': xrange(1, total_pages+1, 1), 
                                                           'current_page': current_page,
                                                           'last_query': last_query})
    # add this last query to the session in case it's used later
    request.session['last_query'] = last_query
  
  posts_html = render_to_string('posts.html', {'posts': posts})

  return HttpResponse(json.dumps({'success': True, 'posts_html': posts_html, 
                                  'total_pages': total_pages ,
                                  'pagination_html': pagination_html }))