def OpenIDFinish(request, default_success_url='/'):
  response = django.http.HttpResponse()
  if request.method not in ('GET', 'POST'):
    return django.http.HttpResponseNotAllowed(['GET', 'POST'])
  else:
    args = args_to_dict(request.GET)
    assert type(args) is dict
    if request.method == 'POST':
      args.update(args_to_dict(request.POST))
    url = 'http://'+request.META['HTTP_HOST']+django.core.urlresolvers.reverse('openidgae.views.OpenIDFinish')
    session = openidgae.get_session(request, response)
    s = {}
    if session.openid_stuff:
      try:
        import pickle
        s = pickle.loads(str(session.openid_stuff))
      except:
        session.openid_stuff = None

    session.put()

    c = Consumer(s, get_store())
    auth_response = c.complete(args, url)

    sreg_response = {}
    ax_items = {}
    if auth_response.status == openid.consumer.consumer.SUCCESS:
      from openid.extensions import sreg
      sreg_response = sreg.SRegResponse.fromSuccessResponse(auth_response)
      sreg_response = dict(sreg_response.iteritems())
      logging.debug("sreg_response: %r" % sreg_response)

      from openid.extensions import ax
      ax_response = ax.FetchResponse.fromSuccessResponse(auth_response)
      logging.debug("ax_response: %r" % ax_response)
      if ax_response:
        SHORTNAMES = (
          ('email', 'http://schema.openid.net/contact/email'),
          ('firstname', 'http://axschema.org/namePerson/first'),
          ('lastname', 'http://axschema.org/namePerson/last'),
          ('language', 'http://axschema.org/pref/language'),
          ('country', 'http://axschema.org/contact/country/home'),
        )
        for short, long in SHORTNAMES:
          try:
            ax_items[short] = ax_response.get(long)
          except __HOLE__, e:
            pass
        logging.debug("ax_items: %r" % ax_items)

      openid_url = auth_response.getDisplayIdentifier()

      import models
      persons = models.Person.gql('WHERE openid = :1', openid_url)
      if persons.count() == 0:
        p = models.Person()
        p.openid = openid_url
        p.ax_dict().update(ax_items)
        p.sreg_dict().update(sreg_response)
        p.put()
      else:
        p = persons[0]
        changed = False
        for key in sreg_response:
          if not p.sreg_dict().has_key(key) or \
              p.sreg_dict()[key] != sreg_response[key]:
            logging.debug("Setting sreg %s" % key)
            p.sreg_dict()[key] = sreg_response[key]
            changed = True
        for key in ax_items:
          if not p.ax_dict().has_key(key) or \
              p.ax_dict()[key] != ax_items[key]:
            logging.info("Setting ax %s" % key)
            p.ax_dict()[key] = ax_items[key]
            changed = True
        if changed:
          p.put()

      s = openidgae.get_session(request, response)
      s.person = p.key()
      request.openidgae_logged_in_person = p

      s.put()

      continueUrl = get_continue_url(request, default_success_url)

      return django.http.HttpResponseRedirect(continueUrl)

    else:
      return show_main_page(request, 'OpenID verification failed :(')
dataset/ETHPy150Open CollabQ/CollabQ/openidgae/views.py/OpenIDFinish