views:

232

answers:

2

Can I use a middleware to preserve several user selected choices in between page requests?

I have several values, namely vehicle year, make, model, series, style, color, and transmission. I want to have the user being able to select a choice while keeping the previously selected choice active. I do not want to use sessions for this as I want the URL to be bookmarkable.

I was thinking of something like:

def get_choices(self):
   try:
      return self.REQUEST["year"]
   except (KeyError, ValueError, TypeError):
      return 1

class ChoicesMiddleware(object):
  def process_request(self, request):
      .....

I'm also not sure how to return all the choices under get_choices().

EDIT 1

def get_choices(self):
    user_choices = {}
    for key in ["year", "model", "make", "series", "style"]:
        user_choices[key] = self.REQUEST.get(key)

    return user_choices

class ChoicesMiddleware(object):
   def process_request(self, request):
      return get_choices(self)

EDIT 2 My URLConf is as below:

(r'^inventory/(?P<year>\d{4})(?P<make>[-\w\s]+)
   (?P<model>[-\w\s]+)(?P<series>[-\w\s]+)(?P<body>[-\w\s]+)
   (?P<exterior>[-\w\s]+)(?P<interior>[-\w\s]+)
   (?P<transmission>[-\w\s]+)$', 'inventory'),

Then the view is as below:

def inventory(request, page_by=None, year=None, make=None, 
    model=None, series=None, body=None, interior=None, exterior=None, 
    transmission=None):

  #Initialize empty variable list.
  kwargs = {}

  if "year" in request.GET:
    year = request.REQUEST["year"]
    kwargs['common_vehicle__year__year__exact'] = year
  ....The rest of the vars are populated in the same way.
A: 

Do you want to automatically add the user choices as GET parameters to the URL?

I do not think you would be able to add GET request parameters to a URL via middleware.

Mayuresh
A: 

You can store them in GET, no problem there. Return via dict. I didn't understand the part about preserving user's choice - you want to have several options for year, for example? Then you need arrays in GET, not values. But for values its simple:

def get_choices(self):
    user_choices = {}
    for key in ["year", "model", "maker"]:
        user_choices[key] = self.REQUEST.get(key)
    return user_choices
kibitzer
It's a view for narrowing down a user's selection from a large database of vehicles...so the user starts with one choice, say year, then the in next request, if the user selects a specific body style, the query will combine the previously selected year with the chosen body style...and so on...I think you get what I mean. Let me try your answer first though
Stephen
I've been trying out the code above...it's not working out for me. See my edit for how I've done it...ideally I want it to work out like how django-pagination, for example, preserves the paginate_by value
Stephen
I don't see any difference between your code and my dict population :) So what is not working?
kibitzer
That's what I'm trying to figure out...no luck so far. I've included the middleware in my settings.py. Let me add some code above and maybe you can spot my mistake
Stephen
kibitzer