def bulk_upload_contacts_to_update(request):
    """
    bulk upload contacts from an excel file
    """
    if request.method == 'POST':
        contactsform = ExcelTestUploadForm(request.POST, request.FILES)
        message = ''
        if contactsform.is_valid():
            import ipdb
            ipdb.set_trace()
            if contactsform.is_valid() \
                and request.FILES.get('excel_file', None):
                fields = [
                    'number',
                    'name',
                    'province',
                    'commune',
                    'colline',
                    'language',
                    'county',
                    'village',
                    'birthdate',
                    'group',
                    'gender',
                ]
                #message = handle_excel_file_test(request.FILES['excel_file'
                #                            ], contactsform.cleaned_data['assign_to_group'
                #                            ], fields)
                message = handle_excel_file_update(request.FILES['excel_file'],
                                                   fields)
                print("AVANT LA VARIABLE MESSAGE")
                print(message)
                print("APRES LA VARIABLE MESSAGE")
            return render_to_response(
                'ureport/bulk_contact_upload_update.html', {
                    'contactsform': contactsform,
                    'message': message
                },
                context_instance=RequestContext(request))
    contactsform = ExcelTestUploadForm()
    return render_to_response('ureport/bulk_contact_upload_update.html',
                              {'contactsform': contactsform},
                              context_instance=RequestContext(request))
def bulk_upload_contacts_to_update(request):
    """
    bulk upload contacts from an excel file
    """
    if request.method == 'POST':
        contactsform = ExcelTestUploadForm(request.POST, request.FILES)
        message=''
        if contactsform.is_valid():
            import ipdb; ipdb.set_trace()
            if contactsform.is_valid() \
                and request.FILES.get('excel_file', None):
                fields = [
                    'number',
                    'name',
                    'province',
                    'commune',
                    'colline',
                    'language',
                    'county',
                    'village',
                    'birthdate',
                    'group',
                    'gender',
                ]
                #message = handle_excel_file_test(request.FILES['excel_file'
                #                            ], contactsform.cleaned_data['assign_to_group'
                #                            ], fields)
                message = handle_excel_file_update(request.FILES['excel_file'], fields)
                print("AVANT LA VARIABLE MESSAGE")
                print(message)
                print("APRES LA VARIABLE MESSAGE")
            return render_to_response('ureport/bulk_contact_upload_update.html'
                , {'contactsform': contactsform, 'message'
                : message},
                                      context_instance=RequestContext(request))
    contactsform = ExcelTestUploadForm()
    return render_to_response('ureport/bulk_contact_upload_update.html',
                              {'contactsform': contactsform},
                              context_instance=RequestContext(request))
def ureporters(request):
    access = get_access(request)
    print request.method
    message = ''
    if request.method == 'POST' and request.FILES:
        contactsform = ExcelTestUploadForm(request.POST, request.FILES)
        if contactsform.is_valid() and contactsform.good_file():
            new_file = contactsform.cleaned_data
            fields = [
                'number',
                'name',
                'province',
                'commune',
                'colline',
                'language',
                'county',
                'village',
                'birthdate',
                'group',
                'gender',
            ]
            message = handle_excel_file_update(new_file['excel_file'], fields)
            print(message)
            messages.success(request, message)
            return HttpResponseRedirect("/reporter")

        else:
            return HttpResponse(
                content='The form is not valid. Resfresh this page')

    columns = [
        ('Identifier', True, 'connection_pk', SimpleSorter()),
        ('Modile', True, 'mobile', SimpleSorter()),
        (
            'Age',
            True,
            'age',
            SimpleSorter(),
        ),
        (
            'Gender',
            True,
            'gender',
            SimpleSorter(),
        ),
        (
            'Language',
            True,
            'language',
            SimpleSorter(),
        ),
        (
            'Province',
            True,
            'province',
            SimpleSorter(),
        ),
        (
            'Group(s)',
            True,
            'group',
            SimpleSorter(),
        ),
        (
            'Polls ',
            True,
            'questions',
            SimpleSorter(),
        ),
        (
            'Responses ',
            True,
            'responses',
            SimpleSorter(),
        ),
        (
            'Messages Sent',
            True,
            'incoming',
            SimpleSorter(),
        ),
        #        ('caregiver', True, 'is_caregiver', SimpleSorter(),),
        #        ('join date', True, 'autoreg_join_date', SimpleSorter(),),
        #        ('quit date', True, 'quit_date', SimpleSorter(),),
    ]
    queryset = get_contacts2(request=request)
    if access is not None:
        groups = ",".join(list(access.groups.values_list('name', flat=True)))
        queryset = queryset.filter(group__icontains=groups)
    return generic(
        request,
        model=UreportContact,
        queryset=get_contacts2,
        contactsform=ExcelTestUploadForm,
        message=message,
        results_title='uReporters',
        filter_forms=[
            UreporterSearchForm, GenderFilterForm, AgeFilterForm,
            MultipleDistictFilterForm, FilterGroupsForm, FilterByGroupForm,
            FilterByLocationForm
        ],
        action_forms=[
            MassTextForm, AssignGroupForm, BlacklistForm, AssignToNewPollForm,
            RemoveGroupForm, TemplateMessage, ExportToExcelForm
        ],
        objects_per_page=25,
        base_template='ureport/ureporters_base.html',
        partial_base='ureport/partials/contacts/partial_base.html',
        partial_row='ureport/partials/contacts/partial_row.html',
        paginator_template='ureport/partials/new_pagination.html',
        paginator_func=ureport_paginate,
        columns=columns,
    )
def ureporters(request):
    access = get_access(request)
    print request.method
    message=''
    if request.method == 'POST' and request.FILES:
        contactsform = ExcelTestUploadForm(request.POST, request.FILES)
        if contactsform.is_valid() and contactsform.good_file() :
            new_file = contactsform.cleaned_data
            fields = [
                'number',
                'name',
                'province',
                'commune',
                'colline',
                'language',
                'county',
                'village',
                'birthdate',
                'group',
                'gender',
                ]
            message = handle_excel_file_update(new_file['excel_file'], fields)
            print(message)
            messages.success(request, message )
            return HttpResponseRedirect("/reporter")


        else:
        	return HttpResponse(content='The form is not valid. Resfresh this page')

    columns = [
        ('Identifier', True, 'connection_pk', SimpleSorter()),
        ('Modile', True, 'mobile', SimpleSorter()),
        ('Age', True, 'age', SimpleSorter(),),
        ('Gender', True, 'gender', SimpleSorter(),),
        ('Language', True, 'language',SimpleSorter(),),
        ('Province', True, 'province', SimpleSorter(),),
        ('Group(s)', True, 'group', SimpleSorter(),),
        ('Polls ', True, 'questions', SimpleSorter(),),
        ('Responses ', True, 'responses', SimpleSorter(),),
        ('Messages Sent', True, 'incoming', SimpleSorter(),),
#        ('caregiver', True, 'is_caregiver', SimpleSorter(),),
#        ('join date', True, 'autoreg_join_date', SimpleSorter(),),
#        ('quit date', True, 'quit_date', SimpleSorter(),),
        ]
    queryset = get_contacts2(request=request)
    if access is not None:
        groups = ",".join(list(access.groups.values_list('name', flat=True)))
        queryset = queryset.filter(group__icontains=groups)
    return generic(request,
        model=UreportContact,
        queryset=get_contacts2,
        contactsform=ExcelTestUploadForm,
        message=message,
        results_title='uReporters',
        filter_forms=[ UreporterSearchForm,  GenderFilterForm, AgeFilterForm, MultipleDistictFilterForm, FilterGroupsForm, FilterByGroupForm, FilterByLocationForm ],
        action_forms=[MassTextForm, AssignGroupForm, BlacklistForm,  AssignToNewPollForm,RemoveGroupForm,TemplateMessage , ExportToExcelForm],
        objects_per_page=25,
        base_template='ureport/ureporters_base.html',
        partial_base='ureport/partials/contacts/partial_base.html',
        partial_row='ureport/partials/contacts/partial_row.html',
        paginator_template='ureport/partials/new_pagination.html',
        paginator_func=ureport_paginate,
        columns=columns,
        )