<commit_before>import uuid
import os.path
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseNotFound
from django.core.urlresolvers import reverse
from dimagi.utils.web import render_to_response
from casexml.apps.case.models import CommCareCase
from corehq.apps.domain.decorators import login_and_domain_required
from corehq.apps.importer import base
from corehq.apps.importer.util import ExcelFile
from tempfile import mkstemp

@login_and_domain_required
def excel_config(request, domain):
    if request.method == 'POST' and request.FILES:
        named_columns = request.POST['named_columns']    
        uploaded_file_handle = request.FILES['file']
        
        extension = os.path.splitext(uploaded_file_handle.name)[1][1:].strip().lower()
        
        if extension in ExcelFile.ALLOWED_EXTENSIONS and uploaded_file_handle.content_type == 'application/vnd.ms-excel':
            # get a temp file
            fd, filename = mkstemp(suffix='.'+extension)
            
            with os.fdopen(fd, "wb") as destination:
                # write the uploaded file to the temp file
                for chunk in uploaded_file_handle.chunks():
                    destination.write(chunk)

            uploaded_file_handle.close() 
            
            # stash filename for subsequent views      
            request.session['excel_path'] = filename  
            
            # open spreadsheet and get columns
            spreadsheet = ExcelFile(filename, (named_columns == 'yes'))
            columns = spreadsheet.get_header_columns()
                
            # get case types in this domain
            case_types = []
            for row in CommCareCase.view('hqcase/types_by_domain',reduce=True,group=True,startkey=[domain],endkey=[domain,{}]).all():
                if not row['key'][1] in case_types:
                    case_types.append(row['key'][1])
                                            
            return render_to_response(request, "excel_config.html", {
                                        'named_columns': named_columns, 
                                        'columns': columns,
                                        'case_types': case_types,
                                        'domain': domain,
                                        'report': {
                                            'name': 'Import: Configuration'
                                         },
                                        'slug': base.ExcelImporter.slug})
    
    #TODO show bad/invalid file error on this page
    return HttpResponseRedirect(reverse("report_dispatcher", args=[domain, base.ExcelImporter.slug]))
      
@login_and_domain_required
def excel_fields(request, domain):
    named_columns = request.POST['named_columns']
    filename = request.session.get('excel_path')
    case_type = request.POST['case_type']
    search_column = request.POST['search_column']
    search_field = request.POST['search_field']
    key_value_columns = request.POST['key_value_columns']
    key_column = ''
    value_column = ''
    
    spreadsheet = ExcelFile(filename, named_columns)
    columns = spreadsheet.get_header_columns()
    
    if key_value_columns == 'yes':
        key_column = request.POST['key_column']
        value_column = request.POST['value_column']
        
        excel_fields = []
        key_column_index = columns.index(key_column)        
        
        # if key/value columns were specified, get all the unique keys listed
        if key_column_index:        
            excel_fields = spreadsheet.get_unique_column_values(key_column_index)
                
        # concatenate unique key fields with the rest of the columns
        excel_fields = columns + excel_fields
        # remove key/value column names from list
        excel_fields.remove(key_column)
        excel_fields.remove(value_column)                 
    else:
        excel_fields = columns
              
    # get all unique existing case properties, known and unknown
    case_fields = []
    row = CommCareCase.view('hqcase/unique_known_properties',reduce=True,group=True,startkey=domain,endkey=domain).one()
    for value in row['value']:
        case_fields.append(value)
        
    row = CommCareCase.view('hqcase/unique_unknown_properties',reduce=True,group=True,startkey=domain,endkey=domain).one()
    for value in row['value']:
        case_fields.append(value)               
    
    return render_to_response(request, "excel_fields.html", {
                                'named_columns': named_columns,
                                'case_type': case_type,                                                               
                                'search_column': search_column, 
                                'search_field': search_field,                                                        
                                'key_column': key_column,
                                'value_column': value_column,
                                'columns': columns,
                                'excel_fields': excel_fields,
                                'excel_fields_range': range(len(excel_fields)),
                                'case_fields': case_fields,
                                'domain': domain,
                                'report': {
                                    'name': 'Import: Match columns to fields'
                                 },
                                'slug': base.ExcelImporter.slug})     

@login_and_domain_required
def excel_commit(request, domain):  
    named_columns = request.POST['named_columns'] 
    filename = request.session.get('excel_path')
    case_type = request.POST['case_type']
    search_column = request.POST['search_column']
    search_field = request.POST['search_field']
    key_column = request.POST['key_column']
    value_column = request.POST['value_column']    
    
    # unset filename session var
    try:
        del request.session['excel_path']<commit_msg>add require_POST decorator to importer view<commit_after>import uuid
import os.path
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseNotFound
from django.core.urlresolvers import reverse
from dimagi.utils.web import render_to_response
from casexml.apps.case.models import CommCareCase
from corehq.apps.domain.decorators import login_and_domain_required
from corehq.apps.importer import base
from corehq.apps.importer.util import ExcelFile
from tempfile import mkstemp
from django.views.decorators.http import require_POST

@login_and_domain_required
def excel_config(request, domain):
    if request.method == 'POST' and request.FILES:
        named_columns = request.POST['named_columns']    
        uploaded_file_handle = request.FILES['file']
        
        extension = os.path.splitext(uploaded_file_handle.name)[1][1:].strip().lower()
        
        if extension in ExcelFile.ALLOWED_EXTENSIONS and uploaded_file_handle.content_type == 'application/vnd.ms-excel':
            # get a temp file
            fd, filename = mkstemp(suffix='.'+extension)
            
            with os.fdopen(fd, "wb") as destination:
                # write the uploaded file to the temp file
                for chunk in uploaded_file_handle.chunks():
                    destination.write(chunk)

            uploaded_file_handle.close() 
            
            # stash filename for subsequent views      
            request.session['excel_path'] = filename  
            
            # open spreadsheet and get columns
            spreadsheet = ExcelFile(filename, (named_columns == 'yes'))
            columns = spreadsheet.get_header_columns()
                
            # get case types in this domain
            case_types = []
            for row in CommCareCase.view('hqcase/types_by_domain',reduce=True,group=True,startkey=[domain],endkey=[domain,{}]).all():
                if not row['key'][1] in case_types:
                    case_types.append(row['key'][1])
                                            
            return render_to_response(request, "excel_config.html", {
                                        'named_columns': named_columns, 
                                        'columns': columns,
                                        'case_types': case_types,
                                        'domain': domain,
                                        'report': {
                                            'name': 'Import: Configuration'
                                         },
                                        'slug': base.ExcelImporter.slug})
    
    #TODO show bad/invalid file error on this page
    return HttpResponseRedirect(reverse("report_dispatcher", args=[domain, base.ExcelImporter.slug]))
      
@login_and_domain_required
def excel_fields(request, domain):
    named_columns = request.POST['named_columns']
    filename = request.session.get('excel_path')
    case_type = request.POST['case_type']
    search_column = request.POST['search_column']
    search_field = request.POST['search_field']
    key_value_columns = request.POST['key_value_columns']
    key_column = ''
    value_column = ''
    
    spreadsheet = ExcelFile(filename, named_columns)
    columns = spreadsheet.get_header_columns()
    
    if key_value_columns == 'yes':
        key_column = request.POST['key_column']
        value_column = request.POST['value_column']
        
        excel_fields = []
        key_column_index = columns.index(key_column)        
        
        # if key/value columns were specified, get all the unique keys listed
        if key_column_index:        
            excel_fields = spreadsheet.get_unique_column_values(key_column_index)
                
        # concatenate unique key fields with the rest of the columns
        excel_fields = columns + excel_fields
        # remove key/value column names from list
        excel_fields.remove(key_column)
        excel_fields.remove(value_column)                 
    else:
        excel_fields = columns
              
    # get all unique existing case properties, known and unknown
    case_fields = []
    row = CommCareCase.view('hqcase/unique_known_properties',reduce=True,group=True,startkey=domain,endkey=domain).one()
    for value in row['value']:
        case_fields.append(value)
        
    row = CommCareCase.view('hqcase/unique_unknown_properties',reduce=True,group=True,startkey=domain,endkey=domain).one()
    for value in row['value']:
        case_fields.append(value)               
    
    return render_to_response(request, "excel_fields.html", {
                                'named_columns': named_columns,
                                'case_type': case_type,                                                               
                                'search_column': search_column, 
                                'search_field': search_field,                                                        
                                'key_column': key_column,
                                'value_column': value_column,
                                'columns': columns,
                                'excel_fields': excel_fields,
                                'excel_fields_range': range(len(excel_fields)),
                                'case_fields': case_fields,
                                'domain': domain,
                                'report': {
                                    'name': 'Import: Match columns to fields'
                                 },
                                'slug': base.ExcelImporter.slug})     

@require_POST
@login_and_domain_required
def excel_commit(request, domain):  
    named_columns = request.POST['named_columns'] 
    filename = request.session.get('excel_path')
    case_type = request.POST['case_type']
    search_column = request.POST['search_column']
    search_field = request.POST['search_field']
    key_column = request.POST['key_column']
    value_column = request.POST['value_column']    
    
    # unset filename session var
    try:
        del request.session['excel_path']
{'fraction_non_alphanumeric': 0.08429995098840058, 'fraction_numerical': 0.001143603986276752, 'mean_word_length': 1.7736746714997735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2108406', 'n_tokens_mistral': 3089, 'n_tokens_neox': 2986, 'n_words': 623}