<commit_before>from django.db.models import Q
from django_filters.rest_framework import CharFilter
from django_filters.rest_framework import DjangoFilterBackend
from django_filters.rest_framework import FilterSet
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT

from contentcuration.models import Invitation
from contentcuration.models import User
from contentcuration.viewsets.base import BulkListSerializer
from contentcuration.viewsets.base import BulkModelSerializer
from contentcuration.viewsets.base import ValuesViewset


class InvitationSerializer(BulkModelSerializer):
    accepted = serializers.BooleanField(default=False)

    class Meta:
        model = Invitation
        fields = ("id", "accepted", "email", "channel", "share_mode", "first_name", "last_name")
        list_serializer_class = BulkListSerializer

    def save(self, **kwargs):
        created = self.instance is None
        if "email" in self.validated_data:
            try:
                self.validated_data["invited"] = User.objects.get(
                    email=self.validated_data["email"]
                )
                self.validated_data.pop("email")
            except User.DoesNotExist:
                pass
        if created and "request" in self.context:
            # If this has been newly created add the current user as the sender
            self.validated_data["sender"] = self.context["request"].user
        instance = super(InvitationSerializer, self).save(**kwargs)
        return instance


class InvitationFilter(FilterSet):
    invited = CharFilter(method="filter_invited")
    channel = CharFilter(method="filter_channel")

    class Meta:
        model = Invitation
        fields = ("invited", "channel",)

    def filter_invited(self, queryset, name, value):
        return queryset.filter(email=self.request.user.email)

    def filter_channel(self, queryset, name, value):
        return queryset.filter(channel_id=value)


def get_sender_name(item):
    return "{} {}".format(item.get("sender__first_name"), item.get("sender__last_name"))


class InvitationViewSet(ValuesViewset):
    queryset = Invitation.objects.all()
    filter_backends = (DjangoFilterBackend,)
    filter_class = InvitationFilter
    serializer_class = InvitationSerializer
    values = (
        "id",
        "invited",
        "email",
        "invited__first_name",
        "invited__last_name",
        "sender__first_name",
        "sender__last_name",
        "channel_id",
        "share_mode",
        "channel__name",
    )
    field_map = {
        "first_name": "invited__first_name",
        "last_name": "invited__last_name",
        "sender_name": get_sender_name,
        "channel_name": "channel__name",
        "channel": "channel_id",
        "accepted": False,
    }

    def get_queryset(self):
        return Invitation.objects.filter(
            Q(email=self.request.user.email)
            | Q(sender=self.request.user)
            | Q(channel__editors=self.request.user)
            | Q(channel__viewers=self.request.user)
        ).distinct()

    def prefetch_queryset(self, queryset):
        return queryset.select_related("sender", "channel")

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()

        if request.query_params.get("accepted", False):
            instance.accept()
        else:
            instance.delete()
        return Response(status=HTTP_204_NO_CONTENT)<commit_msg>Make invitations case insensitive on email<commit_after>from django.db.models import Q
from django_filters.rest_framework import CharFilter
from django_filters.rest_framework import DjangoFilterBackend
from django_filters.rest_framework import FilterSet
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT

from contentcuration.models import Invitation
from contentcuration.models import User
from contentcuration.viewsets.base import BulkListSerializer
from contentcuration.viewsets.base import BulkModelSerializer
from contentcuration.viewsets.base import ValuesViewset


class InvitationSerializer(BulkModelSerializer):
    accepted = serializers.BooleanField(default=False)

    class Meta:
        model = Invitation
        fields = ("id", "accepted", "email", "channel", "share_mode", "first_name", "last_name")
        list_serializer_class = BulkListSerializer

    def save(self, **kwargs):
        created = self.instance is None
        if "email" in self.validated_data:
            try:
                self.validated_data["invited"] = User.objects.get(
                    email=self.validated_data["email"]
                )
                self.validated_data.pop("email")
            except User.DoesNotExist:
                pass
        if created and "request" in self.context:
            # If this has been newly created add the current user as the sender
            self.validated_data["sender"] = self.context["request"].user
        instance = super(InvitationSerializer, self).save(**kwargs)
        return instance


class InvitationFilter(FilterSet):
    invited = CharFilter(method="filter_invited")
    channel = CharFilter(method="filter_channel")

    class Meta:
        model = Invitation
        fields = ("invited", "channel",)

    def filter_invited(self, queryset, name, value):
        return queryset.filter(email__iexact=self.request.user.email)

    def filter_channel(self, queryset, name, value):
        return queryset.filter(channel_id=value)


def get_sender_name(item):
    return "{} {}".format(item.get("sender__first_name"), item.get("sender__last_name"))


class InvitationViewSet(ValuesViewset):
    queryset = Invitation.objects.all()
    filter_backends = (DjangoFilterBackend,)
    filter_class = InvitationFilter
    serializer_class = InvitationSerializer
    values = (
        "id",
        "invited",
        "email",
        "invited__first_name",
        "invited__last_name",
        "sender__first_name",
        "sender__last_name",
        "channel_id",
        "share_mode",
        "channel__name",
    )
    field_map = {
        "first_name": "invited__first_name",
        "last_name": "invited__last_name",
        "sender_name": get_sender_name,
        "channel_name": "channel__name",
        "channel": "channel_id",
        "accepted": False,
    }

    def get_queryset(self):
        return Invitation.objects.filter(
            Q(email__iexact=self.request.user.email)
            | Q(sender=self.request.user)
            | Q(channel__editors=self.request.user)
            | Q(channel__viewers=self.request.user)
        ).distinct()

    def prefetch_queryset(self, queryset):
        return queryset.select_related("sender", "channel")

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()

        if request.query_params.get("accepted", False):
            instance.accept()
        else:
            instance.delete()
        return Response(status=HTTP_204_NO_CONTENT)
{'fraction_non_alphanumeric': 0.09723197976675566, 'fraction_numerical': 0.0016861036953772657, 'mean_word_length': 3.0535307517084282, 'pattern_counts': {'":': 12, '<': 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': '2555801', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1965, 'n_words': 418}