def get_courses() -> JSONResponse[t.Sequence[t.Mapping[str, t.Any]]]: """Return all :class:`.models.Course` objects the current user is a member of. .. :quickref: Course; Get all courses the current user is enrolled in. :returns: A response containing the JSON serialized courses :param str extended: If set to ``true``, ``1`` or the empty string all the assignments and group sets for each course are also included under the key ``assignments`` and ``group_sets`` respectively. :>jsonarr str role: The name of the role the current user has in this course. :>jsonarr ``**rest``: JSON serialization of :py:class:`psef.models.Course`. :raises PermissionException: If there is no logged in user. (NOT_LOGGED_IN) """ def _get_rest(course: models.Course) -> t.Mapping[str, t.Any]: if helpers.extended_requested(): snippets: t.Sequence[models.CourseSnippet] = [] if (current_user.has_permission(GPerm.can_use_snippets) and current_user.has_permission( CPerm.can_view_course_snippets, course_id=course.id)): snippets = course.snippets return { 'assignments': course.get_all_visible_assignments(), 'group_sets': course.group_sets, 'snippets': snippets, **course.__to_json__(), } return course.__to_json__() extra_loads: t.Optional[t.List[t.Any]] = None if helpers.extended_requested(): extra_loads = [ selectinload(models.Course.assignments), selectinload(models.Course.snippets), selectinload(models.Course.group_sets) ] # We don't use `helpers.get_or_404` here as preloading doesn't seem to work # when we do. user = models.User.query.filter_by(id=current_user.id).options([ selectinload(models.User.courses, ).selectinload( models.CourseRole._permissions, # pylint: disable=protected-access ), ]).first() assert user is not None return jsonify([{ 'role': user.courses[c.id].name, **_get_rest(c), } for c in helpers.get_in_or_error( models.Course, t.cast(models.DbColumn[int], models.Course.id), [cr.course_id for cr in user.courses.values()], extra_loads, )])
def _get_rest(course: models.Course) -> t.Mapping[str, t.Any]: if helpers.extended_requested(): snippets: t.Sequence[models.CourseSnippet] = [] if (current_user.has_permission(GPerm.can_use_snippets) and current_user.has_permission( CPerm.can_view_course_snippets, course_id=course.id)): snippets = course.snippets return { 'assignments': course.get_all_visible_assignments(), 'group_sets': course.group_sets, 'snippets': snippets, **course.__to_json__(), } return course.__to_json__()
def self_information( ) -> t.Union[JSONResponse[t.Union[models.User, t.MutableMapping[str, t.Any], t. Mapping[int, str]]], ExtendedJSONResponse[t.Union[models.User, t.MutableMapping[str, t. Any]]], ]: """Get the info of the currently logged in :class:`.models.User`. .. :quickref: User; Get information about the currently logged in user. :query type: If this is ``roles`` a mapping between course_id and role name will be returned, if this is ``extended`` the result of :py:meth:`.models.User.__extended_to_json__()` will be returned. If this is something else or not present the result of :py:meth:`.models.User.__to_json__()` will be returned. :query with_permissions: Setting this to true will add the key ``permissions`` to the user. The value will be a mapping indicating which global permissions this user has. :returns: A response containing the JSON serialized user :raises PermissionException: If there is no logged in user. (NOT_LOGGED_IN) """ args = request.args if args.get('type') == 'roles': return jsonify( { role.course_id: role.name for role in current_user.courses.values() } ) elif helpers.extended_requested() or args.get('type') == 'extended': obj = current_user.__extended_to_json__() if request_arg_true('with_permissions'): obj['permissions'] = GPerm.create_map( current_user.get_all_permissions() ) return extended_jsonify(obj) return jsonify(current_user)