app/projrev/views/proposal.py
changeset 4 8d9da911ed7d
parent 0 c94bd9ae70d2
child 5 88ae12bc6280
equal deleted inserted replaced
3:252a2d9713a5 4:8d9da911ed7d
     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 os
    11 import time
    12 import time
    12 
    13 
       
    14 from django.core.urlresolvers import reverse
       
    15 from django.http import HttpResponseRedirect
    13 from django.shortcuts import render_to_response, get_object_or_404
    16 from django.shortcuts import render_to_response, get_object_or_404
    14 
    17 
    15 from projrev.models import Project
    18 from projrev.models import Project
    16 from projrev.models import Proposal
    19 from projrev.models import Proposal
    17 from projrev.views.helpers import forms as projrev_forms
    20 from projrev.views.helpers import forms as projrev_forms
    18 
    21 
    19 
    22 
    20 def submit(request):
    23 def getMicr(request):
       
    24   """View to get MICR Code from the user.
       
    25   """
       
    26 
       
    27   if request.method == 'POST':
       
    28     post_params = request.POST
       
    29     submit = post_params.get('submit')
       
    30     if submit == 'New Proposal':
       
    31       return HttpResponseRedirect(
       
    32             reverse('app.projrev.views.proposal.submit'))
       
    33     else:
       
    34       micr_code = request.POST.get('micr_code')
       
    35       if micr_code:
       
    36         try:
       
    37           Project.objects.get(micr_code=micr_code)
       
    38         except Project.DoesNotExist:
       
    39           if (submit == 'Edit Proposal for MICR Code' or 
       
    40               submit == 'Withdraw Proposal for MICR Code'):
       
    41             template = 'projrev/proposal/get_micr.html'
       
    42             context = {
       
    43                 'error': True,
       
    44                 }
       
    45             return render_to_response(template, context)
       
    46         else:
       
    47           if submit == 'Edit Proposal for MICR Code':
       
    48             return HttpResponseRedirect(
       
    49                 reverse('app.projrev.views.proposal.submit', 
       
    50                         args=(micr_code, )))
       
    51           elif submit == 'Withdraw Proposal for MICR Code':
       
    52             return HttpResponseRedirect(
       
    53                 reverse('app.projrev.views.proposal.withdraw', 
       
    54                         args=(micr_code, )))
       
    55   else:
       
    56     template = 'projrev/proposal/get_micr.html'
       
    57     context = {}
       
    58 
       
    59     return render_to_response(template, context)
       
    60 
       
    61 def submit(request, micr_code=None):
    21   """View for proposal submission.
    62   """View for proposal submission.
    22   """
    63   """
    23 
    64 
    24   if request.method == 'POST':
    65   if request.method == 'POST':
    25     return submitPost(request)
    66     return submitPost(request, micr_code)
    26   else:
    67   else:
    27     return submitGet(request)
    68     return submitGet(request, micr_code)
    28 
    69 
    29 def submitPost(request):
    70 def submitPost(request, micr_code=None):
    30   """Handles POST request for the submitted proposal form. 
    71   """Handles POST request for the submitted proposal form. 
    31   """
    72   """
    32 
    73 
    33   prop_form = projrev_forms.ProposalForm(request.POST, request.FILES)
    74   prop_form = projrev_forms.ProposalForm(request.POST, request.FILES)
    34 
    75 
    46 
    87 
    47     cleaned_data['line_item'] = Project.getLineItem(cleaned_data['line_item'])
    88     cleaned_data['line_item'] = Project.getLineItem(cleaned_data['line_item'])
    48     cleaned_data['state'] = Project.getState(cleaned_data['state'])
    89     cleaned_data['state'] = Project.getState(cleaned_data['state'])
    49     cleaned_data['district'] = Project.getDistrict(cleaned_data['district'])
    90     cleaned_data['district'] = Project.getDistrict(cleaned_data['district'])
    50 
    91 
       
    92     prop_form.cleaned_data = cleaned_data
       
    93 
    51     # If the form is valid create a new project or update the project
    94     # If the form is valid create a new project or update the project
    52     # if it already exists from the form.
    95     # if it already exists from the form.
    53     project = prop_form.save()
    96     project = prop_form.save()
    54 
    97 
    55     project.status = 'new'
    98     project.status = 'new'
       
    99     project.micr_code = cleaned_data['micr_code']
       
   100     micr_code = cleaned_data['micr_code']
    56 
   101 
    57     project.save()
   102     project.save()
    58 
   103 
    59     # Create a proposal for the project.
   104     # Create a proposal for the project.
    60     proposal = project.proposal_set.create(
   105     proposal = project.proposal_set.create(
    61         document=prop_form.cleaned_data['document'], rev_num = 0)
   106         document=prop_form.cleaned_data['document'], rev_num = 0)
    62 
   107 
    63     proposal.save()
   108     proposal.save()
    64 
   109 
    65   return submitGet(request, project, proposal)
   110     return HttpResponseRedirect(
    66 
   111         reverse('app.projrev.views.proposal.submit', args=(micr_code,)))
    67 def submitGet(request, project=None, proposal=None):
   112 
       
   113   return HttpResponseRedirect('')
       
   114 
       
   115 def submitGet(request, micr_code=None):
    68   """Handles GET request for the submission of proposal form.
   116   """Handles GET request for the submission of proposal form.
    69   """
   117   """
    70 
   118 
    71   context = {}
   119   context = {}
    72 
   120   project = None
    73   if proposal:
   121 
    74     context['document'] = proposal.document
   122   if micr_code:
    75 
   123     project = Project.objects.get(micr_code=micr_code)
    76   if not project:
   124 
       
   125   if project:
       
   126     initial_vals = {
       
   127         'line_item': Project.getLineItemCode(project.line_item),
       
   128         'state': Project.getStateCode(project.state),
       
   129         'district': Project.getDistrictCode(project.district),
       
   130         }
       
   131     prop_form = projrev_forms.ProposalForm(
       
   132         initial=initial_vals, instance=project)
       
   133 
       
   134     proposal_path = str(project.proposal_set.all()[0].document)
       
   135 
       
   136     proposal_name = proposal_path.split('/')[-1]
       
   137 
       
   138     context['proposal_path'] = proposal_path
       
   139     context['proposal_name'] = proposal_name
       
   140 
       
   141     if 'HTTP_REFERER' in request.META:
       
   142       referer = request.META['HTTP_REFERER'].split('/')
       
   143       if referer[-1]:
       
   144         ref = referer[-1]
       
   145       else:
       
   146         ref = referer[-2]
       
   147         
       
   148       if ref == 'create':
       
   149         context['created_now'] = True
       
   150 
       
   151     context['micr_code'] = project.micr_code
       
   152   else:
    77     prop_form = projrev_forms.ProposalForm()
   153     prop_form = projrev_forms.ProposalForm()
    78   else:
       
    79     prop_form = projrev_forms.ProposalForm(instance=project)
       
    80 
   154 
    81   context['form'] = prop_form
   155   context['form'] = prop_form
    82 
   156 
    83   template = 'projrev/proposal/submit.html'
   157   template = 'projrev/proposal/submit.html'
    84 
   158 
    85   return render_to_response(template, context)
   159   return render_to_response(template, context)
    86 
   160 
       
   161 def withdraw(request, micr_code=None):
       
   162   """View Method for withdrawal of proposal.
       
   163   """
       
   164 
       
   165   if micr_code:
       
   166     project = Project.objects.get(micr_code=micr_code)
       
   167     if project:
       
   168       project.status = 'invalid'
       
   169       project.save()
       
   170       context = {
       
   171           'withdrawn': True,
       
   172       }
       
   173 
       
   174   template = 'projrev/proposal/withdraw.html'
       
   175 
       
   176   return render_to_response(template, context)
       
   177 
    87 def review(request, micr_code=None):
   178 def review(request, micr_code=None):
    88   """
   179   """View for reviewing the proposal.
    89   """
   180   """
    90 
   181 
    91   if request.method == 'POST':
   182   if request.method == 'POST':
    92     return reviewPost(request, micr_code)
   183     return reviewPost(request, micr_code)
    93   else:
   184   else: