def read_override(self,obj,username,role): if resolve_type(obj["@graph"]["dc:type"]) in ["course_collection","themed_collection"]: return role=="student" and is_enrolled(obj) elif resolve_type(self.bundle["@graph"]["dc:type"]) in ["course_collection","themed_collection"]: return role=="student" and (is_enrolled(self.bundle) or username in self.bundle["@graph"]["dc:rights"]["read"]) else: return False
def test_is_enrolled(self): """Query db to see if course is currently being taken by user.""" jane = get_user_by_email("*****@*****.**") assert is_enrolled(jane, 1) is False assert is_enrolled(jane, 2) is False assert is_enrolled(jane, 3) is True
def serialize_bundle(self,payload): if payload: v=assets.find({"@graph.ma:isMemberOf.@id":payload["_id"]}) payload["@graph"]["videos"]=[] if not is_enrolled(payload) and not (payload['@graph']['dc:coverage'] == 'public'): v=self.auth_filter(v) thumbRetriever=[] for vid in v: if self.request.args.get("full",False): resource=uri_pattern(vid["@graph"]["pid"],config.APIHOST+"/video") vid["@graph"]["type"]=resolve_type(vid["@graph"]["dc:type"]) vid["@graph"]["resource"]=resource vid["@graph"]["ma:image"]=[] annot=self.find_by_relations(vid['@graph']['pid'], payload['_id']) try: vid['@graph']['transcript']=bool(annot['@graph']['vcp:playSettings']['vcp:showTranscript']) except (TypeError, KeyError): vid['@graph']['transcript']=False if vid["@graph"]["type"]=="humvideo": needs_ext=True elif 'type' in payload['@graph'] and payload['@graph']['type']=='humaudio': needs_ext=True elif vid["@graph"]["type"]=="yt": needs_ext=False for location in vid["@graph"]["ma:locator"]: if needs_ext: poster=uri_pattern(location["@id"]+".jpg",config.HOST+"/posters") thumb=uri_pattern(location["@id"]+"_thumb.jpg",config.HOST+"/posters") vid["@graph"]["ma:image"].append({"poster":poster,"thumb":thumb}) else: loc=location["@id"] vid["@graph"]["ma:image"].append({"ytId":loc}) thumbRetriever.append(loc) payload["@graph"]['videos'].append(vid["@graph"]) else: payload["@graph"]["videos"].append(assets.Video.make_part(vid["@graph"],config.APIHOST,self.request.args.get("part","details"))) thumbRetriever.extend(payload["@graph"]["videos"][-1].get("fromYt")) payload["@graph"]["videos"][-1].pop("fromYt",None) payload["@graph"]["type"]=resolve_type(payload["@graph"]["dc:type"]) ytThumbs=getYtThumbs(thumbRetriever) for vid in payload["@graph"]["videos"]: for image in vid["ma:image"]: if image.get("ytId"): try: image["thumb"] = ytThumbs[image["ytId"]]["thumb"] image["poster"] = ytThumbs[image["ytId"]]["poster"] except KeyError: image["thumb"] = None image["poster"] = None image.pop("ytId",None) return mongo_jsonify(payload["@graph"]) else: return mongo_jsonify({})
def serialize_bundle(self,payload): if payload: v=assets.find({"@graph.ma:isMemberOf.@id":payload["_id"]}) payload["@graph"]["videos"]=[] if not is_enrolled(payload) and not (payload['@graph']['dc:coverage'] == 'public'): v=self.auth_filter(v) thumbRetriever=[] for vid in v: if self.request.args.get("full",False): resource=uri_pattern(vid["@graph"]["pid"],config.APIHOST+"/video") vid["@graph"]["type"]=resolve_type(vid["@graph"]["dc:type"]) vid["@graph"]["resource"]=resource vid["@graph"]["ma:image"]=[] annot=self.find_by_relations(vid['@graph']['pid'], payload['_id']) try: vid['@graph']['transcript']=bool(annot['@graph']['vcp:playSettings']['vcp:showTranscript']) except (TypeError, KeyError): vid['@graph']['transcript']=False if vid["@graph"]["type"]=="humvideo": needs_ext=True elif vid["@graph"]["type"]=="yt": needs_ext=False for location in vid["@graph"]["ma:locator"]: if needs_ext: poster=uri_pattern(location["@id"]+".jpg",config.HOST+"/posters") thumb=uri_pattern(location["@id"]+"_thumb.jpg",config.HOST+"/posters") vid["@graph"]["ma:image"].append({"poster":poster,"thumb":thumb}) else: loc=location["@id"] vid["@graph"]["ma:image"].append({"ytId":loc}) thumbRetriever.append(loc) payload["@graph"]['videos'].append(vid["@graph"]) else: payload["@graph"]["videos"].append(assets.Video.make_part(vid["@graph"],config.APIHOST,self.request.args.get("part","details"))) thumbRetriever.extend(payload["@graph"]["videos"][-1].get("fromYt")) payload["@graph"]["videos"][-1].pop("fromYt",None) payload["@graph"]["type"]=resolve_type(payload["@graph"]["dc:type"]) ytThumbs=getYtThumbs(thumbRetriever) for vid in payload["@graph"]["videos"]: for image in vid["ma:image"]: if image.get("ytId"): image["thumb"] = ytThumbs[image["ytId"]]["thumb"] image["poster"] = ytThumbs[image["ytId"]]["poster"] image.pop("ytId",None) return mongo_jsonify(payload["@graph"]) else: return mongo_jsonify({})
def read_override(self,obj,username,role): from auth import get_profile atts=get_profile() if atts['superuser']: return True for c in obj["@graph"]["ma:isMemberOf"]: coll=ags.find_one({"_id":c["@id"]}) try: if coll["@graph"].get("dc:creator")==atts['username'] or atts['username'] in coll['@graph']["dc:rights"]["read"] or coll['@graph']['dc:coverage'] == 'public': return True except TypeError: pass if is_enrolled(coll): return True return False
def bookmark_course(): """Add favorited course of user to courses_favorited table.""" if is_user(): email = session.get("current_user") user = get_user_by_email(email) course_id = request.form.get("id") if is_favorited(user, course_id): alert = "You have already added this course to your \ favorites list!" elif is_taken(user, course_id): alert = "You have already added this course to your \ courses taken list!" elif is_enrolled(user, course_id): alert = "You are currently enrolled in this course!" else: action = request.form.get("action") if action == "favorite": new_course = Course_Favorited(user_id=user.user_id, course_id=course_id) alert = "You have successfully added this course to your favorites!" elif action == "enrolled": new_course = Course_Taking(user_id=user.user_id, course_id=course_id) alert = "You have successfully added this course to your enrolled courses list!" elif action == "taken": new_course = Course_Taken(user_id=user.user_id, course_id=course_id) alert = "You have successfully added this course to your taken courses list!" db.session.add(new_course) db.session.commit() else: alert = "You must be signed in to add this course." return jsonify({'alert': alert})