app/projrev/views/proposal.py
changeset 0 c94bd9ae70d2
child 4 8d9da911ed7d
equal deleted inserted replaced
-1:000000000000 0:c94bd9ae70d2
       
     1 """This module contains the views for the project's proposal 
       
     2 funded by NME through ICT.
       
     3 """
       
     4 
       
     5 
       
     6 __authors__ = [
       
     7   '"Madhusudan.C.S" <madhusudancs@gmail.com>',
       
     8 ]
       
     9 
       
    10 
       
    11 import time
       
    12 
       
    13 from django.shortcuts import render_to_response, get_object_or_404
       
    14 
       
    15 from projrev.models import Project
       
    16 from projrev.models import Proposal
       
    17 from projrev.views.helpers import forms as projrev_forms
       
    18 
       
    19 
       
    20 def submit(request):
       
    21   """View for proposal submission.
       
    22   """
       
    23 
       
    24   if request.method == 'POST':
       
    25     return submitPost(request)
       
    26   else:
       
    27     return submitGet(request)
       
    28 
       
    29 def submitPost(request):
       
    30   """Handles POST request for the submitted proposal form. 
       
    31   """
       
    32 
       
    33   prop_form = projrev_forms.ProposalForm(request.POST, request.FILES)
       
    34 
       
    35   project = None
       
    36   proposal = None
       
    37 
       
    38   if prop_form.is_valid():
       
    39     cleaned_data = prop_form.cleaned_data
       
    40 
       
    41     # Generate MICR code
       
    42     cleaned_data['micr_code'] = '%s%s%s%d' % (
       
    43         cleaned_data['state'], cleaned_data['district'],
       
    44         cleaned_data['line_item'], 
       
    45         int(time.time() * 1000) % 1000000000)
       
    46 
       
    47     cleaned_data['line_item'] = Project.getLineItem(cleaned_data['line_item'])
       
    48     cleaned_data['state'] = Project.getState(cleaned_data['state'])
       
    49     cleaned_data['district'] = Project.getDistrict(cleaned_data['district'])
       
    50 
       
    51     # If the form is valid create a new project or update the project
       
    52     # if it already exists from the form.
       
    53     project = prop_form.save()
       
    54 
       
    55     project.status = 'new'
       
    56 
       
    57     project.save()
       
    58 
       
    59     # Create a proposal for the project.
       
    60     proposal = project.proposal_set.create(
       
    61         document=prop_form.cleaned_data['document'], rev_num = 0)
       
    62 
       
    63     proposal.save()
       
    64 
       
    65   return submitGet(request, project, proposal)
       
    66 
       
    67 def submitGet(request, project=None, proposal=None):
       
    68   """Handles GET request for the submission of proposal form.
       
    69   """
       
    70 
       
    71   context = {}
       
    72 
       
    73   if proposal:
       
    74     context['document'] = proposal.document
       
    75 
       
    76   if not project:
       
    77     prop_form = projrev_forms.ProposalForm()
       
    78   else:
       
    79     prop_form = projrev_forms.ProposalForm(instance=project)
       
    80 
       
    81   context['form'] = prop_form
       
    82 
       
    83   template = 'projrev/proposal/submit.html'
       
    84 
       
    85   return render_to_response(template, context)
       
    86 
       
    87 def review(request, micr_code=None):
       
    88   """
       
    89   """
       
    90 
       
    91   if request.method == 'POST':
       
    92     return reviewPost(request, micr_code)
       
    93   else:
       
    94     return reviewGet(request, micr_code)
       
    95 
       
    96 def reviewPost(request, micr_code=None):
       
    97   """
       
    98   """
       
    99 
       
   100   rev_form = projrev_forms.ReviewForm(request.POST)
       
   101 
       
   102   if rev_form.is_valid():
       
   103     cleaned_data = rev_form.cleaned_data
       
   104 
       
   105     cleaned_data['project'] = Project.objects.get(micr_code=micr_code)
       
   106 
       
   107     # If the form is valid create a new project or update the project
       
   108     # if it already exists from the form.
       
   109     review = rev_form.save()
       
   110 
       
   111   return reviewGet(request, micr_code, rev_form)
       
   112 
       
   113 def reviewGet(request, micr_code=None, rev_form=None):
       
   114   """
       
   115   """
       
   116 
       
   117   if not micr_code:
       
   118     template = 'projrev/proposal/list.html'
       
   119     context = {
       
   120         'projects': Project.objects.all(),
       
   121         'row_url': '/proposal/review/',
       
   122         }
       
   123 
       
   124     return render_to_response(template, context)
       
   125 
       
   126   if not rev_form:
       
   127     rev_form = projrev_forms.ReviewForm()
       
   128 
       
   129   proposal_path = str(Project.objects.get(
       
   130           micr_code=micr_code).proposal_set.all()[0].document)
       
   131 
       
   132   proposal_name = proposal_path.split('/')[-1]
       
   133 
       
   134   context = {
       
   135       'form': rev_form,
       
   136       'project': Project.objects.get(micr_code=micr_code),
       
   137       'proposal_path': proposal_path,
       
   138       'proposal_name': proposal_name,
       
   139       }
       
   140 
       
   141   template = 'projrev/proposal/review.html'
       
   142 
       
   143   return render_to_response(template, context)
       
   144 
       
   145 def rank(request, micr_code=None):
       
   146   """
       
   147   """
       
   148 
       
   149   if request.method == 'POST':
       
   150     return rankPost(request, micr_code)
       
   151   else:
       
   152     return rankGet(request, micr_code)
       
   153 
       
   154 def rankPost(request, micr_code=None):
       
   155   """
       
   156   """
       
   157 
       
   158   return rankGet(request, micr_code)
       
   159 
       
   160 def rankGet(request, micr_code=None):
       
   161   """
       
   162   """
       
   163 
       
   164   if not micr_code:
       
   165     template = 'projrev/proposal/list.html'
       
   166     context = {
       
   167         'projects': Project.objects.all(),
       
   168         'row_url': '/proposal/rank/',
       
   169         }
       
   170 
       
   171     return render_to_response(template, context)
       
   172 
       
   173   projects = Project.objects.get(micr_code=micr_code)
       
   174 
       
   175   proposal_path = str(projects.proposal_set.all()[0].document)
       
   176 
       
   177   proposal_name = proposal_path.split('/')[-1]
       
   178 
       
   179   reviews = projects.review_set.all()
       
   180 
       
   181   review_score = [0] * 9
       
   182   for review in reviews:
       
   183     review_score[0] += review.attribute1
       
   184     review_score[1] += review.attribute2
       
   185     review_score[2] += review.attribute3
       
   186     review_score[3] += review.attribute4
       
   187     review_score[4] += review.attribute5
       
   188     review_score[5] += review.attribute6
       
   189     review_score[6] += review.attribute7
       
   190     review_score[7] += review.attribute8
       
   191     review_score[8] += review.attribute9
       
   192 
       
   193   total_score = sum(review_score)
       
   194 
       
   195   context = {
       
   196       'project': projects,
       
   197       'proposal_path': proposal_path,
       
   198       'proposal_name': proposal_name,
       
   199       'review_score': review_score,
       
   200       'total_score': total_score,
       
   201       }
       
   202 
       
   203   template = 'projrev/proposal/rank.html'
       
   204 
       
   205   return render_to_response(template, context)