Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

change in examples/djopenid/util.py normalDict function #6

Closed
ashok-raavi opened this issue Jun 25, 2010 · 1 comment
Closed

change in examples/djopenid/util.py normalDict function #6

ashok-raavi opened this issue Jun 25, 2010 · 1 comment

Comments

@ashok-raavi
Copy link

as per the django docs, http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict.iteritems

QueryDict.iteritems will returns the last value if the key has more than one value,

the return value of normalDict function in examples/djopenid/util.py has to be
return dict((k, v) for k, v in request_data.iteritems())

@ziima
Copy link
Contributor

ziima commented Apr 27, 2011

It should be even bit more complicated to fulfill conditions from protocol.

def normal_dict(request):
    """
    Converts a django request arguments (MultiValueDict request.GET, request.POST) into a standard python dict 
    whose values are the last value from each of the MultiValueDict's value lists. This avoids the OpenID library's 
    refusal to deal with dicts whose values are lists, because in OpenID, each key in the query arg set can have at most one value.
    Also converts keys from unicode to string, so they can be used as arguments in functions.

    If request method is POST silently throw out OpenID-like arguments from GET see
    U{http://openid.net/specs/openid-authentication-2_0.html#rfc.section.4.1.2}

    @param request: Django request object 
    @type request: C{django.http.HttpRequest}
    @return: Dictionary with request arguments
    @rtype: C{dict}
    """
    if request.method == 'POST':
        normal = {}
        #take GET first, override them with POST
        for key, value in request.GET.items():
            #all openid arguments must be in POST
            try:
                prefix, rest = key.split('.', 1)
                if prefix != 'openid':
                    normal[str(key)] = value
            except ValueError: #no prefix
                normal[str(key)] = value

        for key, value in request.POST.items():
            normal[str(key)] = value
        return normal
    else:
        #if not POST, all arguments are GET
        normal = {}
        for key, value in request.GET.items():
            normal[str(key)] = value
        return normal

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants