Exemplo n.º 1
0
def call_api_from_request(request, api_call):
  """Call an API function 'api_call' if it's present in the request parameters.

  The first parameter to the API call is always the logged-in user.

  The rest of the parameters may come in two forms:
    api_call_name=first_param& ... rest of params
    or
    api_call_name=& ... rest of params

    rest_of_params is always turned into Python keyword arguments.

    If the api_call_name has a value, that is turned into Python positional
    params.
  RETURNS:
    (False, None) if it isn't or the call throws an exception,
    (True, return value from call) otherwise.
  """
  # TODO(termie): make this only accept POST once we update javascript
  #               to turn the links into POSTs
  for request_dict in (request.POST, request.GET):
    if api_call in request_dict:
      # debug
      logging.info(">>> CALL: %s", str(api_call))
      # end debug
      call = getattr(api, api_call)
      try:
        validate.nonce(request, api_call)
        confirm_msg = messages.confirmation(api_call)
        # debug
        # logging.info(">>> MESSAGE : %s", str(confirm_msg))
        # end debug
 
        if not confirm_msg is None:
          validate.confirm_dangerous(
              request, messages.confirmation(api_call))
        kwparams = util.query_dict_to_keywords(request_dict)
        # debug
        logging.info(">>> KWPARAMS: %s", str(kwparams))
        # end debug
 
        if '' in kwparams:
          del kwparams['']
        first_param = kwparams.pop(api_call, '')
        params = list()
        if len(first_param):
          params = (first_param,)
        validate.nonce(request, api_call)
        kwparams.pop('_nonce')
        kwparams.pop('confirm', None)
        kwparams.pop('redirect_to', None)
        # debug
        logging.info("##### CALL: %s", str((params, kwparams)))
        # end debug

        return (True, call(request.user, *params, **kwparams))
      except:
        exception.handle_exception(request)
  return (False, None)
Exemplo n.º 2
0
def oauth_request_from_django_request(request):
    url = request.build_absolute_uri()
    url = url.split("?")[0]
    params = util.query_dict_to_keywords(request.REQUEST)

    post_data = request.method == "post" and request.raw_post_data or ""
    headers = {}
    if 'HTTP_AUTHORIZATION' in request.META:
        headers['Authorization'] = request.META['HTTP_AUTHORIZATION']

    # TODO(termie): fix the oauth library to not use this call sig
    oauth_request = oauth.OAuthRequest.from_request(request.method,
                                                    url,
                                                    headers=headers,
                                                    query_string=post_data,
                                                    parameters=params)

    return oauth_request
Exemplo n.º 3
0
def call_api_from_request(request, api_call):
    """Call an API function 'api_call' if it's present in the request parameters.

  The first parameter to the API call is always the logged-in user.

  The rest of the parameters may come in two forms:
    api_call_name=first_param& ... rest of params
    or
    api_call_name=& ... rest of params

    rest_of_params is always turned into Python keyword arguments.

    If the api_call_name has a value, that is turned into Python positional
    params.
  RETURNS:
    (False, None) if it isn't or the call throws an exception,
    (True, return value from call) otherwise.
  """
    # TODO(termie): make this only accept POST once we update javascript
    #               to turn the links into POSTs
    for request_dict in (request.POST, request.GET):
        if api_call in request_dict:
            call = getattr(api, api_call)
            try:
                validate.nonce(request, api_call)
                confirm_msg = messages.confirmation(api_call)
                if not confirm_msg is None:
                    validate.confirm_dangerous(request,
                                               messages.confirmation(api_call))
                kwparams = util.query_dict_to_keywords(request_dict)
                if '' in kwparams:
                    del kwparams['']
                first_param = kwparams.pop(api_call, '')
                params = list()
                if len(first_param):
                    params = (first_param, )
                validate.nonce(request, api_call)
                kwparams.pop('_nonce')
                kwparams.pop('confirm', None)
                kwparams.pop('redirect_to', None)
                return (True, call(request.user, *params, **kwparams))
            except:
                exception.handle_exception(request)
    return (False, None)
Exemplo n.º 4
0
def oauth_request_from_django_request(request):
  url = request.build_absolute_uri()
  url = url.split("?")[0]
  params = util.query_dict_to_keywords(request.REQUEST)
  
  post_data = request.method == "post" and request.raw_post_data or ""
  headers = {}
  if 'HTTP_AUTHORIZATION' in request.META:
    headers['Authorization'] = request.META['HTTP_AUTHORIZATION']

  # TODO(termie): fix the oauth library to not use this call sig
  oauth_request = oauth.OAuthRequest.from_request(
      request.method, 
      url,
      headers=headers,
      query_string=post_data, 
      parameters=params)
  
  return oauth_request
Exemplo n.º 5
0
def join_join(request):
  if request.user:
    raise exception.AlreadyLoggedInException()

  redirect_to = request.REQUEST.get('redirect_to', '/')

  # get the submitted vars
  nick = request.REQUEST.get('nick', '')
  first_name = request.REQUEST.get('first_name', '')
  last_name = request.REQUEST.get('last_name', '')
  email = request.REQUEST.get('email', '')
  password = request.REQUEST.get('password', '')
  confirm = request.REQUEST.get('confirm', '')
  homepage = request.REQUEST.get('homepage', '')
  hide = request.REQUEST.get('hide', '')

  if request.POST:
    try:
      # TODO validate
      params = util.query_dict_to_keywords(request.POST)

      if hide:
        params['privacy'] = 2
 
      validate.email(email)
      if not mail.is_allowed_to_send_email_to(email):
        raise exception.ValidationError("Cannot send email to that address")

      # TODO start transaction
      if api.actor_lookup_email(api.ROOT, email):
        raise exception.ValidationError(
            'That email address is already associated with a member.')
    
      actor_ref = api.user_create(api.ROOT, **params)
      actor_ref.access_level = "delete"

      api.post(actor_ref, 
               nick=actor_ref.nick, 
               message='Joined %s!' % (settings.SITE_NAME),
               icon='jaiku-new-user')

      # send off email confirmation
      api.activation_request_email(actor_ref, actor_ref.nick, email)
      
      # TODO end transaction
  
      welcome_url = util.qsa('/welcome', {'redirect_to': redirect_to})

      # NOTE: does not provide a flash message
      response = http.HttpResponseRedirect(welcome_url)
      user.set_user_cookie(response, actor_ref)
      return response
    except:
      exception.handle_exception(request)

  # for legal section
  legal_component = component.include('legal', 'dummy_legal')
  legal_html = legal_component.embed_join()

  # for sidebar
  sidebar_green_top = True

  area = "join"
  c = template.RequestContext(request, locals())

  t = loader.get_template('join/templates/join.html')
  return http.HttpResponse(t.render(c))
Exemplo n.º 6
0
def join_join(request):
  if request.user:
    raise exception.AlreadyLoggedInException()

  redirect_to = request.REQUEST.get('redirect_to', '/')

  account_types = api.get_config_values(api.ROOT, 'account_type')

  # get the submitted vars
  nick = request.REQUEST.get('nick', '');
  first_name = request.REQUEST.get('first_name', '');
  last_name = request.REQUEST.get('last_name', '');
  email = request.REQUEST.get('email', '');
  password = request.REQUEST.get('password', '');
  confirm = request.REQUEST.get('confirm', '');
  hide = request.REQUEST.get('hide', '');
  country_tag = request.REQUEST.get('country_tag', '')

  if request.POST:
    try:
      # TODO validate
      params = util.query_dict_to_keywords(request.POST)

      if hide:
        params['privacy'] = 2

      # XXX: Check if the data come from a openid account
      # @author: [email protected]
      fromopenid = request.POST.get('fromopenid', False) and True
      if fromopenid:
        try:
          person = openidgae.get_current_person(request, http.HttpResponse())
        except:
          raise exception.ServiceError
        
        email = person.get_email()
        if email == params['email']:
          params['password'] = util.generate_password()
        else:
          raise exception.ServiceError

      # ENDXXX

      validate.email(email)
      if not mail.is_allowed_to_send_email_to(email):
        raise exception.ValidationError("Cannot send email to that address")

      # TODO start transaction
      if api.actor_lookup_email(api.ROOT, email):
        raise exception.ValidationError(
            'That email address is already associated with a member.')
      
      actor_ref = api.user_create(api.ROOT, **params)
      actor_ref.access_level = "delete"

      api.post(actor_ref, 
               nick=actor_ref.nick, 
               message='Joined %s!' % (util.get_metadata('SITE_NAME')))
      if fromopenid:
        api.email_associate(api.ROOT, actor_ref.nick, email)
      else:
        # send off email confirmation
        api.activation_request_email(actor_ref, actor_ref.nick, email)

      logging.info('setting firsttime_%s from register page' % actor_ref.nick)
      memcache.client.set('firsttime_%s' % nick, True)
      # TODO end transaction
      welcome_url = util.qsa('/', {'redirect_to': redirect_to})

      # NOTE: does not provide a flash message
      response = http.HttpResponseRedirect(welcome_url)
      user.set_user_cookie(response, actor_ref)
      return response
    except:
      exception.handle_exception(request)

  # for legal section
  legal_component = component.include('legal', 'dummy_legal')
  legal_html = legal_component.embed_join()
  
  # for sidebar
  sidebar_green_top = True

  area = "join"
  c = template.RequestContext(request, locals())

  t = loader.get_template('join/templates/join.html')
  return http.HttpResponse(t.render(c))
Exemplo n.º 7
0
def get_method_kwargs(request):
  args = util.query_dict_to_keywords(request.REQUEST)
  return get_non_oauth_params(args)
Exemplo n.º 8
0
def get_method_kwargs(request):
    args = util.query_dict_to_keywords(request.REQUEST)
    return get_non_oauth_params(args)
Exemplo n.º 9
0
def join_join(request):
    if request.user:
        raise exception.AlreadyLoggedInException()

    redirect_to = get_clean_redirect(request)

    # get the submitted vars
    nick = request.REQUEST.get('nick', '')
    first_name = request.REQUEST.get('first_name', '')
    last_name = request.REQUEST.get('last_name', '')
    email = request.REQUEST.get('email', '')
    password = request.REQUEST.get('password', '')
    confirm = request.REQUEST.get('confirm', '')
    homepage = request.REQUEST.get('homepage', '')
    hide = request.REQUEST.get('hide', '')

    if request.POST:
        try:
            # TODO validate
            params = util.query_dict_to_keywords(request.POST)

            if hide:
                params['privacy'] = 2

            validate.email(email)
            if not mail.is_allowed_to_send_email_to(email):
                raise exception.ValidationError(
                    "Cannot send email to that address")

            # TODO start transaction
            if api.actor_lookup_email(api.ROOT, email):
                raise exception.ValidationError(
                    'That email address is already associated with a member.')

            actor_ref = api.user_create(api.ROOT, **params)
            actor_ref.access_level = "delete"

            api.post(actor_ref,
                     nick=actor_ref.nick,
                     message='Joined %s!' % (settings.SITE_NAME),
                     icon='jaiku-new-user')

            # send off email confirmation
            api.activation_request_email(actor_ref, actor_ref.nick, email)

            # TODO end transaction

            welcome_url = util.qsa('/welcome', {'redirect_to': redirect_to})

            # NOTE: does not provide a flash message
            response = http.HttpResponseRedirect(welcome_url)
            user.set_user_cookie(response, actor_ref)
            return response
        except:
            exception.handle_exception(request)

    # for legal section
    legal_component = component.include('legal', 'dummy_legal')
    legal_html = legal_component.embed_join()

    # for sidebar
    sidebar_green_top = True

    area = "join"
    c = template.RequestContext(request, locals())

    t = loader.get_template('join/templates/join.html')
    return http.HttpResponse(t.render(c))