def is_repo_file_accessible_as(access_kind, repo, commit_sha, path): """ :arg commit_sha: A byte string containing the commit hash """ from os.path import dirname, basename, join attributes_path = join(dirname(path), ".attributes.yml") from course.content import get_raw_yaml_from_repo try: attributes = get_raw_yaml_from_repo(repo, attributes_path, commit_sha.encode()) except ObjectDoesNotExist: # no attributes file: not public return False path_basename = basename(path) access_patterns = attributes.get(access_kind, []) from fnmatch import fnmatch if isinstance(access_patterns, list): for pattern in access_patterns: if isinstance(pattern, six.string_types): if fnmatch(path_basename, pattern): return True return False
def is_repo_file_accessible_as(access_kind, repo, commit_sha, path): """ :arg commit_sha: A byte string containing the commit hash """ from os.path import dirname, basename, join attributes_path = join(dirname(path), ".attributes.yml") from course.content import get_raw_yaml_from_repo try: attributes = get_raw_yaml_from_repo( repo, attributes_path, commit_sha.encode()) except ObjectDoesNotExist: # no attributes file: not public return False path_basename = basename(path) access_patterns = attributes.get(access_kind, []) from fnmatch import fnmatch if isinstance(access_patterns, list): for pattern in access_patterns: if isinstance(pattern, six.string_types): if fnmatch(path_basename, pattern): return True return False
def is_repo_file_public(repo, commit_sha, path): from os.path import dirname, basename, join attributes_path = join(dirname(path), ".attributes.yml") from course.content import get_raw_yaml_from_repo try: attributes = get_raw_yaml_from_repo(repo, attributes_path, commit_sha.encode()) except ObjectDoesNotExist: # no attributes file: not public return False path_basename = basename(path) public_patterns = attributes.get("public", []) from fnmatch import fnmatch if isinstance(public_patterns, list): for pattern in attributes.get("public", []): if isinstance(pattern, (str, unicode)): if fnmatch(path_basename, pattern): return True return False
def is_repo_file_accessible_as(access_kind, repo, commit_sha, path): """ Check of a file in a repo directory is accessible. For example, 'instructor' can access anything listed in the attributes. 'student' can access 'student' and 'unenrolled'. The 'unenrolled' role can only access 'unenrolled'. :arg commit_sha: A byte string containing the commit hash """ # set the path to .attributes.yml from os.path import dirname, basename, join attributes_path = join(dirname(path), ".attributes.yml") # retrieve the .attributes.yml structure from course.content import get_raw_yaml_from_repo try: attributes = get_raw_yaml_from_repo(repo, attributes_path, commit_sha) except ObjectDoesNotExist: # no attributes file: not accessible return False path_basename = basename(path) # [unenrolled, student, ta, instructor] # access_kind hierarchy and who should be allowed in sets: # in_exam : in_exam # instructor : [public, unenrolled, student, ta, instructor] # ta : [public, unenrolled, student, ta] # student : [public, unenrolled, student] # unenrolled : [public, unenrolled] # "public" is a deprecated alias for "unenrolled". if access_kind == 'in_exam': kind_list = ['in_exam'] elif access_kind == ('unenrolled' or 'public'): kind_list = ['public', 'unenrolled'] elif access_kind == 'student': kind_list = ['public', 'unenrolled', 'student'] elif access_kind == 'ta': kind_list = ['public', 'unenrolled', 'student', 'ta'] elif access_kind == 'instructor': kind_list = ['public', 'unenrolled', 'student', 'ta', 'instructor'] access_patterns = [] for kind in kind_list: access_patterns += attributes.get(kind, []) from fnmatch import fnmatch if isinstance(access_patterns, list): for pattern in access_patterns: if isinstance(pattern, six.string_types): if fnmatch(path_basename, pattern): return True return False
def is_repo_file_public(repo, commit_sha, path): from os.path import dirname, basename, join attributes_path = join(dirname(path), ".attributes.yml") from course.content import get_raw_yaml_from_repo try: attributes = get_raw_yaml_from_repo( repo, attributes_path, commit_sha.encode()) except ObjectDoesNotExist: # no attributes file: not public return False path_basename = basename(path) public_patterns = attributes.get("public", []) from fnmatch import fnmatch if isinstance(public_patterns, list): for pattern in attributes.get("public", []): if isinstance(pattern, (str, unicode)): if fnmatch(path_basename, pattern): return True return False
def view_calendar(pctx): from course.content import markup_to_html events_json = [] from course.content import get_raw_yaml_from_repo try: event_descr = get_raw_yaml_from_repo(pctx.repo, pctx.course.events_file, pctx.course_commit_sha) except ObjectDoesNotExist: event_descr = {} event_kinds_desc = event_descr.get("event_kinds", {}) event_info_desc = event_descr.get("events", {}) event_info_list = [] for event in (Event.objects.filter( course=pctx.course, shown_in_calendar=True).order_by("time")): kind_desc = event_kinds_desc.get(event.kind) human_title = unicode(event) event_json = { "id": event.id, "start": event.time.isoformat(), "allDay": event.all_day, } if event.end_time is not None: event_json["end"] = event.end_time.isoformat() if kind_desc is not None: if "color" in kind_desc: event_json["color"] = kind_desc["color"] if "title" in kind_desc: if event.ordinal is not None: human_title = kind_desc["title"].format(nr=event.ordinal) else: human_title = kind_desc["title"] description = None event_desc = event_info_desc.get(unicode(event)) if event_desc is not None: if "description" in event_desc: description = markup_to_html(pctx.course, pctx.repo, pctx.course_commit_sha, event_desc["description"]) if "title" in event_desc: human_title = event_desc["title"] if "color" in event_desc: human_title = event_desc["color"] event_json["title"] = human_title if description: event_json["url"] = "#event-%d" % event.id start_time = event.time end_time = event.end_time if event.all_day: start_time = start_time.date() end_time = end_time.date() event_info_list.append( EventInfo(id=event.id, human_title=human_title, start_time=start_time, end_time=end_time, description=description)) events_json.append(event_json) from json import dumps return render_course_page(pctx, "course/calendar.html", { "events_json": dumps(events_json), "event_info_list": event_info_list, })
def view_calendar(pctx): from course.content import markup_to_html, parse_date_spec from course.views import get_now_or_fake_time now = get_now_or_fake_time(pctx.request) if not pctx.has_permission(pperm.view_calendar): raise PermissionDenied(_("may not view calendar")) events_json = [] from course.content import get_raw_yaml_from_repo try: event_descr = get_raw_yaml_from_repo(pctx.repo, pctx.course.events_file, pctx.course_commit_sha) except ObjectDoesNotExist: event_descr = {} event_kinds_desc = event_descr.get("event_kinds", {}) event_info_desc = event_descr.get("events", {}) event_info_list = [] events = sorted( Event.objects .filter( course=pctx.course, shown_in_calendar=True), key=lambda evt: ( -evt.time.year, -evt.time.month, -evt.time.day, evt.time.hour, evt.time.minute, evt.time.second)) for event in events: kind_desc = event_kinds_desc.get(event.kind) human_title = six.text_type(event) event_json = { "id": event.id, "start": event.time.isoformat(), "allDay": event.all_day, } if event.end_time is not None: event_json["end"] = event.end_time.isoformat() if kind_desc is not None: if "color" in kind_desc: event_json["color"] = kind_desc["color"] if "title" in kind_desc: if event.ordinal is not None: human_title = kind_desc["title"].format(nr=event.ordinal) else: human_title = kind_desc["title"] description = None show_description = True event_desc = event_info_desc.get(six.text_type(event)) if event_desc is not None: if "description" in event_desc: description = markup_to_html( pctx.course, pctx.repo, pctx.course_commit_sha, event_desc["description"]) if "title" in event_desc: human_title = event_desc["title"] if "color" in event_desc: event_json["color"] = event_desc["color"] if "show_description_from" in event_desc: ds = parse_date_spec( pctx.course, event_desc["show_description_from"]) if now < ds: show_description = False if "show_description_until" in event_desc: ds = parse_date_spec( pctx.course, event_desc["show_description_until"]) if now > ds: show_description = False event_json["title"] = human_title if show_description and description: event_json["url"] = "#event-%d" % event.id start_time = event.time end_time = event.end_time if event.all_day: start_time = start_time.date() if end_time is not None: local_end_time = as_local_time(end_time) end_midnight = datetime.time(tzinfo=local_end_time.tzinfo) if local_end_time.time() == end_midnight: end_time = (end_time - datetime.timedelta(days=1)).date() else: end_time = end_time.date() event_info_list.append( EventInfo( id=event.id, human_title=human_title, start_time=start_time, end_time=end_time, description=description )) events_json.append(event_json) default_date = now.date() if pctx.course.end_date is not None and default_date > pctx.course.end_date: default_date = pctx.course.end_date from json import dumps return render_course_page(pctx, "course/calendar.html", { "events_json": dumps(events_json), "event_info_list": event_info_list, "default_date": default_date.isoformat(), })
def view_calendar(pctx): from course.content import markup_to_html, parse_date_spec from course.views import get_now_or_fake_time now = get_now_or_fake_time(pctx.request) if not pctx.has_permission(pperm.view_calendar): raise PermissionDenied(_("may not view calendar")) events_json = [] from course.content import get_raw_yaml_from_repo try: event_descr = get_raw_yaml_from_repo(pctx.repo, pctx.course.events_file, pctx.course_commit_sha) except ObjectDoesNotExist: event_descr = {} event_kinds_desc = event_descr.get("event_kinds", {}) event_info_desc = event_descr.get("events", {}) event_info_list = [] for event in Event.objects.filter(course=pctx.course, shown_in_calendar=True).order_by("-time"): kind_desc = event_kinds_desc.get(event.kind) human_title = six.text_type(event) event_json = {"id": event.id, "start": event.time.isoformat(), "allDay": event.all_day} if event.end_time is not None: event_json["end"] = event.end_time.isoformat() if kind_desc is not None: if "color" in kind_desc: event_json["color"] = kind_desc["color"] if "title" in kind_desc: if event.ordinal is not None: human_title = kind_desc["title"].format(nr=event.ordinal) else: human_title = kind_desc["title"] description = None show_description = True event_desc = event_info_desc.get(six.text_type(event)) if event_desc is not None: if "description" in event_desc: description = markup_to_html(pctx.course, pctx.repo, pctx.course_commit_sha, event_desc["description"]) if "title" in event_desc: human_title = event_desc["title"] if "color" in event_desc: event_json["color"] = event_desc["color"] if "show_description_from" in event_desc: ds = parse_date_spec(pctx.course, event_desc["show_description_from"]) if now < ds: show_description = False if "show_description_until" in event_desc: ds = parse_date_spec(pctx.course, event_desc["show_description_until"]) if now > ds: show_description = False event_json["title"] = human_title if show_description and description: event_json["url"] = "#event-%d" % event.id start_time = event.time end_time = event.end_time if event.all_day: start_time = start_time.date() local_end_time = as_local_time(end_time) end_midnight = datetime.time(tzinfo=local_end_time.tzinfo) if local_end_time.time() == end_midnight: end_time = (end_time - datetime.timedelta(days=1)).date() else: end_time = end_time.date() event_info_list.append( EventInfo( id=event.id, human_title=human_title, start_time=start_time, end_time=end_time, description=description, ) ) events_json.append(event_json) default_date = now.date() if pctx.course.end_date is not None and default_date > pctx.course.end_date: default_date = pctx.course.end_date from json import dumps return render_course_page( pctx, "course/calendar.html", { "events_json": dumps(events_json), "event_info_list": event_info_list, "default_date": default_date.isoformat(), }, )
def view_calendar(pctx): from course.content import markup_to_html events_json = [] from course.content import get_raw_yaml_from_repo try: event_descr = get_raw_yaml_from_repo(pctx.repo, pctx.course.events_file, pctx.course_commit_sha) except ObjectDoesNotExist: event_descr = {} event_kinds_desc = event_descr.get("event_kinds", {}) event_info_desc = event_descr.get("events", {}) event_info_list = [] for event in (Event.objects .filter( course=pctx.course, shown_in_calendar=True) .order_by("-time")): kind_desc = event_kinds_desc.get(event.kind) human_title = six.text_type(event) event_json = { "id": event.id, "start": event.time.isoformat(), "allDay": event.all_day, } if event.end_time is not None: event_json["end"] = event.end_time.isoformat() if kind_desc is not None: if "color" in kind_desc: event_json["color"] = kind_desc["color"] if "title" in kind_desc: if event.ordinal is not None: human_title = kind_desc["title"].format(nr=event.ordinal) else: human_title = kind_desc["title"] description = None event_desc = event_info_desc.get(six.text_type(event)) if event_desc is not None: if "description" in event_desc: description = markup_to_html( pctx.course, pctx.repo, pctx.course_commit_sha, event_desc["description"]) if "title" in event_desc: human_title = event_desc["title"] if "color" in event_desc: event_json["color"] = event_desc["color"] event_json["title"] = human_title if description: event_json["url"] = "#event-%d" % event.id start_time = event.time end_time = event.end_time if event.all_day: start_time = start_time.date() end_time = end_time.date() event_info_list.append( EventInfo( id=event.id, human_title=human_title, start_time=start_time, end_time=end_time, description=description )) events_json.append(event_json) from course.views import get_now_or_fake_time default_date = get_now_or_fake_time(pctx.request).date() if pctx.course.end_date is not None and default_date > pctx.course.end_date: default_date = pctx.course.end_date from json import dumps return render_course_page(pctx, "course/calendar.html", { "events_json": dumps(events_json), "event_info_list": event_info_list, "default_date": default_date.isoformat(), })