app/projrev/views/proposal.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Mon, 10 Aug 2009 00:06:31 +0530
changeset 24 7257b66a6766
parent 23 91cf6872d853
child 26 97bd3c28c957
permissions -rw-r--r--
Purr implemented for submit form.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
"""This module contains the views for the project's proposal 
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
funded by NME through ICT.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
"""
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
__authors__ = [
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
  '"Madhusudan.C.S" <madhusudancs@gmail.com>',
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
]
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
23
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
    11
import json
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    12
import os
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
import time
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    15
from django.core.urlresolvers import reverse
22
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
    16
from django.http import HttpResponse
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    17
from django.http import HttpResponseRedirect
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    18
from django.shortcuts import render_to_response
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    19
from django.template import RequestContext
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
from projrev.models import Project
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
from projrev.models import Proposal
5
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
    23
from projrev.views.helpers import access 
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
from projrev.views.helpers import forms as projrev_forms
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
5
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
    26
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    27
@access.register('proposer')
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    28
@access.checkAccess
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    29
def getMicr(request):
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    30
  """View to get MICR Code from the user.
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    31
  """
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    32
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    33
  if request.method == 'POST':
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    34
    post_params = request.POST
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    35
    submit = post_params.get('submit')
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    36
    if submit == 'New Proposal':
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    37
      return HttpResponseRedirect(
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    38
            reverse('app.projrev.views.proposal.submit'))
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    39
    else:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    40
      micr_code = request.POST.get('micr_code')
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    41
      if micr_code:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    42
        try:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    43
          Project.objects.get(micr_code=micr_code)
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    44
        except Project.DoesNotExist:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    45
          if (submit == 'Edit Proposal for MICR Code' or 
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    46
              submit == 'Withdraw Proposal for MICR Code'):
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    47
            template = 'projrev/proposal/get_micr.html'
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    48
            context = {
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    49
                'error': True,
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    50
                }
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    51
            return render_to_response(template,
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    52
                                      RequestContext(request, context))
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    53
        else:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    54
          if submit == 'Edit Proposal for MICR Code':
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    55
            return HttpResponseRedirect(
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    56
                reverse('app.projrev.views.proposal.submit', 
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    57
                        args=(micr_code, )))
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    58
          elif submit == 'Withdraw Proposal for MICR Code':
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    59
            return HttpResponseRedirect(
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    60
                reverse('app.projrev.views.proposal.withdraw', 
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    61
                        args=(micr_code, )))
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    62
  else:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    63
    template = 'projrev/proposal/get_micr.html'
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    64
    context = {}
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    65
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    66
    return render_to_response(template, RequestContext(request, context))
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    67
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    68
@access.register('proposer')
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    69
@access.checkAccess
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    70
def submit(request, micr_code=None):
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
  """View for proposal submission.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
  if request.method == 'POST':
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    75
    return submitPost(request, micr_code)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
  else:
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    77
    return submitGet(request, micr_code)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
    79
def submitPost(request, micr_code=None):
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
  """Handles POST request for the submitted proposal form. 
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
  prop_form = projrev_forms.ProposalForm(request.POST, request.FILES)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
  project = None
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
  proposal = None
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
  if prop_form.is_valid():
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
    cleaned_data = prop_form.cleaned_data
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    91
    if micr_code:
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    92
      project = Project.objects.get(micr_code=micr_code)
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    93
      project.line_item = Project.getLineItem(cleaned_data['line_item'])
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    94
      project.institution = cleaned_data['institution']
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    95
      project.state = Project.getState(cleaned_data['state'])
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    96
      project.district = Project.getDistrict(cleaned_data['district'])
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    97
    else:
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    98
      # Generate MICR code
21
153db75bb515 Changed data models to all new data.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 19
diff changeset
    99
      cleaned_data['micr_code'] = '%s%s%d' % (
153db75bb515 Changed data models to all new data.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 19
diff changeset
   100
          cleaned_data['district'], cleaned_data['line_item'], 
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   101
          int(time.time() * 1000) % 1000000000)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   103
      cleaned_data['line_item'] = Project.getLineItem(cleaned_data['line_item'])
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   104
      cleaned_data['state'] = Project.getState(cleaned_data['state'])
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   105
      cleaned_data['district'] = Project.getDistrict(cleaned_data['district'])
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   106
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   107
      prop_form.cleaned_data = cleaned_data
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   109
      # If the form is valid create a new project or update the project
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   110
      # if it already exists from the form.
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   111
      project = prop_form.save()
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   112
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   113
      project.micr_code = cleaned_data['micr_code']
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
18
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   115
      subject = "[Sakshath] MICR Code of the new proposal submitted"
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   116
      message = """Hi,
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   117
   We have received a new proposal for National Mission on education from your, 
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   118
email address "%s", to the http://sakshath.ac.in portal. The MICR Code of the
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   119
submitted proposal is: 
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   120
  MICR Code: %s
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   121
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   122
Please remember this code for all future references.
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   123
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   124
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   125
--  
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   126
Regards,
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   127
Saksath admin""" % (request.user.username, project.micr_code)
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   128
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   129
      request.user.email_user(subject=subject, message=message)
05b9e60e6a10 Changed CSS and removed Withdraw proposal.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 16
diff changeset
   130
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
    project.status = 'new'
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   132
    micr_code = project.micr_code
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   133
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
    project.save()
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   136
    if prop_form.cleaned_data['document']:
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   137
      # Create a proposal for the project.
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   138
      proposal = project.proposal_set.create(
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   139
          document=prop_form.cleaned_data['document'], 
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   140
          submitted_by=request.user, rev_num = 0)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   142
      proposal.save()
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   144
    return HttpResponseRedirect(
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   145
        reverse('app.projrev.views.proposal.submit', args=(micr_code,)))
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   146
19
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   147
  return submitGet(request, micr_code, prop_form)
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   148
19
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   149
def submitGet(request, micr_code=None, prop_form=None):
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
  """Handles GET request for the submission of proposal form.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
  context = {}
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   154
  project = None
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   156
  if micr_code:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   157
    project = Project.objects.get(micr_code=micr_code)
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   158
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   159
  if project:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   160
    initial_vals = {
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   161
        'line_item': Project.getLineItemCode(project.line_item),
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   162
        'state': Project.getStateCode(project.state),
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   163
        'district': Project.getDistrictCode(project.district),
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   164
        }
19
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   165
    if not prop_form:
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   166
      prop_form = projrev_forms.ProposalForm(
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   167
          initial=initial_vals, instance=project)
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   168
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   169
    proposal = project.proposal_set.all().order_by('-submitted_on')[0]
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   170
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   171
    if proposal:
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   172
      proposal_path = str(proposal.document)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   174
      proposal_name = proposal_path.split('/')[-1]
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   175
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   176
      context['proposal_path'] = proposal_path
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   177
      context['proposal_name'] = proposal_name
14
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   178
      context['last_submitted'] = proposal.submitted_on
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   179
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   180
    if 'HTTP_REFERER' in request.META:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   181
      referer = request.META['HTTP_REFERER'].split('/')
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   182
      if referer[-1]:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   183
        ref = referer[-1]
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   184
      else:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   185
        ref = referer[-2]
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   186
        
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   187
      if ref == 'create':
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   188
        context['created_now'] = True
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   189
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   190
    context['micr_code'] = project.micr_code
13
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   191
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   192
    reviews = project.review_set.all().order_by('-reviewed_on')
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   193
    if reviews:
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   194
      context['last_reviewed'] = reviews[0].reviewed_on
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   195
      context['nr_reviews'] = len(reviews)
684540719344 Added footer line.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   196
 
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   197
  else:
19
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   198
    if not prop_form:
22
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   199
19
0c9bdcfac9f7 Added all data
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
   200
      prop_form = projrev_forms.ProposalForm()
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   202
  context['form'] = prop_form
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   203
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   204
  template = 'projrev/proposal/submit.html'
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   205
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   206
  return render_to_response(template, RequestContext(request, context))
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   208
@access.register('proposer')
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   209
@access.checkAccess
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   210
def withdraw(request, micr_code=None):
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   211
  """View Method for withdrawal of proposal.
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   212
  """
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   213
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   214
  if micr_code:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   215
    project = Project.objects.get(micr_code=micr_code)
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   216
    if project:
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   217
      project.status = 'invalid'
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   218
      project.save()
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   219
      context = {
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   220
          'withdrawn': True,
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   221
      }
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   222
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   223
  template = 'projrev/proposal/withdraw.html'
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   224
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   225
  return render_to_response(template, RequestContext(request, context))
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   226
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   227
@access.register('reviewer')
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   228
@access.checkAccess
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   229
def review(request, micr_code=None):
4
8d9da911ed7d Withdraw of proposals.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 0
diff changeset
   230
  """View for reviewing the proposal.
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   231
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   232
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   233
  if request.method == 'POST':
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   234
    return reviewPost(request, micr_code)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   235
  else:
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   236
    return reviewGet(request, micr_code)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   237
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   238
def reviewPost(request, micr_code=None):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   239
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   240
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   241
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   242
  rev_form = projrev_forms.ReviewForm(request.POST)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   243
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   244
  if rev_form.is_valid():
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   245
    cleaned_data = rev_form.cleaned_data
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   246
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   247
    cleaned_data['project'] = Project.objects.get(micr_code=micr_code)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   248
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   249
    # If the form is valid create a new project or update the project
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   250
    # if it already exists from the form.
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   251
    review = rev_form.save()
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   252
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   253
  return reviewGet(request, micr_code, rev_form)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   254
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   255
def reviewGet(request, micr_code=None, rev_form=None):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   256
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   257
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   258
14
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   259
  context = {}
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   260
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   261
  if not micr_code:
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   262
    template = 'projrev/proposal/list.html'
16
bed14c9685a5 Project model changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 15
diff changeset
   263
    context['projects'] = Project.objects.all().exclude(
bed14c9685a5 Project model changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 15
diff changeset
   264
        status__exact='invalid').order_by('-last_updated_on')
14
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   265
    context['row_url'] = '/proposal/review/'
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   266
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   267
    return render_to_response(template, RequestContext(request, context))
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   268
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   269
  if not rev_form:
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   270
    rev_form = projrev_forms.ReviewForm()
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   271
14
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   272
  project = Project.objects.get(micr_code=micr_code)
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   273
  context['form'] = rev_form
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   274
  context['project'] = project
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   275
14
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   276
  proposal = project.proposal_set.all().order_by('-submitted_on')[0]
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   277
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   278
  if proposal:
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   279
    proposal_path = str(proposal.document)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   280
14
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   281
    proposal_name = proposal_path.split('/')[-1]
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   282
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   283
    context['proposal_path'] = proposal_path
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   284
    context['proposal_name'] = proposal_name
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   285
    context['last_submitted'] = proposal.submitted_on
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   286
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   287
  reviews = project.review_set.all().order_by('-reviewed_on')
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   288
  if reviews:
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   289
    context['last_reviewed'] = reviews[0].reviewed_on
9d720dfded68 Footer for reviews.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 13
diff changeset
   290
    context['nr_reviews'] = len(reviews)
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   291
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   292
  template = 'projrev/proposal/review.html'
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   293
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   294
  return render_to_response(template, RequestContext(request, context))
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   295
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
   296
@access.register('reviewer')
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   297
@access.checkAccess
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   298
def rank(request, micr_code=None):
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   299
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   300
  """
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   301
15
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   302
  context = {}
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   303
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   304
  if not micr_code:
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   305
    template = 'projrev/proposal/list.html'
16
bed14c9685a5 Project model changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 15
diff changeset
   306
    context['projects'] = Project.objects.all().exclude(
bed14c9685a5 Project model changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 15
diff changeset
   307
        status__exact='invalid').order_by('-last_updated_on')
15
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   308
    context['row_url'] = '/proposal/rank/'
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   309
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   310
    return render_to_response(template, RequestContext(request, context))
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   311
15
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   312
  project = Project.objects.get(micr_code=micr_code)
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   313
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   314
  proposal = project.proposal_set.all().order_by('-submitted_on')[0]
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   315
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   316
  if proposal:
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   317
    proposal_path = str(proposal.document)
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   318
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   319
    proposal_name = proposal_path.split('/')[-1]
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   320
15
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   321
    context['proposal_path'] = proposal_path
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   322
    context['proposal_name'] = proposal_name
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   323
    context['last_submitted'] = proposal.submitted_on
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   324
15
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   325
  reviews = project.review_set.all().order_by('-reviewed_on')
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   326
  nr_reviews = len(reviews)
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   327
  if reviews:
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   328
    context['last_reviewed'] = reviews[0].reviewed_on
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   329
    context['nr_reviews'] = nr_reviews
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   330
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   331
  review_score = [0] * 9
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   332
  for review in reviews:
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   333
    review_score[0] += review.attribute1
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   334
    review_score[1] += review.attribute2
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   335
    review_score[2] += review.attribute3
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   336
    review_score[3] += review.attribute4
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   337
    review_score[4] += review.attribute5
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   338
    review_score[5] += review.attribute6
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   339
    review_score[6] += review.attribute7
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   340
    review_score[7] += review.attribute8
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   341
    review_score[8] += review.attribute9
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   342
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   343
  total_score = sum(review_score)
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   344
15
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   345
  review_avg = [0] * 9
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   346
  for i, rs in enumerate(review_score):
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   347
    try:
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   348
      review_avg[i] = float(rs) / nr_reviews
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   349
    except ZeroDivisionError:
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   350
      review_avg[i] = 0
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   351
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   352
  try:
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   353
    total_avg = float(total_score) / nr_reviews
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   354
  except ZeroDivisionError:
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   355
    total_avg = 0
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   356
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   357
  context['project'] = project
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   358
  context['review_score'] = review_score
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   359
  context['total_score'] = total_score
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   360
  context['review_avg'] = review_avg
8c9e6c98de82 Ranking of proposals view completion.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 14
diff changeset
   361
  context['total_avg'] = total_avg
0
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   362
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   363
  template = 'projrev/proposal/rank.html'
c94bd9ae70d2 First commit.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   364
6
17cab73c49eb Request context.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   365
  return render_to_response(template, RequestContext(request, context))
22
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   366
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   367
def getDistrictsForState(request):
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   368
  """View function that sends the districts for the given state code via AJAXy.
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   369
  """
23
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   370
22
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   371
  get_params = request.GET
23
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   372
  if get_params:
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   373
    st_code = get_params['_value']
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   374
    dt_dict = Project.DISTRICT_CHOICES
22
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   375
23
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   376
    # Get the Districts corresponding to the given State code.
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   377
    dt_names = {}
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   378
    count = 1
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   379
    for dt_code in dt_dict:
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   380
      if dt_code[:2] == st_code:
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   381
        dt_names[dt_code] = dt_dict[dt_code]
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   382
        count += 1
22
0321f984fb16 Made form changes.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 21
diff changeset
   383
23
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   384
    # Sort the List based on District Name.
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   385
    dt_send = [{'': "---------" }]
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   386
    dt_names_sorted = sorted(dt_names.items(), key=lambda (k,v): (v,k))
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   387
    for dt_code, dt_name in dt_names_sorted:
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   388
      dt_send.append({dt_code: dt_name})
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   389
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   390
    return HttpResponse(json.dumps(dt_send))
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   391
91cf6872d853 Add jqeury purr plugin for help text.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 22
diff changeset
   392
  return HttpResponse(None)