Example #1
0
def collect_page(request, access_code):
    """
    Collection Interface.
    """
    template = loader.get_template('annotator/collect_page.html')
    user = safe_get_user(access_code)

    utility = request.GET.get('utility')
    tag = get_tag(utility)
    url = get_url(request.GET.get('url'))

    # search for existing annotations
    annotation_dict = {}
    command_list = []

    if user.is_judger:
        # judger sees the annotations of a web page by all other users
        annotation_list = Annotation.objects.filter(url=url)
    else:
        # annotator only sees the annotations of a web page submitted by
        # themselves
        annotation_list = Annotation.objects.filter(url=url, annotator=user)
        for command in url.commands.all():
            if command.tags.filter(str=tag.str).exists():
                if not Annotation.objects.filter(cmd__template=command.template,
                        annotator=user).exists():
                    command_list.append(command.str)

    for annotation in annotation_list:
        key = '__NL__{}__Command__{}'.format(annotation.nl.str, annotation.cmd.str)
        if not key in annotation_dict:
            annotation_dict[key] = (annotation.id, annotation.cmd.str, annotation.nl.str)

    annotation_list = sorted(annotation_dict.values(), key=lambda x: x[0])

    hypothes_prefix = "https://via.hypothes.is/"
    context = {
        'utility': utility,
        'url': hypothes_prefix + url.str,
        'annotation_list': annotation_list,
        'command_list': command_list,
        'completed': False,
        'access_code': access_code,
        'is_judger': user.is_judger == True
    }

    try:
        record = AnnotationProgress.objects.get(
            annotator=user, tag=get_tag(utility), url=url)
        if record.status == 'completed':
            context['completed'] = True
    except ObjectDoesNotExist:
        pass

    return HttpResponse(template.render(context=context, request=request))
Example #2
0
def next_url(request, access_code):
    user = safe_get_user(access_code)
    utility = request.GET.get('utility')
    tag = get_tag(utility)
    current_url = request.GET.get('url')

    is_current_url = False
    next_url = None
    for url_tag in URLTag.objects.filter(tag=utility).order_by('url__str'):
        if is_current_url:
            if not user.is_judger and \
                    get_num_commands_missed_url(url_tag.url, tag, access_code) == 0:
                continue
            else:
                next_url = url_tag.url
                break
        else:
            if url_tag.url.str == current_url:
                is_current_url = True

    if next_url is not None:
        resp = json_response({'url': next_url.str}, status='NEXT_URL_SUCCESS')
    else:
        if is_current_url:
            resp = json_response(status='IS_LAST_URL')
        else:
            resp = json_response(status='URL_DOES_NOT_EXIST')

    return resp
Example #3
0
def previous_url(request, access_code):
    user = safe_get_user(access_code)
    utility = request.GET.get('utility')
    tag = get_tag(utility)
    current_url = request.GET.get('url')

    is_current_url = False
    prev_url = None

    for url_tag in URLTag.objects.filter(tag=utility).order_by('url__str'):
        if url_tag.url.str == current_url:
            is_current_url = True
            break
        if user.is_judger or \
                get_num_commands_missed_url(url_tag.url, tag, access_code) > 0:
            prev_url = url_tag.url

    if prev_url is not None:
        resp = json_response({'url': prev_url.str}, status="PREVIOUS_URL_SUCCESS")
    else:
        if is_current_url:
            resp = json_response(status='IS_FIRST_URL')
        else:
            resp = json_response(status='URL_DOES_NOT_EXIST')

    return resp
Example #4
0
def populate_tag_commands():
    for tag in Tag.objects.all():
        tag.commands.clear()
    for url_tag in URLTag.objects.all():
        print(url_tag.url.str)
        tag = get_tag(url_tag.tag)
        for cmd in url_tag.url.commands.all():
            if tag in cmd.tags.all():
                tag.commands.add(cmd)
        tag.save()
Example #5
0
def populate_command_tags():
    for cmd in Command.objects.all():
        if len(cmd.str) > 600:
            cmd.delete()
        else:
            cmd.tags.clear()
            print(cmd.str)
            ast = data_tools.bash_parser(cmd.str)
            for utility in data_tools.get_utilities(ast):
                print(utility)
                cmd.tags.add(get_tag(utility))
            cmd.save()
Example #6
0
def update_progress(request, access_code):
    user = User.objects.get(access_code=access_code)
    tag = get_tag(request.GET.get('utility'))
    url = get_url(request.GET.get('url'))
    status = request.GET.get('status')

    try:
        record = AnnotationProgress.objects.get(
            annotator=user, tag=tag, url=url)
        record.status = status
        record.save()
    except ObjectDoesNotExist:
        AnnotationProgress.objects.create(
            annotator=user, tag=tag, url=url, status=status)

    return json_response(status='PROGRESS_UPDATED')
Example #7
0
def submit_annotation(request, access_code):
    user = User.objects.get(access_code=access_code)
    url = get_url(request.GET.get('url'))
    nl = get_nl(request.GET.get('nl'))
    tag = get_tag(request.GET.get('utility'))
    command = get_command(request.GET.get('command'))
    url.tags.add(tag)

    annotation = Annotation.objects.create(
        url=url, nl=nl, cmd=command, annotator=user)
    tag.annotations.add(annotation)

    if not AnnotationProgress.objects.filter(annotator=user, url=url, tag=tag):
        AnnotationProgress.objects.create(
            annotator=user, url=url, tag=tag, status='in-progress')

    resp = json_response({'nl': annotation.nl.str, 'command': annotation.cmd.str},
                         status='ANNOTATION_SAVED')

    return resp
Example #8
0
def get_utility_stats(request, access_code):
    utility = request.GET.get('utility')
    user = safe_get_user(access_code)
    tag = get_tag(utility)
    url_set = URLTag.objects.filter(tag=utility)
    num_urls = url_set.count()
    num_pairs_annotated = tag.annotations.all().count()
    annotated_url_set = AnnotationProgress.objects.filter(tag=tag)
    num_urls_annotated = annotated_url_set.count()
    completed_url_set = annotated_url_set.filter(status='completed')
    num_urls_completed = completed_url_set.count() + 0.0
    num_urls_completed_by_user = completed_url_set.filter(
            annotator__access_code=access_code).count() + 0.0
    if utility in GREY_LIST:
        num_commands = 0
        num_commands_annotated = 0
        completion_ratio = -1
        self_completion_ratio = -1
    else:
        completion_ratio = num_urls_completed / num_urls if num_urls > 0 else 0
        self_completion_ratio = num_urls_completed_by_user / num_urls \
            if num_urls > 0 else 0
        num_commands = 0
        num_commands_annotated = 0
        if self_completion_ratio > 0:
            for command in tag.commands.all():
                num_commands += 1
                if Annotation.objects.filter(cmd__template=command.template, 
                        annotator=user).exists():
                    num_commands_annotated += 1

    return json_response({
        'num_urls': num_urls,
        'num_urls_annotated': num_urls_annotated,
        'num_pairs_annotated': num_pairs_annotated,
        'completion_ratio': completion_ratio,
        'self_completion_ratio': self_completion_ratio,
        'num_commands_missing': (num_commands - num_commands_annotated)
    }, status='UTILITY_STATS_SUCCESS')
Example #9
0
def get_url_stats(request, access_code):
    url = get_url(request.GET.get('url'))
    tag = get_tag(request.GET.get('utility'))

    # check if any commands were missed
    num_commands_missed = get_num_commands_missed_url(url, tag, access_code)

    # check if there are notifications sent from other annotators
    num_notifications = Notification.objects.filter(url=url,
            receiver__access_code=access_code, status='issued').count()

    try:
        record = AnnotationProgress.objects.get(
            annotator__access_code=access_code, tag=tag, url=url)
        record_status = record.status
    except ObjectDoesNotExist:
        record_status = ''

    return json_response({
            'status': record_status,
            'num_commands_missed': num_commands_missed,
            'num_notifications': num_notifications
        }, status='URL_STATS_SUCCESS')