1

I'm facing a blocking problem: I'm using authomatic 0.1.0 and in adapters.py there's a property params which is declared like this:

@property
def params(self):
    return dict(self.request.REQUEST)

If I let it like that, I get this blocking error:

Erreur #1 : 'WSGIRequest' object has no attribute 'REQUEST'

Googling for that my first guess is that REQUEST is deprecated, and so I tried to rewrite it this way:

@property
def params(self):
    # (!) Olivier Pons ! REQUEST removed
    a = QueryDict('', mutable=True)
    a.update(self.request.GET)
    a.update(self.request.POST)
    return dict(a.iterlists())

But I get this:

Erreur #1 : The returned state "[u'cxxxx']" doesn't match with the stored state!

I dont know what I'm doing wrong, and how to replace REQUEST "properly".

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213

3 Answers3

2

Here's my own version, which may not be the best, but is v2 and v3 python compatible, and solves the main problem of iterlists(): this function always returns an array, even where there's only one item. So authomatic makes a comparison between a string and "an array with one item" and thus fails. My solution: if only an array with one item, just keep this item, not the array.

@property
def params(self):
    # (!) Olivier Pons ! REQUEST removed
    a = QueryDict('', mutable=True)
    a.update(self.request.GET)
    a.update(self.request.POST)
    retour = {}
    for key, value in a.iterlists():
        if len(value) > 1:
            retour[key] = value
        else:
            retour[key] = value[0]
    return retour
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • Thanks Bro, I am new to DJango..Finally this one helped me. But if you pass list value as a parameter this method will not take the full data . So I have done smalle modification make it work .. for key, value in item_value.items(): if len(value) > 1: retour[key]=item_value.getlist(key) else: retour[key] = value[0] – RatheeshTS Feb 05 '21 at 12:09
0

Another possible solution:

@property
def params(self):
    if self.request.method == 'POST':
        return dict(self.request.POST)
    else:
        return dict(self.request.GET)

Update: Previous code is not equivalent to old request.RESQUEST

It can be used QueryDict.dict():

@property
def params(self):
    a = self.request.POST.copy()
    a.update(self.request.GET)
    return(a.dict())
Javier Clavero
  • 445
  • 5
  • 13
  • In this case the behavior is not like the original REQUEST, which should fall back to GET if there was nothing in the POST – Olivier Pons May 04 '16 at 14:50
  • But `REQUEST` is a `MergedDict` which contains the `QueryDict` POST and GET. https://docs.djangoproject.com/en/1.9/releases/1.7/#merging-of-post-and-get-arguments-into-wsgirequest-request so, depending on what's being used will be POST or GET. https://docs.djangoproject.com/en/1.8/ref/request-response/#django.http.HttpRequest.REQUEST – Javier Clavero May 04 '16 at 15:20
  • Let's suppose you do a post with `POST['toto']=12` to a URL like `//test?titi=12`. It's perfectly valid, right? With your solution you can't read `titi`, whereas with the old `REQUEST` behavior you had both `toto` and `titi` set, which is what my code mimics. – Olivier Pons May 04 '16 at 15:38
  • You're right, I haven't taken note this situation and I appreciate the explanation. See the update, is your code simplified. It should work. – Javier Clavero May 04 '16 at 16:43
0

Thanks @Oliver Ponus, I am new to DJango..Finally your answer e helped me. But if you pass list value as a parameter this method will not take the full data . So I have done small modification make it work

for key, value in item_value.items():
  if len(value) > 1:
        retour[key]=item_value.getlist(key) 
  else:
        retour[key] = value[0]
RatheeshTS
  • 411
  • 3
  • 15