<commit_before>from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.exceptions import APIException

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group

from accounts.serializers import (
    UserSerializer,
    GroupSerializer,
)

User = get_user_model()

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def update(self, request, *args, **kwargs):
        group_ids = request.data.pop('groups', None)
        user = self.get_object()
        if group_ids:
            try:
                groups = [Group.objects.get(id=group_id) for group_id in group_ids]
            except:
                raise APIException('One or more of the given group IDs are invalid')
            for group in groups:
                user.groups.add(group)

        partial = kwargs.pop('partial', False)
        serializer = self.get_serializer(user, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)
        return Response(serializer.data)


class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
<commit_msg>Implements both 'add_to_groups' and 'remove_from_groups'  data parameters<commit_after>from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.exceptions import APIException

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group

from accounts.serializers import (
    UserSerializer,
    GroupSerializer,
)

User = get_user_model()

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def update(self, request, *args, **kwargs):
        add_to_group_ids = request.data.pop('add_to_groups', [])
        remove_from_group_ids = request.data.pop('remove_from_groups', [])
        user = self.get_object()
        self.update_groups(
            user,
            add_to_group_ids,
            remove_from_group_ids
        )
        partial = kwargs.pop('partial', False)
        serializer = self.get_serializer(user, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)
        return Response(serializer.data)

    def update_groups(self, user, add_to_group_ids, remove_from_group_ids):
        try:
            groups = [
                Group.objects.get(id=group_id)
                for group_id in add_to_group_ids + remove_from_group_ids
            ]
        except:
            raise APIException('One or more of the given group IDs are invalid')

        if add_to_group_ids:
            for group in add_to_group_ids:
                user.groups.add(group)

        if remove_from_group_ids:
            for group in remove_from_group_ids:
                user.groups.remove(group)


class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer

{'fraction_non_alphanumeric': 0.07818007049022749, 'fraction_numerical': 0.0, 'mean_word_length': 2.9720101781170483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2853004', 'n_tokens_mistral': 889, 'n_tokens_neox': 873, 'n_words': 196}