<commit_before>from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import FbUser, Event
from .serializer import FbUserSerializer
from rest_framework import permissions
import facebook


class MinhaViradaView(APIView):

    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        fb_user_uid = request.query_params.get('uid')
        if fb_user_uid:
            try:
                fb_user = FbUser.objects.get(uid=fb_user_uid)
                serializer = FbUserSerializer(fb_user)
                return Response(serializer.data)
            except FbUser.DoesNotExist:
                return Response('')
        else:
            return Response('')

    def post(self, request, *args, **kwargs):
        fb_user_uid = request.data.get('uid')
        data = request.data
        data['fb_user_uid'] = fb_user_uid
        if fb_user_uid:
            fb_user, _ = FbUser.objects.get_or_create(uid=fb_user_uid)
            serializer = FbUserSerializer(fb_user, data=data)
            if serializer.is_valid():
                serializer.save()
                events = request.data.get('events')
                Event.objects.filter(fb_user=fb_user).exclude(event_id__in=events).delete()
                for event in events:
                    Event.objects.get_or_create(event_id=event, fb_user=fb_user)
            return Response('{status: success}')
        else:
            return Response('{status: fail}')


class FriendsOnEventsView(APIView):

    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        # fb_user_id = request.query_params.get('uid')
        oauth_access_token = request.query_params.get('oauth_token')
        # import ipdb;ipdb.set_trace()
        if not oauth_access_token:
            return Response('Invalid Facebook Oauth Token', 400)
        event_id = request.query_params.get('event_id')
        # TODO save friends in database
        graph = facebook.GraphAPI(oauth_access_token, version='2.3')
        friends = graph.get_connections("me", "friends", fields='id, name', limit=500)
        friend_uids = [data.get('id') for data in friends.get('data')]

        if event_id:
            events = Event.objects.filter(fb_user__uid__in=friend_uids, event_id=event_id)
            events_data = {event_id: [event.fb_user.uid for event in events]}
        else:
            events = Event.objects.filter(fb_user__uid__in=friend_uids)
            events_data = {}
            for event in events:
                fb_user_data = {'uid': event.fb_user.uid,
                               'name': event.fb_user.name,
                               'picture': event.fb_user.picture}
                if event.event_id in events_data.keys():
                    events_data[event.event_id].append(fb_user_data)
                else:
                    events_data[event.event_id] = [fb_user_data]
        print(events_data)
        return Response(events_data)
<commit_msg>fix friends on events api<commit_after>from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import FbUser, Event
from .serializer import FbUserSerializer
from rest_framework import permissions
import facebook


class MinhaViradaView(APIView):

    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        fb_user_uid = request.query_params.get('uid')
        if fb_user_uid:
            try:
                fb_user = FbUser.objects.get(uid=fb_user_uid)
                serializer = FbUserSerializer(fb_user)
                return Response(serializer.data)
            except FbUser.DoesNotExist:
                return Response('')
        else:
            return Response('')

    def post(self, request, *args, **kwargs):
        fb_user_uid = request.data.get('uid')
        data = request.data
        data['fb_user_uid'] = fb_user_uid
        if fb_user_uid:
            fb_user, _ = FbUser.objects.get_or_create(uid=fb_user_uid)
            serializer = FbUserSerializer(fb_user, data=data)
            if serializer.is_valid():
                serializer.save()
                events = request.data.get('events')
                Event.objects.filter(fb_user=fb_user).exclude(event_id__in=events).delete()
                for event in events:
                    Event.objects.get_or_create(event_id=event, fb_user=fb_user)
            return Response('{status: success}')
        else:
            return Response('{status: fail}')


class FriendsOnEventsView(APIView):

    permission_classes = (permissions.AllowAny,)

    def get(self, request, *args, **kwargs):
        # fb_user_id = request.query_params.get('uid')
        oauth_access_token = request.query_params.get('oauth_token')
        # import ipdb;ipdb.set_trace()
        if not oauth_access_token:
            return Response('Invalid Facebook Oauth Token', 400)
        event_id = request.query_params.get('event_id')
        # TODO save friends in database
        graph = facebook.GraphAPI(oauth_access_token, version='2.3')
        friends = graph.get_connections("me", "friends", fields='id, name', limit=500)
        friend_uids = [data.get('id') for data in friends.get('data')]

        if event_id:
            events = Event.objects.filter(fb_user__uid__in=friend_uids, event_id=event_id)
            events_data = {event_id: [event.fb_user.uid for event in events]}
        else:
            events = Event.objects.filter(fb_user__uid__in=friend_uids)
            events_data = {}
            for event in events:
                fb_user_data = {'uid': event.fb_user.uid,
                               'name': event.fb_user.name,
                               'picture': event.fb_user.picture}
                if event.event_id in events_data.keys():
                    events_data[event.event_id].append(fb_user_data)
                else:
                    events_data[event.event_id] = [fb_user_data]
        return Response(events_data)

{'fraction_non_alphanumeric': 0.0978633175664655, 'fraction_numerical': 0.0026096884684390803, 'mean_word_length': 2.3289902280130295, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5267582', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1748, 'n_words': 353}