Пример #1
0
    def remove_blacklisted_results():
      """Private helper function for dedup().

      Looks up stats for each result and deletes blacklisted results."""
      opp_ids = [result.merge_key for result in self.results]
      opp_stats = modelutils.get_by_ids(models.VolunteerOpportunityStats, 
                                        opp_ids)
      unknown_keys = set()
      nonblacklisted_results = []

      for result in self.results:
        if re.search('ACORN', result.title + result.snippet):
          logging.debug("blacklisting ACORN listing.")
          continue
        if result.merge_key not in opp_stats:
          unknown_keys.add(result.merge_key)
          nonblacklisted_results.append(result)
        elif not opp_stats[result.merge_key].blacklisted:
          nonblacklisted_results.append(result)

      self.results = nonblacklisted_results
      if unknown_keys:
        # This probably shouldn't be done right here... but we'll stuff these
        # in the memcache to prevent future datastore lookups.
        logging.debug('Found unblacklisted items which had no memcache or ' +
                      'datastore entries. Adding to memcache. Items: %s', 
                      unknown_keys)
        models.VolunteerOpportunityStats.add_default_entities_to_memcache(
            unknown_keys)
Пример #2
0
  def get(self):
    """HTTP get method for blacklist actions."""
    action = self.request.get('action')
    if action not in ['blacklist', 'unblacklist']:
      self.error(400)
      return

    key = self.request.get('key')
    if not key:
      self.error(400)
      self.response.out.write("<html><body>sorry: key required</body></html>")
      return


    def generate_blacklist_form(action, key):
      """Return an HTML form for the blacklist action."""
      # TODO: This should obviously be in a template.
      usig = userinfo.get_usig(self.user)
      return ('<form method="POST" action="%s">'
              '<input type="hidden" name="action" value="%s">'
              '<input type="hidden" name="usig" value="%s">'
              '<input type="hidden" name="key" value="%s">'
              '<input type="submit" value="I am sure">'
              '</form>' %
              (self.request.path_url, action, usig, key))

    text = 'Internal error.'
    opp_stats = modelutils.get_by_ids(models.VolunteerOpportunityStats,
                                      [key])
    key_blacklisted = key in opp_stats and opp_stats[key].blacklisted


    if action == "blacklist" and not key_blacklisted:
      text = ('Please confirm blacklisting of key %s: %s' %
              (key, generate_blacklist_form('blacklist', key)))
    elif action == 'unblacklist' and not key_blacklisted:
      text = 'Key %s is not currently blacklisted.' % key
    else:
      text = ('key %s is already blacklisted.<br>'
              'Would you like to restore it?%s' %
              (key, generate_blacklist_form('unblacklist', key)))

    # TODO: Switch this to its own template!
    template_values = {
        'user' : self.user,
        'path' : self.request.path,
        'static_content' : text,
    }
    self.response.out.write(render_template(urls.CONTENT_TEMPLATE,
                                            template_values))
def get_interest_for_opportunities(opp_ids):
  """Get the interest statistics for a set of volunteer opportunities.

  Args:
    opp_ids: list of volunteer opportunity ids.

  Returns:
    Dictionary of volunteer opportunity id: aggregated interest values.
  """
  others_interests = {}
  try:
    # this can time out
    interests = modelutils.get_by_ids(models.VolunteerOpportunityStats, opp_ids)
    for (item_id, interest) in interests.iteritems():
      if interest:
        others_interests[item_id] = getattr(interest, 
                                     models.USER_INTEREST_LIKED)
  except:
    e, v = sys.exc_info()[:2]
    logging.error("view_helper.get_interest_for_opportunities %s %s" % (e, v))

  return others_interests