app/projrev/views/proposal.py
changeset 23 91cf6872d853
parent 22 0321f984fb16
child 26 97bd3c28c957
equal deleted inserted replaced
22:0321f984fb16 23:91cf6872d853
     6 __authors__ = [
     6 __authors__ = [
     7   '"Madhusudan.C.S" <madhusudancs@gmail.com>',
     7   '"Madhusudan.C.S" <madhusudancs@gmail.com>',
     8 ]
     8 ]
     9 
     9 
    10 
    10 
       
    11 import json
    11 import os
    12 import os
    12 import time
    13 import time
    13 
    14 
    14 from django.core.urlresolvers import reverse
    15 from django.core.urlresolvers import reverse
    15 from django.http import HttpResponse
    16 from django.http import HttpResponse
   364   return render_to_response(template, RequestContext(request, context))
   365   return render_to_response(template, RequestContext(request, context))
   365 
   366 
   366 def getDistrictsForState(request):
   367 def getDistrictsForState(request):
   367   """View function that sends the districts for the given state code via AJAXy.
   368   """View function that sends the districts for the given state code via AJAXy.
   368   """
   369   """
   369   import json
   370 
   370   get_params = request.GET
   371   get_params = request.GET
   371   st_code = get_params['_value']
   372   if get_params:
   372   dt_dict = Project.DISTRICT_CHOICES
   373     st_code = get_params['_value']
   373 
   374     dt_dict = Project.DISTRICT_CHOICES
   374   # Get the Districts corresponding to the given State code.
   375 
   375   dt_names = {}
   376     # Get the Districts corresponding to the given State code.
   376   count = 1
   377     dt_names = {}
   377   for dt_code in dt_dict:
   378     count = 1
   378     if dt_code[:2] == st_code:
   379     for dt_code in dt_dict:
   379       dt_names[dt_code] = dt_dict[dt_code]
   380       if dt_code[:2] == st_code:
   380       count += 1
   381         dt_names[dt_code] = dt_dict[dt_code]
   381 
   382         count += 1
   382   # Sort the List based on District Name.
   383 
   383   dt_send = [{'': "---------" }]
   384     # Sort the List based on District Name.
   384   dt_names_sorted = sorted(dt_names.items(), key=lambda (k,v): (v,k))
   385     dt_send = [{'': "---------" }]
   385   for dt_code, dt_name in dt_names_sorted:
   386     dt_names_sorted = sorted(dt_names.items(), key=lambda (k,v): (v,k))
   386     dt_send.append({dt_code: dt_name})
   387     for dt_code, dt_name in dt_names_sorted:
   387  
   388       dt_send.append({dt_code: dt_name})
   388   return HttpResponse(json.dumps(dt_send))
   389 
       
   390     return HttpResponse(json.dumps(dt_send))
       
   391 
       
   392   return HttpResponse(None)