def grade_me(request): user_id = request.LTI["hx_user_id"] context_id = request.LTI["hx_context_id"] collection_id = request.LTI["hx_collection_id"] object_id = request.LTI["hx_object_id"] params = { "source_id": object_id, "collection_id": collection_id, "context_id": context_id, "userid": user_id, } assignment = Assignment.objects.get(assignment_id=collection_id) search_database_url = str(assignment.annotation_database_url).strip() token = retrieve_token( user_id, assignment.annotation_database_apikey, assignment.annotation_database_secret_token, ) headers = { "x-annotator-auth-token": token, "content-type": "application/json", } response = requests.get( search_database_url, headers=headers, params=urllib.parse.urlencode(params, True), ) request_sent = False if response.status_code == 200: logger.info("Grade me search was made successfully %s" % str(response.url)) annotations = json.loads(response.text) if annotations["total"] > 0: logger.info("Should get a grade back") store = AnnotationStore.from_settings(request) store.lti_grade_passback() request_sent = True return HttpResponse( json.dumps({"grade_request_sent": request_sent}), content_type="application/json", )
def access_annotation_target(request, course_id, assignment_id, object_id, user_id=None, user_name=None, roles=None): """ Renders an assignment page """ if user_id is None: user_name = request.LTI['hx_user_name'] user_id = request.LTI['hx_user_id'] roles = request.LTI['hx_roles'] try: assignment = Assignment.objects.get(assignment_id=assignment_id) targ_obj = TargetObject.objects.get(pk=object_id) assignment_target = AssignmentTargets.objects.get( assignment=assignment, target_object=targ_obj) object_uri = targ_obj.get_target_content_uri() course_obj = LTICourse.objects.get(course_id=course_id) except Assignment.DoesNotExist or TargetObject.DoesNotExist: logger.error( "User attempted to access an Assignment or Target Object that does not exist: assignment_id={assignment_id} object_id={object_id}" .format(assignment_id=assignment_id, object_id=object_id)) raise AnnotationTargetDoesNotExist( 'Assignment or target object does not exist') try: is_instructor = request.LTI['is_staff'] except: is_instructor = False if not is_instructor and not assignment.is_published: raise PermissionDenied( 'Permission to access unpublished assignment by a non-instructor is denied' ) save_session(request, collection_id=assignment_id, object_id=object_id, object_uri=object_uri, context_id=course_id) # Dynamically pass in the address that the detail view will use to fetch annotations. # there's definitely a more elegant way (or a library function) to do this. # also, we may want to consider denying if theres no ssl protocol = 'https://' if request.is_secure() else 'http://' abstract_db_url = protocol + get_current_site(request).domain + reverse( 'annotation_store:api_root') logger.debug("DEBUG - Abstract Database URL: " + abstract_db_url) original = { 'user_id': user_id, 'username': user_name, 'is_instructor': request.LTI['is_staff'], 'collection': assignment_id, 'course': course_id, 'object': object_id, 'target_object': targ_obj, 'token': retrieve_token(user_id, assignment.annotation_database_apikey, assignment.annotation_database_secret_token), 'assignment': assignment, 'roles': [str(role) for role in roles], 'instructions': assignment_target.target_instructions, 'abstract_db_url': abstract_db_url, 'session': request.session.session_key, 'org': settings.ORGANIZATION, 'logger_url': settings.ANNOTATION_LOGGER_URL, } if not assignment.object_before(object_id) is None: original['prev_object'] = assignment.object_before(object_id) original['assignment_target'] = assignment_target if not assignment.object_after(object_id) is None: original['next_object'] = assignment.object_after(object_id) original['assignment_target'] = assignment_target if targ_obj.target_type == 'vd': srcurl = targ_obj.target_content if 'youtu' in srcurl: typeSource = 'video/youtube' else: disassembled = urlparse(srcurl) file_ext = os.path.splitext(os.path.basename(disassembled.path))[1] typeSource = 'video/' + file_ext.replace('.', '') original.update({'typeSource': typeSource}) elif targ_obj.target_type == 'ig': original.update({'osd_json': targ_obj.target_content}) viewtype = assignment_target.get_view_type_for_mirador() canvas_id = assignment_target.get_canvas_id_for_mirador() if viewtype is not None: original.update({'viewType': viewtype}) if canvas_id is not None: original.update({'canvas_id': canvas_id}) if assignment_target.target_external_css: original.update({'custom_css': assignment_target.target_external_css}) elif course_obj.course_external_css_default: original.update({'custom_css': course_obj.course_external_css_default}) original.update( {'dashboard_hidden': assignment_target.get_dashboard_hidden()}) original.update( {'transcript_hidden': assignment_target.get_transcript_hidden()}) original.update( {'transcript_download': assignment_target.get_transcript_download()}) original.update({'video_download': assignment_target.get_video_download()}) get_paras = {} for k in request.GET.keys(): get_paras[k] = request.GET[k] original.update(get_paras) return render(request, '%s/detail.html' % targ_obj.target_type, original)
def _retrieve_annotator_token(self, user_id): return retrieve_token(user_id, ANNOTATION_DB_API_KEY, ANNOTATION_DB_SECRET_TOKEN)
def transfer(request, instructor_only="1"): user_id = request.LTI['hx_user_id'] old_assignment_id = request.POST.get('old_assignment_id') new_assignment_id = request.POST.get('new_assignment_id') old_course_id = request.POST.get('old_course_id') new_course_id = request.POST.get('new_course_id') old_course = LTICourse.objects.get(course_id=old_course_id) new_course = LTICourse.objects.get(course_id=new_course_id) old_admins = [] new_admins = dict() for ads in old_course.course_admins.all(): old_admins.append(ads.anon_id) for ads in new_course.course_admins.all(): new_admins[ads.name] = ads.anon_id assignment = Assignment.objects.get(assignment_id=old_assignment_id) object_ids = request.POST.getlist('object_ids[]') token = retrieve_token(user_id, assignment.annotation_database_apikey, assignment.annotation_database_secret_token) types = {"ig": "image", "tx": "text", "vd": "video"} responses = [] for pk in object_ids: obj = TargetObject.objects.get(pk=pk) uri = pk target_type = types[obj.target_type] if target_type == "image": result = requests.get(obj.target_content) uri = json.loads(result.text)["sequences"][0]["canvases"][0]["@id"] search_database_url = str( assignment.annotation_database_url).strip() + '/search?' create_database_url = str( assignment.annotation_database_url).strip() + '/create' headers = { 'x-annotator-auth-token': token, 'content-type': 'application/json', } params = { 'uri': uri, 'contextId': old_course_id, 'collectionId': old_assignment_id, 'media': target_type, 'limit': -1, } if str(instructor_only) == "1": params.update({'userid': old_admins}) url_values = urllib.urlencode(params, True) response = requests.get(search_database_url, headers=headers, params=url_values) if response.status_code == 200: annotations = json.loads(response.text) for ann in annotations['rows']: ann['contextId'] = unicode(new_course_id) ann['collectionId'] = unicode(new_assignment_id) ann['id'] = None logger.info("annotation user_id: %s" % ann['user']['id']) if ann['user']['id'] in old_admins: try: if new_admins[ann['user']['name']]: ann['user']['id'] = new_admins[ann['user']['name']] except: ann['user']['id'] = user_id response2 = requests.post(create_database_url, headers=headers, data=json.dumps(ann)) #logger.debug("%s" % str(request.POST.getlist('assignment_inst[]'))) data = dict() return HttpResponse(json.dumps(data), content_type='application/json')
def transfer(request, instructor_only="1"): user_id = request.LTI['hx_user_id'] old_assignment_id = request.POST.get('old_assignment_id') new_assignment_id = request.POST.get('new_assignment_id') old_course_id = request.POST.get('old_course_id') new_course_id = request.POST.get('new_course_id') old_course = LTICourse.objects.get(course_id=old_course_id) new_course = LTICourse.objects.get(course_id=new_course_id) old_admins = [] new_admins = dict() for ads in old_course.course_admins.all(): old_admins.append(ads.anon_id) for ads in new_course.course_admins.all(): new_admins[ads.name] = ads.anon_id assignment = Assignment.objects.get(assignment_id=old_assignment_id) object_ids = request.POST.getlist('object_ids[]') token = retrieve_token( user_id, assignment.annotation_database_apikey, assignment.annotation_database_secret_token ) types = { "ig": "image", "tx": "text", "vd": "video" } responses = [] for pk in object_ids: obj = TargetObject.objects.get(pk=pk) uri = pk target_type = types[obj.target_type] if target_type == "image": result = requests.get(obj.target_content) uri = json.loads(result.text)["sequences"][0]["canvases"][0]["@id"] search_database_url = str(assignment.annotation_database_url).strip() + '/search?' create_database_url = str(assignment.annotation_database_url).strip() + '/create' headers = { 'x-annotator-auth-token': token, 'content-type': 'application/json', } params = { 'uri': uri, 'contextId': old_course_id, 'collectionId': old_assignment_id, 'media': target_type, 'limit': -1, } if str(instructor_only) == "1": params.update({'userid': old_admins}) url_values = urllib.urlencode(params, True) response = requests.get(search_database_url, headers=headers, params=url_values) if response.status_code == 200: annotations = json.loads(response.text) for ann in annotations['rows']: ann['contextId'] = unicode(new_course_id) ann['collectionId'] = unicode(new_assignment_id) ann['id'] = None logger.info("annotation user_id: %s" % ann['user']['id']) if ann['user']['id'] in old_admins: try: if new_admins[ann['user']['name']]: ann['user']['id'] = new_admins[ann['user']['name']] except: ann['user']['id'] = user_id response2 = requests.post(create_database_url, headers=headers, data=json.dumps(ann)) #logger.debug("%s" % str(request.POST.getlist('assignment_inst[]'))) data = dict() return HttpResponse(json.dumps(data), content_type='application/json')
def transfer(request, instructor_only="1"): user_id = request.LTI["hx_user_id"] old_assignment_id = request.POST.get("old_assignment_id") new_assignment_id = request.POST.get("new_assignment_id") old_course_id = request.POST.get("old_course_id") new_course_id = request.POST.get("new_course_id") old_course = LTICourse.objects.get(course_id=old_course_id) new_course = LTICourse.objects.get(course_id=new_course_id) old_admins = [] new_admins = dict() for ads in old_course.course_admins.all(): old_admins.append(ads.anon_id) for ads in new_course.course_admins.all(): new_admins[ads.name] = ads.anon_id assignment = Assignment.objects.get(assignment_id=old_assignment_id) object_ids = request.POST.getlist("object_ids[]") token = retrieve_token( user_id, assignment.annotation_database_apikey, assignment.annotation_database_secret_token, ) types = {"ig": "image", "tx": "text", "vd": "video"} responses = [] for pk in object_ids: obj = TargetObject.objects.get(pk=pk) uri = pk target_type = types[obj.target_type] if target_type == "image": result = requests.get(obj.target_content) uri = json.loads(result.text)["sequences"][0]["canvases"][0]["@id"] search_database_url = ( str(assignment.annotation_database_url).strip() + "/search?") create_database_url = ( str(assignment.annotation_database_url).strip() + "/create") headers = { "x-annotator-auth-token": token, "content-type": "application/json", } params = { "uri": uri, "contextId": old_course_id, "collectionId": old_assignment_id, "media": target_type, "limit": -1, } if str(instructor_only) == "1": params.update({"userid": old_admins}) url_values = urllib.parse.urlencode(params, True) response = requests.get(search_database_url, headers=headers, params=url_values) if response.status_code == 200: annotations = json.loads(response.text) for ann in annotations["rows"]: ann["contextId"] = str(new_course_id) ann["collectionId"] = str(new_assignment_id) ann["id"] = None logger.info("annotation user_id: %s" % ann["user"]["id"]) if ann["user"]["id"] in old_admins: try: if new_admins[ann["user"]["name"]]: ann["user"]["id"] = new_admins[ann["user"]["name"]] except Exception: ann["user"]["id"] = user_id response2 = requests.post(create_database_url, headers=headers, data=json.dumps(ann)) # logger.debug("%s" % str(request.POST.getlist('assignment_inst[]'))) data = dict() return HttpResponse(json.dumps(data), content_type="application/json")
def access_annotation_target( request, course_id, assignment_id, object_id, user_id=None, user_name=None, roles=None, ): """ Renders an assignment page """ if user_id is None: user_name = request.LTI["hx_user_name"] user_id = request.LTI["hx_user_id"] roles = request.LTI["hx_roles"] try: assignment = Assignment.objects.get(assignment_id=assignment_id) targ_obj = TargetObject.objects.get(pk=object_id) assignment_target = AssignmentTargets.objects.get( assignment=assignment, target_object=targ_obj) object_uri = targ_obj.get_target_content_uri() course_obj = LTICourse.objects.get(course_id=course_id) except Assignment.DoesNotExist or TargetObject.DoesNotExist: logger.error( "User attempted to access an Assignment or Target Object that does not exist: assignment_id={assignment_id} object_id={object_id}" .format(assignment_id=assignment_id, object_id=object_id)) raise AnnotationTargetDoesNotExist( "Assignment or target object does not exist") is_instructor = request.LTI.get("is_staff", False) if not is_instructor and not assignment.is_published: raise PermissionDenied( "Permission to access unpublished assignment by a non-instructor is denied" ) try: hide_sidebar = request.LTI["launch_params"][ "custom_hide_sidebar_instance"].split(",") except: hide_sidebar = [] save_session( request, collection_id=assignment_id, object_id=object_id, object_uri=object_uri, context_id=course_id, ) # Dynamically pass in the address that the detail view will use to fetch annotations. # there's definitely a more elegant way (or a library function) to do this. # also, we may want to consider denying if theres no ssl protocol = "https://" if request.is_secure() else "http://" abstract_db_url = (protocol + get_current_site(request).domain + reverse("annotation_store:api_root")) logger.debug("DEBUG - Abstract Database URL: " + abstract_db_url) original = { "user_id": user_id, "username": user_name, "is_instructor": request.LTI["is_staff"], "collection": assignment_id, "course": course_id, "object": object_id, "target_object": targ_obj, "token": retrieve_token( user_id, assignment.annotation_database_apikey, assignment.annotation_database_secret_token, ), "assignment": assignment, "roles": [str(role) for role in roles], "instructions": assignment_target.target_instructions, "abstract_db_url": abstract_db_url, "session": request.session.session_key, "org": settings.ORGANIZATION, "logger_url": settings.ANNOTATION_LOGGER_URL, "accessibility": settings.ACCESSIBILITY, "hide_sidebar_instance": hide_sidebar, "is_graded": request.LTI["launch_params"].get("lis_outcome_service_url", None) is not None, } if not assignment.object_before(object_id) is None: original["prev_object"] = assignment.object_before(object_id) original["assignment_target"] = assignment_target if not assignment.object_after(object_id) is None: original["next_object"] = assignment.object_after(object_id) original["assignment_target"] = assignment_target if targ_obj.target_type == "vd": srcurl = targ_obj.target_content if "youtu" in srcurl: typeSource = "video/youtube" else: disassembled = urlparse(srcurl) file_ext = os.path.splitext(os.path.basename(disassembled.path))[1] typeSource = "video/" + file_ext.replace(".", "") original.update({"typeSource": typeSource}) elif targ_obj.target_type == "ig": original.update({"osd_json": targ_obj.target_content}) viewtype = assignment_target.get_view_type_for_mirador() canvas_id = assignment_target.get_canvas_id_for_mirador() logger.debug("CANVAS: %s" % canvas_id) if viewtype is not None: original.update({"viewType": viewtype}) if canvas_id is not None: original.update({"canvas_id": canvas_id}) if assignment_target.target_external_css: original.update({"custom_css": assignment_target.target_external_css}) elif course_obj.course_external_css_default: original.update({"custom_css": course_obj.course_external_css_default}) original.update( {"dashboard_hidden": assignment_target.get_dashboard_hidden()}) original.update( {"transcript_hidden": assignment_target.get_transcript_hidden()}) original.update( {"transcript_download": assignment_target.get_transcript_download()}) original.update({"video_download": assignment_target.get_video_download()}) get_paras = {} for k in request.GET.keys(): get_paras[k] = request.GET[k] original.update(get_paras) if (targ_obj.target_type == "tx" or targ_obj.target_type == "ig") and assignment.use_hxighlighter: return render(request, "%s/detail_hxighlighter.html" % targ_obj.target_type, original) else: return render(request, "%s/detail.html" % targ_obj.target_type, original)