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
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
def user_login(request): access_code = request.GET.get('access_code') if User.objects.filter(access_code=access_code): resp = json_response({'access_code': access_code}, status='LOGIN_SUCCESS') resp.set_cookie('access_code', access_code) else: resp = json_response(status='USER_DOES_NOT_EXIST') return resp
def submit_annotation_update(request, access_code): user = User.objects.get(access_code=access_code) annotation_id = request.GET.get('annotation_id') update_id = request.GET.get('update_id') update_str = request.GET.get('update') comment_str = request.GET.get('comment') annotation = Annotation.objects.get(id=annotation_id) if update_id: pre_update = AnnotationUpdate.objects.get(id=update_id) receiver = pre_update.judger else: receiver = annotation.annotator comment = Comment.objects.create(user=user, str=comment_str) update = AnnotationUpdate.objects.create(annotation=annotation, judger=user, update_str=update_str, comment=comment) # create notification Notification.objects.create(sender=user, receiver=receiver, type='annotation_update', annotation_update=update, url=annotation.url) return json_response({ 'update_id': update.id, 'access_code': access_code, 'submission_time': update.submission_time, }, status='ANNOTATION_UPDATE_SAVE_SUCCESS')
def example_requests_with_translations(request): example_requests_with_translations = [] example_request_list = [ 'remove all pdfs in my current directory', 'delete all *.txt files in "myDir/"', 'list files in "myDir/" that have been modified within 24 hours', 'find all files named "test*.cpp" and move them to "project/code/"', 'find all files larger than a gigabyte in the current folder', 'find all png files larger than 50M that were last modified more than 30 days ago' ] for request_str in example_request_list: nl = get_nl(request_str) if Translation.objects.filter(nl__str=request_str).exists(): translations = Translation.objects.filter(nl__str=request_str) max_score = translations.aggregate(Max('score'))['score__max'] for top_translation in Translation.objects.filter( nl__str=request_str, score=max_score): break else: # Compute the translations on the fly top_translation = None if not WEBSITE_DEVELOP: # call learning model and store the translations batch_outputs, output_logits = translate_fun(request_str) max_score = -np.inf if batch_outputs: top_k_predictions = batch_outputs[0] top_k_scores = output_logits[0] for i in range(len(top_k_predictions)): pred_tree, pred_cmd = top_k_predictions[i] score = top_k_scores[i] if score > max_score: max_score = score cmd = get_command(pred_cmd) top_translation = Translation.objects.create( nl=nl, pred_cmd=cmd, score=score) if top_translation: example_requests_with_translations.append({ 'nl': nl.str, 'top_translation': top_translation.pred_cmd.str, 'tags': [ tag.str for tag in top_translation.pred_cmd.tags.all().order_by('frequency') ] }) else: example_requests_with_translations.append({ 'nl': nl.str, 'top_translation': 'No translation available.', 'tags': [] }) return json_response({ 'example_requests_with_translations': example_requests_with_translations })
def mark_i_dont_know(request, access_code): user = User.objects.get(access_code=access_code) url = get_url(request.GET.get('url')) cmd = get_command(request.GET.get('command')) url.commands.remove(cmd); Annotation.objects.create( url=url, nl=get_nl('I DON\'T KNOW'), cmd=cmd, annotator=user) return json_response(status='MARK_I_DONT_KNOW_SUCCESS')
def mark_wrong(request, access_code): user = User.objects.get(access_code=access_code) url = get_url(request.GET.get('url')) cmd = get_command(request.GET.get('command')) url.commands.remove(cmd); Annotation.objects.create( url=url, nl=get_nl('ERROR'), cmd=cmd, annotator=user) return json_response(status='MARK_WRONG_SUCCESS')
def get_url_num_notifications(request, access_code): url_str = request.GET.get('url') num_notifications = Notification.objects.filter(url__str=url_str, receiver__access_code=access_code, status='issued').count() return json_response({ 'num_notifications': num_notifications }, status='GET_URL_NUM_NOTIFICATIONS_SUCCESS')
def register_user(request): first_name = request.GET.get('firstname') last_name = request.GET.get('lastname') ip_address = request.GET.get('ip_address') roles = request.GET.get('roles').split() if User.objects.filter(first_name=first_name, last_name=last_name): resp = json_response({'firstname': first_name, 'lastname': last_name}, status='USER_EXISTS') else: access_code = first_name.lower() + '-' + last_name.lower() user = User.objects.create(access_code=access_code, first_name=first_name, last_name=last_name, ip_address=ip_address) for role in roles: if role == 'annotator': user.is_annotator = True elif role == 'judger': user.is_judger = True resp = json_response({'firstname': first_name, 'lastname': last_name, 'access_code': access_code}, status='REGISTRATION_SUCCESS') return resp
def get_utility_num_notifications(request, access_code): utility = request.GET.get('utility') num_notifications = 0 if not utility in GREY_LIST: for url_tag in URLTag.objects.filter(tag=utility): url = url_tag.url num_notifications += Notification.objects.filter(url=url, receiver__access_code=access_code, status='issued').count() return json_response({ 'num_notifications': num_notifications }, status='GET_UTILITY_NUM_NOTIFICATIONS_SUCCESS')
def delete_annotation(request, access_code): url = get_url(request.GET.get('url')) nl = get_nl(request.GET.get('nl')) command = get_command(request.GET.get('command')) for annotation in Annotation.objects.filter(url=url, nl=nl, cmd=command): for tag in command.tags.all(): tag.annotations.filter(id=annotation.id) tag.save() annotation.delete() return json_response(status='DELETION_SUCCESS')
def update_user_time_logged(request, access_code): ac_code = request.GET.get('ac_code') time_logged = request.GET.get('time_logged') user = User.objects.get(access_code=ac_code) user.time_logged = float(time_logged) user.save() context = {} num_annotations = Annotation.objects.filter(annotator=user).count() context['num_annotations_per_hour'] = 0 if user.time_logged <= 0 \ else round(num_annotations / user.time_logged * 3600, 1) print(num_annotations) return json_response(context, status='UPDATE_USER_TIME_LOGGED_SUCCESS')
def reject_update(request, access_code): update_id = request.GET.get('update_id') update = AnnotationUpdate.objects.get(id=update_id) # remove annotation update notification flag notification = Notification.objects.get(annotation_update=update) notification.status = 'cleared' notification.save() update.status = 'rejected' update.save() return json_response(status='REJECT_UPDATE_SUCCESS')
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')
def submit_edit(request, access_code): user = User.objects.get(access_code=access_code) url = get_url(request.GET.get('url')) original_nl = get_nl(request.GET.get('original_nl')) original_command = get_command(request.GET.get('original_command')) nl = get_nl(request.GET.get('nl')) command = get_command(request.GET.get('command')) Annotation.objects.filter(url=url, nl=original_nl, cmd=original_command).delete() annotation = Annotation.objects.create(url=url, nl=nl, cmd=command, annotator=user) resp = json_response({'nl': annotation.nl.str, 'command': annotation.cmd.str}, status='EDIT_SAVED') return resp
def get_relevant_updates(request, access_code): annotation_id = request.GET.get('annotation_id') annotation = Annotation.objects.get(id=annotation_id) update_list = [] for update in AnnotationUpdate.objects.filter(annotation=annotation): if annotation.annotator.access_code == access_code or \ update.judger.access_code == access_code: # Only the original annotator or the update submitter can view # the updates and their replies update_list.append({ 'update_id': update.id, 'annotation_access_code': annotation.annotator.access_code, 'access_code': update.judger.access_code, 'submission_time': update.submission_time, 'update_str': update.update_str, 'comment_str': update.comment.str }) return json_response({'update_list': update_list})
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
def accept_update(request, access_code): update_id = request.GET.get('update_id') update = AnnotationUpdate.objects.get(id=update_id) annotation = update.annotation update_str = update.update_str old_annotation_nl = annotation.nl.str annotation.nl = get_nl(update_str) annotation.save() # remove annotation update notification flag notification = Notification.objects.get(annotation_update=update) notification.status = 'cleared' notification.save() update.status = 'accepted' update.save() return json_response({ 'old_annotation_nl': old_annotation_nl, 'updated_str': update_str }, status='ACCEPT_UPDATE_SUCCESS')
def latest_requests_with_translations(request): latest_requests_with_translations = [] max_num_translation = 0 for request in NLRequest.objects.order_by('-submission_time'): translations = Translation.objects.filter(nl=request.nl) top_translation = None if translations: max_score = translations.aggregate(Max('score'))['score__max'] for top_translation in Translation.objects.filter(nl=request.nl, score=max_score): break top_translation_tags = [tag.str for tag in top_translation.pred_cmd.tags.all()] \ if top_translation else [] top_translation_cmd = top_translation.pred_cmd.str if top_translation \ else 'No translation available.' latest_requests_with_translations.append({ 'nl': request.nl.str, 'tags': top_translation_tags, 'top_translation': top_translation_cmd, 'submission_time': request.submission_time.strftime("%Y-%m-%d %H:%M:%S"), 'user_city': request.user.city, 'user_region': request.user.region, 'user_country': request.user.country }) max_num_translation += 1 if max_num_translation % 20 == 0: break return json_response({ 'latest_requests_with_translations': latest_requests_with_translations })
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')
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')
def user_logout(request, access_code): resp = json_response(status='LOGOUT_SUCCESS') resp.delete_cookie('access_code') return resp
def get_update_status(request, access_code): update_id = request.GET.get('update_id') update = AnnotationUpdate.objects.get(id=update_id) return json_response({'update_status': update.status})
def retract_update(request, access_code): update_id = request.GET.get('update_id') AnnotationUpdate.objects.get(id=update_id).delete() return json_response(status='RETRACT_UPDATE_SUCCESS')