Made form changes.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Sun, 09 Aug 2009 22:52:45 +0530
changeset 22 0321f984fb16
parent 21 153db75bb515
child 23 91cf6872d853
Made form changes.
app/projrev/models.py
app/projrev/views/proposal.py
app/site-content/js/jquery.chainedSelects.js
app/site-content/proposals/2009/08/09/arm.py
app/templates/projrev/proposal/submit.html
app/urls.py
--- a/app/projrev/models.py	Sun Aug 09 13:04:03 2009 +0530
+++ b/app/projrev/models.py	Sun Aug 09 22:52:45 2009 +0530
@@ -12,6 +12,15 @@
 from django.contrib.auth.models import User
 
 
+def sort_dict(d):
+  """Function to sort dictionary elements.
+  """
+
+  items =  [(v, k) for k, v in d.items()]
+  items.sort()
+  return [(k, v) for v, k in items]
+
+
 class Project(models.Model):
   """Model class for NME funded projects.
   """
@@ -38,6 +47,7 @@
       }
 
   STATE_CHOICES = {
+      "AN": "Andaman & Nicobar", 
       "DN": "Dadra and Nagar Haveli", 
       "DL": "Delhi", 
       "WB": "West Bengal", 
@@ -57,7 +67,6 @@
       "RJ": "Rajasthan", 
       "CH": "Chandigarh", 
       "CG": "Chattisgarh", 
-      "AN": "Andaman & Nicobar", 
       "AP": "Andhra Pradesh", 
       "AS": "Assam", 
       "AR": "Arunachal Pradesh", 
@@ -668,20 +677,21 @@
 
   # Field containing the Line Item to which the project belongs to.
   line_item = models.CharField(max_length=256,
-                               choices=tuple(LINE_ITEM_CHOICES.items()))
+                               choices=sort_dict(LINE_ITEM_CHOICES))
 
   # Field containing the name of the institution working on the
   # project.
   institution = models.CharField(max_length=256)
 
   # Field containing the state to which the institution belongs to.
-  state = models.CharField(max_length=256,
-                           choices=tuple(STATE_CHOICES.items()))
+  state = models.CharField(
+      max_length=256,
+      choices=sorted(STATE_CHOICES.items(), key=lambda (k,v): (v,k)))
 
   # Field containing the district to which the institution belongs
   # to in the state of India.
   district = models.CharField(max_length=256,
-                              choices=tuple(DISTRICT_CHOICES.items()))
+                              choices=sort_dict(DISTRICT_CHOICES))
 
   mobile_num = models.CharField(max_length=20)
 
--- a/app/projrev/views/proposal.py	Sun Aug 09 13:04:03 2009 +0530
+++ b/app/projrev/views/proposal.py	Sun Aug 09 22:52:45 2009 +0530
@@ -12,6 +12,7 @@
 import time
 
 from django.core.urlresolvers import reverse
+from django.http import HttpResponse
 from django.http import HttpResponseRedirect
 from django.shortcuts import render_to_response
 from django.template import RequestContext
@@ -194,6 +195,7 @@
  
   else:
     if not prop_form:
+
       prop_form = projrev_forms.ProposalForm()
 
   context['form'] = prop_form
@@ -360,3 +362,27 @@
   template = 'projrev/proposal/rank.html'
 
   return render_to_response(template, RequestContext(request, context))
+
+def getDistrictsForState(request):
+  """View function that sends the districts for the given state code via AJAXy.
+  """
+  import json
+  get_params = request.GET
+  st_code = get_params['_value']
+  dt_dict = Project.DISTRICT_CHOICES
+
+  # Get the Districts corresponding to the given State code.
+  dt_names = {}
+  count = 1
+  for dt_code in dt_dict:
+    if dt_code[:2] == st_code:
+      dt_names[dt_code] = dt_dict[dt_code]
+      count += 1
+
+  # Sort the List based on District Name.
+  dt_send = [{'': "---------" }]
+  dt_names_sorted = sorted(dt_names.items(), key=lambda (k,v): (v,k))
+  for dt_code, dt_name in dt_names_sorted:
+    dt_send.append({dt_code: dt_name})
+ 
+  return HttpResponse(json.dumps(dt_send))
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/site-content/js/jquery.chainedSelects.js	Sun Aug 09 22:52:45 2009 +0530
@@ -0,0 +1,85 @@
+/**
+*    Chained Selects for jQuery 
+*    Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com  
+*
+*    This program is free software: you can redistribute it and/or modify
+*    it under the terms of the GNU General Public License as published by
+*    the Free Software Foundation, either version 3 of the License, or
+*    (at your option) any later version.
+*
+*    This program is distributed in the hope that it will be useful,
+*    but WITHOUT ANY WARRANTY; without even the implied warranty of
+*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*    GNU General Public License for more details.
+*
+*    You should have received a copy of the GNU General Public License
+*    along with this program.  If not, see http://www.gnu.org/licenses/
+*
+*    
+*   settings = { usePost : true, before:function() {}, after: function() {}, default: null, parameters : { parameter1 : 'value1', parameter2 : 'value2'} }	
+*   if usePost is true, then the form will use POST to pass the parameters to the target, otherwise will use GET
+*   "before" function is called before the ajax request and "after" function is called after the ajax request.
+*   If defaultValue is not null then the specified option will be selected.
+*   You can specify additional parameters to be sent to the the server in settings.parameters.
+*
+*/
+jQuery.fn.chainSelect = function( target, url, settings ) 
+{
+  return this.each( function()
+  {
+	$(this).change( function( ) 
+	{
+		settings = jQuery.extend(
+		{
+			after : null,
+			before : null,
+			usePost : false,
+			defaultValue : null,
+			parameters : {'_id' : $(this).attr('id'), '_name' : $(this).attr('name')}
+        } , settings);
+
+		settings.parameters._value =  $(this).val();
+
+		if (settings.before != null) 
+		{
+			settings.before( target );
+		}
+
+		ajaxCallback = function(data, textStatus) 
+		{
+			$(target).html("");//clear old options
+			data = eval(data);//get json array
+			for (i = 0; i < data.length; i++)//iterate over all options
+			{
+			  for ( key in data[i] )//get key => value
+			  {	
+					$(target).get(0).add(new Option(data[i][key],[key]), document.all ? i : null);
+              }
+			}
+
+			if (settings.defaultValue != null)
+			{
+				$(target).val(settings.defaultValue);//select default value
+			} else
+			{
+				$("option:first", target).attr( "selected", "selected" );//select first option
+			}
+
+			if (settings.after != null) 
+			{
+				settings.after(target);
+			}
+
+			$(target).change();//call next chain
+		};
+
+		if (settings.usePost == true)
+		{
+			$.post( url, settings.parameters, ajaxCallback );
+		} else
+		{
+			$.get( url, settings.parameters, ajaxCallback );
+		}
+	});
+  });
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/site-content/proposals/2009/08/09/arm.py	Sun Aug 09 22:52:45 2009 +0530
@@ -0,0 +1,6 @@
+for i in range(100, 1000):
+    a = i % 10
+    b = (i / 10) % 10
+    c = (i / 100) % 10
+    if i == a ** 3 + b ** 3 + c ** 3:
+        print "Armstrong Number: ", i
--- a/app/templates/projrev/proposal/submit.html	Sun Aug 09 13:04:03 2009 +0530
+++ b/app/templates/projrev/proposal/submit.html	Sun Aug 09 22:52:45 2009 +0530
@@ -1,4 +1,14 @@
 {% extends "projrev/base.html" %}
+{% block scripts %}
+{{ block.super }}
+<script language="JavaScript" type="text/javascript" src="/site-content/js/jquery.chainedSelects.js"></script>
+<script language="JavaScript" type="text/javascript">
+$(function()
+{
+  $('#id_state').chainSelect('#id_district','/proposal/submit/get_dfors');
+});
+</script>
+{% endblock scripts %}
 {% block content %}
 <div class="post">
 			
@@ -15,7 +25,13 @@
 
 	<form enctype="multipart/form-data" method="post" action="">			
 		<p>
-      {{ form.as_p }}
+    {% for field in form %}
+      <p>
+      {{ field.errors }}
+      {{ field.label_tag }}{% if field.field.required %}<span class="special_class">(*)</span>{% endif %}:{{ field }}
+      </p>
+    {% endfor %}
+
       {% if proposal_path %}
         <p>
           <label for="id_proposal">Previous Proposal Document:</label>
--- a/app/urls.py	Sun Aug 09 13:04:03 2009 +0530
+++ b/app/urls.py	Sun Aug 09 22:52:45 2009 +0530
@@ -22,6 +22,8 @@
     (r'^forgot_password/$', 'app.projrev.views.login.forgot_password'),
     (r'^logout/$', 'app.projrev.views.login.logout_view'),
     (r'^proposal/submit/$', 'app.projrev.views.proposal.getMicr'),
+    (r'^proposal/submit/get_dfors$', 
+     'app.projrev.views.proposal.getDistrictsForState'),
     (r'^proposal/submit/create/$', 'app.projrev.views.proposal.submit'),
 #    disabled for now.
 #    (r'^proposal/withdraw/(?P<micr_code>[A-Z]{6}\d{9})/$',