# (c) 2012 Urban Airship and Contributors

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from mithril.forms import WhitelistForm
from mithril.middleware import WhitelistMiddleware


class WhitelistEditor(object):

    template = 'mithril/whitelist_edit.html'
    form_class = WhitelistForm

    def __init__(self, obj_from_request):
        self.obj_from_request = obj_from_request

    def __call__(self, request, *args, **kwargs):
        whitelist = self.obj_from_request(request, *args, **kwargs)
        strategy = WhitelistMiddleware().get_strategy()
        current_ip = strategy.get_ip_from_request(request)

        form_args, form_kwargs = (current_ip, whitelist), {
            'range_form_class':getattr(self, 'range_form_class', None)
        }
        if request.method == 'POST':
            form_args += (request.POST,) 
        form = self.form_class(*form_args, **form_kwargs)
        if form.is_valid():
            self.save_form(request, form, *args, **kwargs)
            return HttpResponseRedirect('.')

        return self.respond(request, form, whitelist, *args, **kwargs)

    def save_form(self, request, form, *args, **kwargs):
        return form.save()

    def respond(self, request, form, whitelist, *args, **kwargs):
        return render_to_response(self.template, {
            'form':form,
            'whitelist':whitelist
        }, context_instance=RequestContext(request))

@@ -721,21 +721,8 @@
 args
-, form_kwargs
  = (
@@ -743,19 +743,41 @@
 itelist)
-,
+%0A        form_kwargs =
  %7B
+%7D
 %0A       
@@ -777,19 +777,32 @@
         
-   
+if hasattr(self,
  'range_
@@ -816,58 +816,82 @@
 ass'
-:getattr(self, 'range_form_class', None)%0A        %7D
+):%0A            form_kwargs = %7B'range_form_class': self.range_form_class%7D%0A 
 %0A   

Fix regression in tests due to removal of lazy evaluation