app/projrev/views/proposal.py
changeset 13 684540719344
parent 12 aac4944aca52
child 14 9d720dfded68
--- a/app/projrev/views/proposal.py	Fri Aug 07 01:03:56 2009 +0530
+++ b/app/projrev/views/proposal.py	Fri Aug 07 03:04:57 2009 +0530
@@ -86,34 +86,43 @@
   if prop_form.is_valid():
     cleaned_data = prop_form.cleaned_data
 
-    # Generate MICR code
-    cleaned_data['micr_code'] = '%s%s%s%d' % (
-        cleaned_data['state'], cleaned_data['district'],
-        cleaned_data['line_item'], 
-        int(time.time() * 1000) % 1000000000)
+    if micr_code:
+      project = Project.objects.get(micr_code=micr_code)
+      project.line_item = Project.getLineItem(cleaned_data['line_item'])
+      project.institution = cleaned_data['institution']
+      project.state = Project.getState(cleaned_data['state'])
+      project.district = Project.getDistrict(cleaned_data['district'])
+    else:
+      # Generate MICR code
+      cleaned_data['micr_code'] = '%s%s%s%d' % (
+          cleaned_data['state'], cleaned_data['district'],
+          cleaned_data['line_item'], 
+          int(time.time() * 1000) % 1000000000)
 
-    cleaned_data['line_item'] = Project.getLineItem(cleaned_data['line_item'])
-    cleaned_data['state'] = Project.getState(cleaned_data['state'])
-    cleaned_data['district'] = Project.getDistrict(cleaned_data['district'])
+      cleaned_data['line_item'] = Project.getLineItem(cleaned_data['line_item'])
+      cleaned_data['state'] = Project.getState(cleaned_data['state'])
+      cleaned_data['district'] = Project.getDistrict(cleaned_data['district'])
+
+      prop_form.cleaned_data = cleaned_data
 
-    prop_form.cleaned_data = cleaned_data
+      # If the form is valid create a new project or update the project
+      # if it already exists from the form.
+      project = prop_form.save()
 
-    # If the form is valid create a new project or update the project
-    # if it already exists from the form.
-    project = prop_form.save()
+      project.micr_code = cleaned_data['micr_code']
 
     project.status = 'new'
-    project.micr_code = cleaned_data['micr_code']
-    micr_code = cleaned_data['micr_code']
+    micr_code = project.micr_code
 
     project.save()
 
-    # Create a proposal for the project.
-    proposal = project.proposal_set.create(
-        document=prop_form.cleaned_data['document'], 
-        submitted_by=request.user, rev_num = 0)
+    if prop_form.cleaned_data['document']:
+      # Create a proposal for the project.
+      proposal = project.proposal_set.create(
+          document=prop_form.cleaned_data['document'], 
+          submitted_by=request.user, rev_num = 0)
 
-    proposal.save()
+      proposal.save()
 
     return HttpResponseRedirect(
         reverse('app.projrev.views.proposal.submit', args=(micr_code,)))
@@ -139,13 +148,16 @@
     prop_form = projrev_forms.ProposalForm(
         initial=initial_vals, instance=project)
 
-    proposal_path = str(
-        project.proposal_set.all().order_by('-submitted_on')[0].document)
+    proposal = project.proposal_set.all().order_by('-submitted_on')[0]
+
+    if proposal:
+      proposal_path = str(proposal.document)
 
-    proposal_name = proposal_path.split('/')[-1]
+      proposal_name = proposal_path.split('/')[-1]
 
-    context['proposal_path'] = proposal_path
-    context['proposal_name'] = proposal_name
+      context['proposal_path'] = proposal_path
+      context['proposal_name'] = proposal_name
+      context['last_submitted'] = proposal.submitted_on 
 
     if 'HTTP_REFERER' in request.META:
       referer = request.META['HTTP_REFERER'].split('/')
@@ -158,6 +170,12 @@
         context['created_now'] = True
 
     context['micr_code'] = project.micr_code
+
+    reviews = project.review_set.all().order_by('-reviewed_on')
+    if reviews:
+      context['last_reviewed'] = reviews[0].reviewed_on
+      context['nr_reviews'] = len(reviews)
+ 
   else:
     prop_form = projrev_forms.ProposalForm()
 
@@ -221,7 +239,7 @@
   if not micr_code:
     template = 'projrev/proposal/list.html'
     context = {
-        'projects': Project.objects.all(),
+        'projects': Project.objects.all().exclude(status__exact='invalid'),
         'row_url': '/proposal/review/',
         }