Beispiel #1
0
 def build_form(self, form_kwargs=None):
     form = super().build_form(
         form_kwargs={
             "user": self.request.user,
             "lti_context": get_current_lti_session(self.request),
         })
     return form
Beispiel #2
0
 def _get_forum(cls, request):
     """LTIContext can have multiple forums, we target the first one"""
     try:
         if request.user.is_authenticated and request.session.get(
                 SESSION_LTI_CONTEXT_ID):
             context = get_current_lti_session(request)
             return Forum.objects.filter(lti_contexts=context).first()
         return None
     except Exception as no_forum:
         raise PermissionDenied() from no_forum
Beispiel #3
0
    def remove_group_moderator(
        self,
        request,
        pk=None,
    ):
        """Remove group moderator."""
        user = self.get_object()
        lti_context = get_current_lti_session(self.request)
        # Asked to revoke moderator,
        if _FORUM_ROLE_MODERATOR in lti_context.get_user_roles(user):
            user.groups.remove(
                lti_context.get_role_group(_FORUM_ROLE_MODERATOR))

        return Response(self.get_serializer(user).data)
Beispiel #4
0
    def add_group_moderator(
        self,
        request,
        pk=None,
    ):
        """Add group moderator."""
        user = self.get_object()

        lti_context = get_current_lti_session(self.request)

        # Load current groups
        user_groups = lti_context.get_user_roles(user)
        # Asked to become moderator, check user is not yet moderator and make sure user
        # belongs to this context by checking he has the group student from this context.
        if (_FORUM_ROLE_MODERATOR not in user_groups
                and _FORUM_ROLE_STUDENT in user_groups):
            user.groups.add(lti_context.get_role_group(_FORUM_ROLE_MODERATOR))

        return Response(self.get_serializer(user).data)
Beispiel #5
0
 def get_queryset(self):
     context = get_current_lti_session(self.request)
     role = self.request.GET.get("role", None)
     # Select all active users who are part of the current LTIContext
     query = User.objects.filter(
         is_active=True,
         groups__name=context.base_group_name,
     )
     # If a role is specified and start with !, we exclude its members from the results
     if role is not None and role.startswith("!"):
         query = query.exclude(
             groups__name=context.get_group_role_name(role[1:]))
     # If a role is specified, we select only users having this role inside the LTIContext
     elif role:
         query = query.filter(
             groups__name=context.get_group_role_name(role))
     # When the role moderator (or !moderator) is specified, we want to exclude the
     # users having the instructor role from the results.
     if role in [_FORUM_ROLE_MODERATOR, f"!{_FORUM_ROLE_MODERATOR}"]:
         query = query.exclude(groups__name=context.get_group_role_name(
             _FORUM_ROLE_INSTRUCTOR))
     return query.order_by(self.ordering)
Beispiel #6
0
 def get_form_kwargs(self):
     """Returns the keyword arguments used to initialize the associated form."""
     return {
         **super().get_form_kwargs(),
         "lti_context": get_current_lti_session(self.request),
     }