Exemple #1
0
    def get(self, request, group_id):
        """
        GET /api/groups/{group_id}/users/
        """
        try:
            existing_group = Group.objects.get(id=group_id)
        except ObjectDoesNotExist:
            return Response({}, status.HTTP_404_NOT_FOUND)
        users = existing_group.user_set.all()

        is_active = request.QUERY_PARAMS.get('is_active', None)
        if is_active:
            users = users.filter(is_active=str2bool(is_active))

        response_data = {}
        response_data['users'] = []
        for user in users:
            user_data = {}
            user_data['id'] = user.id
            user_data['email'] = user.email
            user_data['username'] = user.username
            user_data['first_name'] = user.first_name
            user_data['last_name'] = user.last_name
            response_data['users'].append(user_data)
        response_status = status.HTTP_200_OK
        return Response(response_data, status=response_status)
Exemple #2
0
 def users(self, request, pk):
     """
     Add a User to an Organization
     """
     if request.method == 'GET':
         include_course_counts = request.QUERY_PARAMS.get('include_course_counts', None)
         users = User.objects.filter(organizations=pk)
         response_data = []
         if users:
             for user in users:
                 serializer = UserSerializer(user)
                 user_data = serializer.data
                 if str2bool(include_course_counts):
                     enrollments = CourseEnrollment.enrollments_for_user(user).count()
                     user_data['course_count'] = enrollments
                 response_data.append(user_data)
         return Response(response_data, status=status.HTTP_200_OK)
     else:
         user_id = request.DATA.get('id')
         try:
             user = User.objects.get(id=user_id)
         except ObjectDoesNotExist:
             message = 'User {} does not exist'.format(user_id)
             return Response({"detail": message}, status.HTTP_400_BAD_REQUEST)
         organization = self.get_object()
         organization.users.add(user)
         organization.save()
         return Response({}, status=status.HTTP_201_CREATED)
Exemple #3
0
 def filter_queryset(self, request, queryset, view):
     """
     Parse querystring base on has_organizations query param
     """
     has_orgs = request.QUERY_PARAMS.get('has_organizations', None)
     if has_orgs:
         if str2bool(has_orgs):
             queryset = queryset.filter(organizations__id__gt=0)
         else:
             queryset = queryset.exclude(id__in=User.objects.filter(organizations__id__gt=0).
                                         values_list('id', flat=True))
     return queryset.distinct()