def add_administrative_unit(request):
    if request.method == 'POST':
        form = AdminUnitForm(request.POST)
        if form.is_valid():
            _create_admin_unit(form)
            return HttpResponseRedirect('/admin_unit_creation/success/')
        else:
            return render_to_response('add_admin_unit.html',
                                      {'form': form},
                                      context_instance=RequestContext(request)
                                      ) 
    else:
        form = AdminUnitForm()
        return render_to_response('add_admin_unit.html',
                                  {'form' : form},
                                  context_instance=RequestContext(request)
                                  )
def edit_administrative_unit(request, id):
    if request.method == 'POST':
        form = AdminUnitForm(request.POST)
        if form.is_valid():
            _edit_admin_unit(form, id)
            request.session['message'] = "Admin unit has been edited successfully"
            url = "/admin_units/"
            return HttpResponseRedirect(url)
        else:
            return render_to_response('add_admin_unit.html',
                                     {'form': form,
                                      'action' : 'edit_admin_unit/' + id
                                      },
                                      context_instance=RequestContext(request)
                                      ) 
    else:
        form = AdminUnitForm()
        admin_unit = AdministrativeUnit.objects.get(id=int(id))
        form.fields['name'].initial = admin_unit.name
        form.fields['country'].initial = admin_unit.country
        form.fields['region_type'].initial = admin_unit.region_type
        form.fields['health'].initial = admin_unit.health
        form.fields['economy'].initial = admin_unit.economy
        form.fields['environment'].initial = admin_unit.environment
        form.fields['governance'].initial = admin_unit.governance
        form.fields['infrastructure'].initial = admin_unit.infrastructure
        form.fields['social_sector'].initial = admin_unit.social_sector
        form.fields['agriculture'].initial = admin_unit.agriculture
        form.fields['dev_partners'].initial = admin_unit.dev_partners
        form.fields['recent_reports'].initial = admin_unit.recent_reports
        form.fields['resources'].initial = admin_unit.resources

        return render_to_response('add_admin_unit.html',
                                  {
                                  'form': form,
                                  'action' : 'edit_admin_unit/' + id
                                  },
                                  context_instance=RequestContext(request)
                                  )
def add_administrative_unit(request):
    if request.method == 'POST':
        form = AdminUnitForm(request.POST)
        if form.is_valid():
            _create_admin_unit(form)
            request.session['message'] = "Admin unit has been added successfully"
            url = "/admin_units/"
            return HttpResponseRedirect(url)

        else:
            return render_to_response('add_admin_unit.html',
                                     {'form': form,
                                      'action' : 'add_admin_unit'
                                      },
                                      context_instance=RequestContext(request)
                                      ) 
    else:
        form = AdminUnitForm()
        return render_to_response('add_admin_unit.html',
                                     {'form': form,
                                      'action' : 'add_admin_unit'
                                      },
                                  context_instance=RequestContext(request)
                                  )