Ejemplo n.º 1
0
def generateAndroidOutput(request):
    output = ""
    # Create the output files from the log items
    view_regex = re.compile(r"^android-condition(?P<showSegs>_showSegs)?(?P<autofill>_autofill)?$")
    for form_image in FormImage.objects.all():
        if str(form_image)[0] == "J":
            # Filter out practice forms
            continue
        for user in User.objects.all():
            if user in excluded_users:
                continue
            filtered_li = LogItem.objects.filter(
                formImage=form_image, user=user, activity__in=["android-text changed", "android-answer selected"]
            )
            if len(filtered_li) > 0:
                path_to_initial_json = os.path.join(form_image.output_path, "output.json")
                initial_json = utils.load_json_to_pyobj(path_to_initial_json)
                viewParse = view_regex.search(filtered_li[0].view).groupdict()
                initial_json["autofill"] = bool(viewParse.get("autofill"))
                initial_json["showSegs"] = bool(viewParse.get("showSegs"))
                initial_json["formView"] = False
                initial_json["tableView"] = False
                initial_json["android"] = True
                for field in initial_json["fields"]:
                    ordered_field_li = filtered_li.filter(fieldName=field["name"]).order_by("-timestamp")
                    if len(ordered_field_li) > 0:
                        field["transcription"] = ordered_field_li[0].newValue
                    else:
                        pass
                user_json_path = os.path.join(form_image.output_path, "users", user.username, "output.json")
                if os.path.exists(user_json_path):
                    raise Exception("path exists: " + user_json_path)
                output += user_json_path + "\n"
                utils.print_pyobj_to_json(initial_json, user_json_path)
    return HttpResponse(output, mimetype="application/json")
Ejemplo n.º 2
0
def save_transcriptions(request):
    """
    Save the sumitted group of transcriptions to the file system.
    """
    c = {}
    c.update(csrf(request))
    if request.method == 'POST': # If the form has been submitted...
        #Logging:
        LogItem.objects.create(user=request.user,
                       activity='save_transcriptions',
                       forms=','.join(request.POST.keys())
                       ).save()
        row_dict = group_by_prefix(query_dict_to_dict(request.POST))
        for formImageId, transcription in row_dict.items():
            form_image = FormImage.objects.get(id=formImageId)
            json_path = os.path.join(form_image.output_path, 'transcription.json')
            if not os.path.exists(json_path):
                raise Exception('No json for form image')
            fp = codecs.open(json_path, mode="r", encoding="utf-8")
            pyobj = json.load(fp, encoding='utf-8')#This is where we load the json output
            fp.close()
            for field in pyobj.get('fields'):
                field_transcription = transcription.get(field['name'])
                if field_transcription:
                    field['transcription'] = field_transcription
            utils.print_pyobj_to_json(pyobj, json_path)
            form_image.status = 'm'
            form_image.save()
        return HttpResponse()
    else:
        return HttpResponseBadRequest("Only post requests please.")
Ejemplo n.º 3
0
def correct_transcriptions(request):
    fi_dict = {}
    for form_image in FormImage.objects.all():
        correct_transcription = {}

        add_to_correct_transcription(
            correct_transcription,
            utils.load_json_to_pyobj(
                os.path.join(form_image.output_path, 'output.json')))

        user_dir = os.path.join(form_image.output_path, 'users')
        user_list = []
        if os.path.exists(user_dir):
            user_list = os.listdir(user_dir)
            for user in user_list:
                if user in excluded_users:
                    continue
                json_path = os.path.join(user_dir, user, 'output.json')
                pyobj = utils.load_json_to_pyobj(json_path)
                add_to_correct_transcription(correct_transcription, pyobj)
        add_ground_truth_length(correct_transcription)
        fi_dict[str(form_image)] = correct_transcription
        utils.print_pyobj_to_json(
            correct_transcription,
            os.path.join(form_image.output_path, 'corrected.json'))
    return HttpResponse("corrected", mimetype="application/json")
Ejemplo n.º 4
0
def save_transcriptions(request):
    """
    Save the sumitted group of transcriptions to the file system.
    """
    c = {}
    c.update(csrf(request))
    if request.method == 'POST':  # If the form has been submitted...
        #Logging:
        LogItem.objects.create(user=request.user,
                               activity='save_transcriptions',
                               forms=','.join(request.POST.keys())).save()
        row_dict = group_by_prefix(query_dict_to_dict(request.POST))
        for formImageId, transcription in row_dict.items():
            form_image = FormImage.objects.get(id=formImageId)
            json_path = os.path.join(form_image.output_path,
                                     'transcription.json')
            if not os.path.exists(json_path):
                raise Exception('No json for form image')
            fp = codecs.open(json_path, mode="r", encoding="utf-8")
            pyobj = json.load(
                fp, encoding='utf-8')  #This is where we load the json output
            fp.close()
            for field in pyobj.get('fields'):
                field_transcription = transcription.get(field['name'])
                if field_transcription:
                    field['transcription'] = field_transcription
            utils.print_pyobj_to_json(pyobj, json_path)
            form_image.status = 'm'
            form_image.save()
        return HttpResponse()
    else:
        return HttpResponseBadRequest("Only post requests please.")
Ejemplo n.º 5
0
def transcription_context(modeladmin, request, queryset, autofill=None, showSegs=None, formView=None):
    """
    Function for organizing the data that will be rendered by one of the
    transcription view tempaltes.
    """
    form_template = None
    json_outputs = []
    for formImage in queryset:
        if formImage.status == 'f':
            messages.error(request, "You cannot transcribe a finalized from.")
            return
        if form_template is None:
            #Set the template to the template the first image is set to.
            form_template = formImage.template
        elif form_template.name != formImage.template.name:
            messages.error(request, "Mixed templates: " + form_template.name +
                                    " and " + formImage.template.name)
            return
        
        json_path = os.path.join(formImage.output_path, 'output.json')
        if not os.path.exists(json_path):
            #i.e. skip unprocessed images
            continue
        transcribed_json_path = os.path.join(formImage.output_path, 'transcription.json')
        if not os.path.exists(transcribed_json_path):
            try:
                os.makedirs(os.path.dirname(transcribed_json_path))
            except:
                pass
            fp = codecs.open(json_path, mode="r", encoding="utf-8")
            pyobj = json.load(fp, encoding='utf-8')#This is where we load the json output
            fp.close()
            pyobj['form_id'] = int(formImage.id)
            pyobj['outputDir'] = os.path.basename(formImage.output_path)
            pyobj['imageName'] = str(formImage)
            pyobj['templateName'] = form_template.name
            pyobj['userName'] = str(request.user)
            pyobj['autofill'] = autofill
            pyobj['showSegs'] = showSegs
            pyobj['formView'] = formView
            utils.print_pyobj_to_json(pyobj, transcribed_json_path)

        with codecs.open(transcribed_json_path, mode="r", encoding="utf-8") as fp:
            pyobj = json.load(fp, encoding='utf-8')
        json_outputs.append(pyobj)

    if form_template is None:
        messages.error("no template")
        return
    form_template_fp = codecs.open(form_template.json.path, mode="r", encoding="utf-8")
    json_template = json.load(form_template_fp, encoding='utf-8')
    return RequestContext(request, {
                 'template_name':form_template.name,
                 'json_template':json_template,
                 'json_outputs':json_outputs,
                 'user':request.user,
                 'autofill':autofill,
                 'showSegs':showSegs,
                 })
Ejemplo n.º 6
0
def generateAndroidOutput(request):
    output = ''
    #Create the output files from the log items
    view_regex = re.compile(
        r"^android-condition(?P<showSegs>_showSegs)?(?P<autofill>_autofill)?$")
    for form_image in FormImage.objects.all():
        if str(form_image)[0] == 'J':
            #Filter out practice forms
            continue
        for user in User.objects.all():
            if user in excluded_users:
                continue
            filtered_li = LogItem.objects.filter(formImage=form_image,
                                                 user=user,
                                                 activity__in=[
                                                     'android-text changed',
                                                     'android-answer selected'
                                                 ])
            if len(filtered_li) > 0:
                path_to_initial_json = os.path.join(form_image.output_path,
                                                    'output.json')
                initial_json = utils.load_json_to_pyobj(path_to_initial_json)
                viewParse = view_regex.search(filtered_li[0].view).groupdict()
                initial_json['autofill'] = bool(viewParse.get('autofill'))
                initial_json['showSegs'] = bool(viewParse.get('showSegs'))
                initial_json['formView'] = False
                initial_json['tableView'] = False
                initial_json['android'] = True
                for field in initial_json['fields']:
                    ordered_field_li = filtered_li.filter(
                        fieldName=field['name']).order_by('-timestamp')
                    if len(ordered_field_li) > 0:
                        field['transcription'] = ordered_field_li[0].newValue
                    else:
                        pass
                user_json_path = os.path.join(form_image.output_path, 'users',
                                              user.username, 'output.json')
                if os.path.exists(user_json_path):
                    raise Exception('path exists: ' + user_json_path)
                output += user_json_path + '\n'
                utils.print_pyobj_to_json(initial_json, user_json_path)
    return HttpResponse(output, mimetype="application/json")
Ejemplo n.º 7
0
def correct_transcriptions(request):
    fi_dict = {}
    for form_image in FormImage.objects.all():
        correct_transcription = {}

        add_to_correct_transcription(
            correct_transcription, utils.load_json_to_pyobj(os.path.join(form_image.output_path, "output.json"))
        )

        user_dir = os.path.join(form_image.output_path, "users")
        user_list = []
        if os.path.exists(user_dir):
            user_list = os.listdir(user_dir)
            for user in user_list:
                if user in excluded_users:
                    continue
                json_path = os.path.join(user_dir, user, "output.json")
                pyobj = utils.load_json_to_pyobj(json_path)
                add_to_correct_transcription(correct_transcription, pyobj)
        add_ground_truth_length(correct_transcription)
        fi_dict[str(form_image)] = correct_transcription
        utils.print_pyobj_to_json(correct_transcription, os.path.join(form_image.output_path, "corrected.json"))
    return HttpResponse("corrected", mimetype="application/json")
Ejemplo n.º 8
0
def transcription_context(modeladmin,
                          request,
                          queryset,
                          autofill=None,
                          showSegs=None,
                          formView=None):
    """
    Function for organizing the data that will be rendered by one of the
    transcription view tempaltes.
    """
    form_template = None
    json_outputs = []
    for formImage in queryset:
        if formImage.status == 'f':
            messages.error(request, "You cannot transcribe a finalized from.")
            return
        if form_template is None:
            #Set the template to the template the first image is set to.
            form_template = formImage.template
        elif form_template.name != formImage.template.name:
            messages.error(
                request, "Mixed templates: " + form_template.name + " and " +
                formImage.template.name)
            return

        json_path = os.path.join(formImage.output_path, 'output.json')
        if not os.path.exists(json_path):
            #i.e. skip unprocessed images
            continue
        transcribed_json_path = os.path.join(formImage.output_path,
                                             'transcription.json')
        if not os.path.exists(transcribed_json_path):
            try:
                os.makedirs(os.path.dirname(transcribed_json_path))
            except:
                pass
            fp = codecs.open(json_path, mode="r", encoding="utf-8")
            pyobj = json.load(
                fp, encoding='utf-8')  #This is where we load the json output
            fp.close()
            pyobj['form_id'] = int(formImage.id)
            pyobj['outputDir'] = os.path.basename(formImage.output_path)
            pyobj['imageName'] = str(formImage)
            pyobj['templateName'] = form_template.name
            pyobj['userName'] = str(request.user)
            pyobj['autofill'] = autofill
            pyobj['showSegs'] = showSegs
            pyobj['formView'] = formView
            utils.print_pyobj_to_json(pyobj, transcribed_json_path)

        with codecs.open(transcribed_json_path, mode="r",
                         encoding="utf-8") as fp:
            pyobj = json.load(fp, encoding='utf-8')
        json_outputs.append(pyobj)

    if form_template is None:
        messages.error("no template")
        return
    form_template_fp = codecs.open(form_template.json.path,
                                   mode="r",
                                   encoding="utf-8")
    json_template = json.load(form_template_fp, encoding='utf-8')
    return RequestContext(
        request, {
            'template_name': form_template.name,
            'json_template': json_template,
            'json_outputs': json_outputs,
            'user': request.user,
            'autofill': autofill,
            'showSegs': showSegs,
        })